Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
53 views

Linux Module

This document discusses Linux commands and their uses in the command line interface. It begins by explaining that the CLI is a powerful tool for administering devices and servers. It then defines commands as programs that perform actions when executed, and explains how to execute the basic ls command to list files. The document proceeds to cover additional commands like cd for changing directories, pwd for printing the working directory, and options and arguments that customize commands. It provides examples of navigating the file system and using relative and absolute paths.

Uploaded by

alma cablinan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Linux Module

This document discusses Linux commands and their uses in the command line interface. It begins by explaining that the CLI is a powerful tool for administering devices and servers. It then defines commands as programs that perform actions when executed, and explains how to execute the basic ls command to list files. The document proceeds to cover additional commands like cd for changing directories, pwd for printing the working directory, and options and arguments that customize commands. It provides examples of navigating the file system and using relative and absolute paths.

Uploaded by

alma cablinan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OPERATING SYSTEMS: FINAL TERM

LINUX COMMANDS AND USES


This module deals exclusively with the CLI or Command Line Interface, rather than a GUI or
Graphical User Interface you may be familiar with. The CLI terminal is a powerful tool that is
often the primary method used to administer small low-power devices, extremely capable cloud
computing servers, and everything in between. A basic understanding of the terminal is
essential to diagnosing and fixing most Linux-based systems. Since Linux has now become so
ubiquitous, even those who plan on working primarily with systems not utilizing the Linux kernel
can benefit from having a basic understanding of the terminal.
What is a command? A command is a software program that when executed on the CLI
(Command Line Interface), performs an action on the computer. When you type in a command,
a process is run by the operating system that can read input, manipulate data and produce
output. A command runs a process on the operating system, which then causes the computer to
perform a job.
To execute a command, the first step is to type the name of the command.
Type ls (lowercase letters L and S) and hit Enter. The result should resemble the example
below:

The name of the command is often based on what it does or what the developer who created the
command thinks will best describe the command's function. For example, the ls command displays
a listing of information about files. Associating the name of the command with something mnemonic
for what it does may help you to remember commands more easily.

Consider This
Every part of the command is normally case-sensitive, so LS is incorrect and will fail, but ls is
correct and will execute.

Most commands follow a simple pattern of syntax:

In other words, you type a command, followed by any options and/or arguments before pressing


the Enter key. Typically, options alter the behavior of the command and arguments are items or
values upon the command to act upon. Although there are some commands in Linux that aren’t
entirely consistent with this syntax, most commands use this syntax or something similar.
In the example above, the ls command was executed without any options or arguments. When this
is the case, its default behavior is to return a list of files contained within the current directory.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

Arguments

An argument can be used to specify something for the command to act upon. The ls command can
be given the name of a directory as an argument, and it will list the contents of that directory. In the
next example, the Documents directory will be used as an argument:

The resulting output is a list of files contained in the Documents directory.

Options

Options can be used to alter the behavior of a command. On the previous page, the ls command
was used to list the contents of a directory. In the following example, the  -l option is provided to
the ls command, which results in a "long display" output, meaning the output gives more information
about each of the files listed:

Often the character is chosen to be prompt for its purpose, like choosing the
letter l for long or r for reverse. By default, the ls command prints the results in alphabetical order,
so adding the -r option will print the results in reverse alphabetical order.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
Multiple options can be used at once, either given as
separate options as in -l -r or combined like -lr. The
output of all of these examples would be the same:

As explained above, -l gives a long listing format while -


r reverses the listing. The result of using both options is a
long listing given in reverse order:

Printing Working Directory


In order to discover where you are currently located within the filesystem, the pwd command can be
used. The pwd command prints the working directory, your current location within the filesystem:

The output of the above command indicates that the user is currently in their home folder, shown in
the filesystem below.

Changing Directories
Files are used to store data such as text, graphics and programs. Directories are a type of file
used to store other files–they provide a hierarchical organizational structure.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
To navigate the filesystem structure, use the cd (change directory) command to change directories.

cd [options] [path]

