Linux Module
Linux Module
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.
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:
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.
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.
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$
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:
sysadmin@localhost:~/Documents/$ cd School/Art
sysadmin@localhost:~/Documents/School/Art$
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.
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
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
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
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
- regular file Includes readable files, images files, binary files, and
compressed files.
Symbo
l File Type Description
link
Permissions indicate how certain users can access a file. Keep reading to learn more about
permissions.
Hard Link Count
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
User syslog owns this file. Every time a file is created, the ownership is automatically
assigned to the user who created it.
Group Owner
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
This indicates the time that the file's contents were last modified.
Filename
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 -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:
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
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:
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.
Follow Along
Use the following command to switch to the Documents directory:
sysadmin@localhost:~$ cd ~/Documents
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.
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:
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
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
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.