If you look back at the graphic above, you will see the Documents directory is located within
the home directory, where you are currently located. To move to the Documents directory, use it as
argument to the cd command:

sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$

The argument to the cd command is more than just the name of a directory, it is actually a path. A
path is a list of directories separated by the / character. For example, /home/sysadmin is the path
to your home directory:

Absolute Path
An absolute path allows you to specify the exact location of a directory. It always starts at the root
directory; therefore, it always begins with the / character. The path to the home
directory /home/sysadmin is an absolute path. The path begins at the root / directory, moves
into the home directory, and then into the sysadmin directory. 

A relative path gives directions to a file relative to your current location in the filesystem. Relative
paths do not start with the / character, they start with the name of a directory. Take another look at
the first cd command example. The argument is an example of the simplest relative path: the name
of a directory in your current location.

sysadmin@localhost:~$ cd Documents
sysadmin@localhost:~/Documents$

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

The image below shows a map of the files contained within the sysadmin directory. You are
currently in the Documents directory and want to move to the Art directory:

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
A relative path begins in from with the current directory, however you don't include it in the path. The
first step would be to move into the School directory, and then move into the Art directory. Use
the / character to separate the directory names and the result School/Art is a relative path from
the Documents directory to the Art directory:

Use the relative path as an argument to the cd command to move into the Art directory.

sysadmin@localhost:~/Documents/$ cd School/Art
sysadmin@localhost:~/Documents/School/Art$

Use the pwd command to confirm the change:

sysadmin@localhost:~/Documents/School/Art$ pwd
/home/sysadmin/Documents/School/Art

Consider This
The output of the pwd command is the absolute path to the Art directory.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

Consider This
In the example above the cd command followed the School/Art path:
cd School/Art

A path can also be broken down into multiple cd commands. The following set of commands would
achieve the same results:
cd School
cd Art

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

Shortcuts
The .. Characters
Regardless of which directory you are in, .. always represents one directory higher relative to the
current directory, sometimes referred to as the parent directory. To move from the Art directory
back to the School directory:

sysadmin@localhost:~/Documents/School/Art$ cd ..
sysadmin@localhost:~/Documents/School$

The . Character
Regardless of which directory you are in, the . character always represents your current directory.
For the cd this shortcut is not very useful, but it will come in handy for commands covered in
subsequent sections.
The ~ Character
The home directory of the current user is represented by the ~ character. As stated above, you
always begin as the sysadmin user, whose home is located at /home/sysadmin. To return to your
home directory at any time execute the following command:

sysadmin@localhost:~/Documents/School$ cd ~
sysadmin@localhost:~$

Listing Files
The ls command is used to list the contents of a directory. You've already seen it used a few times
before in examples, but this page will help ensure you are comfortable with its use.

ls [OPTIONS] [FILE]

By default, when the ls command is used with no options or arguments, it will list the files in the
current directory:

sysadmin@localhost:~$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos

To learn the details about a file, such as the type of file, the permissions, ownerships or the
timestamp, perform a long listing using the -l option to the ls command. Below, a listing of
the /var/log directory is used as an example, since it provides a variety of output:

sysadmin@localhost:~$ ls -l /var/log/
total 844
-rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

drwxr-x--- 2 root adm 4096 Dec 20 2017 apache2


drwxr-xr-x 1 root root 4096 Dec 20 2017 apt
-rw-r----- 1 syslog adm 1346 Oct 2 22:17 auth.log
-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log
-rw-rw---- 1 root utmp 0 Dec 7 2017 btmp
-rw-r----- 1 syslog adm 547 Oct 2 22:17 cron.log
-rw-r----- 1 root adm 85083 Dec 20 2017 dmesg
-rw-r--r-- 1 root root 325238 Dec 20 2017 dpkg.log
-rw-r--r-- 1 root root 32064 Dec 20 2017 faillog
drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck
-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log
-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog
-rw-r----- 1 syslog adm 19573 Oct 2 22:57 syslog
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
-rw-rw-r-- 1 root utmp 384 Oct 2 19:57 wtmp

Each line corresponds to a file contained within the directory. The information can be broken down
into fields separated by spaces. The fields are as follows:
 File Type

 - rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log



 d rwxr-x--- 2 root adm 4096 Dec 20 2017 apache2

The first field actually contains ten characters, where the first character indicates the type of
file and the next nine specify permissions. The file types are:

Symbo
l File Type Description

d directory A file used to store other files.

- regular file Includes readable files, images files, binary files, and
compressed files.

l symbolic Points to another file.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

Symbo
l File Type Description

link

s socket Allows for communication between processes.

p pipe Allows for communication between processes.

b block file Used to communicate with hardware.

c character Used to communicate with hardware.


file

The first file alternatives.log is a regular file -, while the second file apache2 is a


directory d.
Permissions

d rwxr-xr-x 2 root root 4096 Apr 11 2014 upstart

Permissions indicate how certain users can access a file. Keep reading to learn more about
permissions.
 Hard Link Count

-rw-r----- 1 syslog adm 1346 Oct 2 22:17 auth.log

This number indicates how many hard links point to this file. Hard links are beyond the scope
of this module, but are covered in the NDG Linux Essentials course.
 User Owner

-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log

User syslog owns this file. Every time a file is created, the ownership is automatically
assigned to the user who created it.
 Group Owner

-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog

Indicates which group owns this file

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
 File Size

-rw-r----- 1 syslog adm 19573 Oct 2 22:57 syslog

Directories and larger files may be shown in kilobytes since displaying their size in bytes
would present a very large number. Therefore, in the case of a directory, it might actually be
a multiple of the block size used for the file system. Block size is the size of a series of data
stored in the filesystem.
 Timestamp

drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck

This indicates the time that the file's contents were last modified.
 Filename

-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log

The final field contains the name of the file or directory.


Consider This
In the case of symbolic links, a file that points to another file, the link name will be displayed
along with an arrow and the pathname of the original file.

lrwxrwxrwx. 1 root root 22 Nov 6 2012 /etc/grub.conf ->


../boot/grub/grub.conf

Symbolic links are beyond the scope of this module, but are covered in the NDG Linux
Essentials course.
Sorting
By default the output of the ls command is sorted alphabetically by filename. It can sort by other
methods as well.
Follow Along
The options in examples below will be combined with the -l option so the relevant details of the files
are displayed. Notice fields corresponding to the search option.
The -t option will sort the files by timestamp:

sysadmin@localhost:~$ ls -lt /var/log


total 844
-rw-r----- 1 syslog adm 19573 Oct 2 22:57 syslog
-rw-r----- 1 syslog adm 1346 Oct 2 22:17 auth.log
-rw-r----- 1 syslog adm 547 Oct 2 22:17 cron.log
-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog
-rw-rw-r-- 1 root utmp 384 Oct 2 19:57 wtmp
-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

-rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log


-rw-r--r-- 1 root root 32064 Dec 20 2017 faillog
-rw-r----- 1 root adm 85083 Dec 20 2017 dmesg
-rw-r--r-- 1 root root 325238 Dec 20 2017 dpkg.log
drwxr-x--- 2 root adm 4096 Dec 20 2017 apache2
drwxr-xr-x 1 root root 4096 Dec 20 2017 apt
-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log
drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck
-rw-rw---- 1 root utmp 0 Dec 7 2017 btmp
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart

The -S option will sort the files by file size:

sysadmin@localhost:~$ ls -l -S /var/log
total 844
-rw-r--r-- 1 root root 325238 Dec 20 2017 dpkg.log
-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog
-rw-r----- 1 root adm 85083 Dec 20 2017 dmesg
-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log
-rw-r--r-- 1 root root 32064 Dec 20 2017 faillog
-rw-r----- 1 syslog adm 19573 Oct 2 22:57 syslog
-rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log
drwxr-x--- 2 root adm 4096 Dec 20 2017 apache2
drwxr-xr-x 1 root root 4096 Dec 20 2017 apt
drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
-rw-r----- 1 syslog adm 1346 Oct 2 22:17 auth.log
-rw-r----- 1 syslog adm 547 Oct 2 22:17 cron.log
-rw-rw-r-- 1 root utmp 384 Oct 2 19:57 wtmp
-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log
-rw-rw---- 1 root utmp 0 Dec 7 2017 btmp

The -r option will reverse the order of any type of sort. Notice the difference when it is added to the
previous example:

sysadmin@localhost:~$ ls -lSr /var/log

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

total 844
-rw-rw---- 1 root utmp 0 Dec 7 2017 btmp
-rw-r----- 1 syslog adm 106 Oct 2 19:57 kern.log
-rw-rw-r-- 1 root utmp 384 Oct 2 19:57 wtmp
-rw-r----- 1 syslog adm 654 Oct 2 23:17 cron.log
-rw-r----- 1 syslog adm 1669 Oct 2 23:17 auth.log
drwxr-xr-x 2 root root 4096 Apr 11 2014 upstart
drwxr-xr-x 2 root root 4096 Dec 7 2017 fsck
drwxr-xr-x 1 root root 4096 Dec 20 2017 apt
drwxr-x--- 2 root adm 4096 Dec 20 2017 apache2
-rw-r--r-- 1 root root 18047 Dec 20 2017 alternatives.log
-rw-r----- 1 syslog adm 19680 Oct 2 23:17 syslog
-rw-r--r-- 1 root root 32064 Dec 20 2017 faillog
-rw-r--r-- 1 root root 47816 Dec 7 2017 bootstrap.log
-rw-r----- 1 root adm 85083 Dec 20 2017 dmesg
-rw-rw-r-- 1 root utmp 292584 Oct 2 19:57 lastlog
-rw-r--r-- 1 root root 325238 Dec 20 2017 dpkg.log

The numbers in file size field switch from descending to ascending.


Used alone the -r option with list the files in reverse alphabetical order:

sysadmin@localhost:~$ ls -r /var/log
wtmp lastlog faillog cron.log auth.log alternatives.log
upstart kern.log dpkg.log btmp apt
syslog fsck dmesg bootstrap.log apache2

Viewing Files
There are a few Linux commands available to view the content of files. The cat command, which
stands for “concatenate”, is often used to quickly view the contents of small files.
The cat command will display the entire contents of the file, hence why it is mainly recommended
for smaller files where the output is limited and does not require scrolling. To view the contents of a
file using the cat command, simply type the command and use the name of the file you wish to view
as the argument:

cat [OPTIONS] [FILE]

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
Our VM has a few small text files that you can view with the cat command. One such file is
the animals.txt file:
Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents
sysadmin@localhost:~/Documents$ cat animals.txt
1 retriever
2 badger
3 bat
4 wolf
5 eagle

The cat command displays all five lines of the file above. When viewing larger files,
the cat command can result in very lengthy output that cannot be paused to scroll through. A better
method of viewing long text files, is with a pager command which has a functionality that can pause
and scroll through the output of the file.

Copying Files
Creating copies of files can be useful for numerous reasons:

 If a copy of a file is created before changes are made, then it is possible to revert back to the
original.
 A copy of a file can be used to transfer a file to removable media devices.
 A copy of an existing document can be used as a template for a new document.

cp [OPTIONS] SOURCE DESTINATION

Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents

The cp command is used to copy files. Similar to the mv command, it requires at least two


arguments: a source and a destination. For example, to copy the /etc/passwd file to the current
directory, use the following command:

sysadmin@localhost:~/Documents$ cp /etc/passwd .

Note
The second argument is the . character. Recall from the Changing Directories section that is a
shortcut which represents the current directory.

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
The result of executing the previous command would create a copy of the contents of
the /etc/passwd file in the Documents directory, since that is our current directory. This can be
confirmed using the ls command:

sysadmin@localhost:~/Documents$ ls
School alpha-third.txt hidden.txt numbers.txt red.txt
Work alpha.txt letters.txt os.csv
adjectives.txt animals.txt linux.txt passwd
alpha-first.txt food.txt longfile.txt people.csv
alpha-second.txt hello.sh newhome.txt profile.txt

Consider This
Permissions can have an impact on file management commands, such as the cp command. In order
to copy a file, it is necessary to have execute permission to access the directory where the file is
located and the read permission for the file being copied.
It is also necessary to have write and execute permission on the directory the file is being copied to.
Typically, there are two places where you should always have write and execute permission on the
directory: your home directory and the /tmp directory.

Moving Files
The mv command is used to move a file from one location in the filesystem to another.

mv SOURCE DESTINATION

The mv command requires at least two arguments. The first argument is the source, a path to the file
to be moved. The second argument is the destination, a path to where the file will be moved to. The
files to be moved are sometimes referred to as the source, and the place where the files are to be
placed is called the destination.
Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents

To move the people.csv file into the Work directory, use the filename as the source, and the
directory name as the destination:

sysadmin@localhost:~/Documents$ mv people.csv Work

If a file is moved from one directory to another without specifying a new name for the file, it will retain
its original name. The move above can be confirmed using the ls command on the Work directory:

sysadmin@localhost:~/Documents$ ls Work
people.csv

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES
The mv command is able to move multiple files, as long as the final argument provided to the
command is the destination. For example, to move three files into the School directory:

sysadmin@localhost:~/Documents$ mv numbers.txt letters.txt alpha.txt School


sysadmin@localhost:~/Documents$ ls School
Art Engineering Math alpha.txt letters.txt numbers.txt

Moving a file within the same directory is an effective way to rename it. For example, in the following
example the animals.txt file is given a new name of zoo.txt:

mv animals.txt zoo.txt
sysadmin@localhost:~/Documents$ ls
School alpha-second.txt hello.sh newhome.txt red.txt
Work alpha-third.txt hidden.txt os.csv
adjectives.txt animals.txt linux.txt passwd
alpha-first.txt food.txt longfile.txt profile.txt
sysadmin@localhost:~/Documents$ mv animals.txt zoo.txt
sysadmin@localhost:~/Documents$ ls
School alpha-second.txt hidden.txt os.csv zoo.txt
Work alpha-third.txt linux.txt passwd
adjectives.txt food.txt longfile.txt profile.txt
alpha-first.txt hello.sh newhome.txt red.txt

Consider This
Permissions can have an impact on file management commands, such as the mv command. Moving
a file requires write and execute permissions on both the origin and destination directories.

Removing Files
The rm command is used to delete files and directories. It is important to keep in mind that deleted
files and directories do not go into a "trash can" as with desktop-oriented operating systems. When a
file is deleted with the rm command, it is almost always permanently gone.

rm [OPTIONS] FILE

Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents

Without any options, the rm command is typically used to remove regular files:

Prepared by: Alma P. Cablinan | CCS Faculty


OPERATING SYSTEMS: FINAL TERM
LINUX COMMANDS AND USES

sysadmin@localhost:~/Documents$ rm linux.txt
sysadmin@localhost:~/Documents$ ls linux.txt
ls: cannot access linux.txt: No such file or directory

The rm command will ignore directories that it's asked to remove; to delete a directory, use a
recursive option, either the -r or -R options. Just be careful since these options are "recursive", this
will delete all files and all subdirectories:

sysadmin@localhost:~/Documents$ rm Work
rm: cannot remove 'Work': Is a directory
sysadmin@localhost:~/Documents$ rm -r Work
sysadmin@localhost:~/Documents$ ls Work
ls: cannot access Work: No such file or directory

Warning
The rm command removes files permanently. To repeat the examples above, reset the terminal
using the reset button.
Consider This
Permissions can have an impact on file management commands, such as the rm command.
To delete a file within a directory, a user must have write and execute permission on a directory.
Regular users typically only have this type of permission in their home directory and its
subdirectories.

Prepared by: Alma P. Cablinan | CCS Faculty

You might also like