3. The Filesystem

Regardless of what you plan to use the Linux distribution for, you need to know how to navigate the filesystem. The filesystem is how the files are structurally organized into directories. Understanding this structure and how to manage the files is critical to using Linux.

Understanding the Filesystem

Typically, new Linux users have some experience in another operating system, such as Microsoft Windows. One of the challenges of using the Linux filesystem is understanding that the layout is likely to be different than what you are used to.

For example, in Microsoft Windows, physical drives are assigned letters, such as C: or F:. They may be visible under the My Computer icon. Linux doesn’t use drive letters or a My Computer icon. Instead, all drives, including network drives and removable media, are located under the root directory.

The root directory is symbolized by the / character. This character is also used to separate directory and filenames in a path. Think of the path as directions to get to a file or directory. For example, the path /home/bob/sample.txt refers to a file named sample.txt that is in the bob directory. The bob directory is under the home directory, which in turn is under the root directory. See Figure 3.1 for a small example of a Linux filesystem.

Figure 3.1 Part of a Linux filesystem

Learning the Most Used Directories

Thousands of directories are in a typical Linux filesystem. You should not worry about learning about all of these directories when you first start learning Linux, but some are important enough to learn now:

/home—A user’s home directory; each user has a directory under the /home directory where she can store her files. This is one of the few places where the user should always have the right to create and manage files.

/root—Root user’s home directory; the system administrator account on the system is called the root user. The home directory for the root user isn’t under the /home directory; instead it is under the /root directory.1

/bin—Executables (programs); most of the commands you execute as a regular user are placed here or in the /usr/bin directory.

/usr/bin—Executables (programs); most of the commands you execute as a regular user are placed here or in the /bin directory.

/sbin—Executables (programs) for system administrators; most of the commands you execute as a system administrator are placed here or in the /usr/sbin directory.

/usr/sbin—Executables (programs) for system administrators; most of the commands you execute as a system administrators are placed here or in the /sbin directory.

/media—Removable media (could also be /run/media); this is where you find the files for removable devices, such as CD-ROMs and USB drives.

/tmp—Temporary files; typically, programs store files in this directory rather than placing files in a user’s home directory.

Naming Considerations

When you create a file or directory, you should take into consideration the following guidelines:

Files and directories have same name rules.

Names are case sensitive. This means that a file named Data.txt is not the same as a file named data.txt.

Special characters are permitted. However, you should avoid using whitespace (space and tab characters) and certain special characters called metacharacters (*, ?, [, ], !, $, &, and so on). Metacharacters are special characters to BASH, and they can create problems when used in a file or directory name.

The / character is used to separate files and directory names: /usr/share/doc. As a result, you cannot use the / character in file or directory names.

Extensions (.txt, .cvs, and so on) are permitted but normally have no special meaning to BASH. In some rare cases they may be required for a specific command, but Linux and the BASH shell typically don’t require specific extensions for files. However, extensions are useful for users because understanding the purpose of a file is easier if the extension is provided (some GUI interfaces also make use of extensions to determine which GUI-based program to launch).

There are some special predefined directory names:

~ —Represents the current user’s home directory

. —Represents the current working directory (the directory you are working in when using a command-line shell)

.. —Represents one level above the current working directory

Navigating the Filesystem

When using the command-line environment, you often need to refer to a directory structure to access a file or subdirectory. For example, you may want to view a file in a specific directory.

When you first open a shell, you are automatically placed in your home directory. The directory you are in is referred to as your working directory or current directory. A common task is to switch the working directory to another directory, a process called change directory.

A path or pathname is how you refer to a file or directory in the directory structure. The two types of pathnames are

Absolute—A path that always starts from the root directory; for example, /home/bob/sample.txt

Relative—A path that starts from the current directory; for example, if you are in the /home directory and want to access the sample.txt file that is under the bob directory (which is under the /home directory), you use bob/sample.txt.


Note

Absolute paths always start with a / character but relative paths never start with a / character.


To help understand the difference between an absolute and relative path, consider Figure 3.2.

Figure 3.2 Using a pathname, example #1

In this example you are in the /home/julia directory and want to switch to the /usr/bin directory. To switch directories, you can use the cd command, and to view the current directory you can use the pwd1 directory. The following example demonstrates using an absolute path:

julia@mintos:~ > pwd
/home/julia
julia@mintos:~ > cd /usr/bin
julia@mintos:/usr/bin > pwd
/usr/bin

Note that the prompt (julia@mintos:/usr/bin) indicates the current directory, so the pwd command isn’t always necessary. This isn’t always the case because the prompt is customizable.

The next example demonstrates using a relative path to move from the /home/julia directory to the /usr/bin directory. Note that the cd command, when given no arguments, returns you to your home directory:

julia@mintos:/usr/bin > cd
julia@mintos:~ > pwd
/home/julia
julia@mintos:~ > cd ../../usr/bin
julia@mintos:/usr/bin > pwd
/usr/bin

Why use ..? Because a relative path requires giving directions from the current directory, and you have to tell the cd command to “go up” two levels before going down to the usr and bin directories.

Clearly, in this case, the absolute path was easier, but that isn’t always the case. For example, now consider Figure 3.3.

Figure 3.3 Using a pathname, example #2

Now you want to move from the /home/julia directory to the /home/sarah directory. The first example uses an absolute path:

julia@mintos:/usr/bin > cd
julia@mintos:~ > cd /home/sarah
julia@mintos:/home/sarah > pwd
/home/sarah

Compare the previous example with the following:

julia@mintos:/home/sarah > cd
julia@mintos:~ > cd ../sarah
julia@mintos:/home/sarah > pwd
/home/sarah

The relative method is easier to use in this case. Consider if you were 20 levels deep in the directory structure and you just wanted to move up one level and down to another subdirectory. Using an absolute path would result in a lot of typing whereas a relative path would be much less work. The lesson here is to know both methods because one is going to be easier than the other most of the time.

Managing the Filesystem

Now that you know how to move from one directory to another, you will want to see what is inside the directories. The ls command lists files in a directory:

julia@mintos:~ > cd /etc/sound/events
julia@mintos:/etc/sound/events > ls
mate-battstat_applet.soundlist

By default the ls command displays all files in the current directory except hidden files. A hidden file has a . character at the beginning of the filename. To see all files, including hidden files, use the -a option to the ls command:

julia@mintos:/etc/sound/events > ls -a
. .. mate-battstat_applet.soundlist

Recall that . represents the current directory and .. represents the directory above the current directories. You will always see these two hidden files3 regardless of which directory you are in.

To understand why some files are hidden, look at the files in a typical user’s home directory:

julia@mintos:/etc/sound/events > ls -a ~
.   .bash_history  .bashrc  .config     .gimp-2.8  .local    .profile
..  .bash_logout   .cache   .face.icon  .kde       .mozilla

Each of the hidden files (in some cases they are directories) that you see in this output contain information that modifies the user’s environment. For example, .bashrc and .profile modify how the BASH shell works for the current user. The .mozilla directory contains configuration settings for Firefox, a web browser provided by the Mozilla Foundation.

How could you tell if .bashrc was a file or .mozilla was a directory? Use the -l option:

julia@mintos:/etc/sound/events > ls -a -l ~
total 48
drwxr-xr-x 8 julia julia 4096 Jul 14 17:26 .
drwxr-xr-x 5 root  root  4096 Jul 14 17:26 ..
-rw------- 1 julia julia   72 Jul 14 17:26 .bash_history
-rw-r--r-- 1 julia julia  220 Apr  8  2014 .bash_logout
-rw-r--r-- 1 julia julia 1452 Jan  5  2016 .bashrc
drwx------ 3 julia julia 4096 Jul 14 17:12 .cache
drwxr-xr-x 6 julia julia 4096 Jun  5 15:59 .config
lrwxrwxrwx 1 julia julia    5 Jun  5 14:40 .face.icon -> .face
drwxr-xr-x 2 julia julia 4096 Jan  5  2016 .gimp-2.8
drwxr-xr-x 3 julia julia 4096 Jan  5  2016 .kde
drwxr-xr-x 3 julia julia 4096 Jan  5  2016 .local
drwxr-xr-x 3 julia julia 4096 Jan  5  2016 .mozilla
-rw-r--r-- 1 julia julia  675 Apr  8  2014 .profile

When you use the -l option, each line describes detailed information for a file. See Figure 3.4 for a demonstration of the details that are provided.

Figure 3.4 Details of the ls -l command

The details of the information provided by the ls -l command:

File type—d means this is a directory whereas - means it is a plain file. There are additional file types, but these are the primary two you should know.4

Permissions—These are used to allow or disallow access to the file. Chapter 4, “Essential Commands” covers permissions.

Hard link count—A more advanced topic normally reserved for system administrators, this is a file that is hard linked and shares the same data block space with another filename.

User owner—The user who owns the file has special access to the file. For example, only the user owner (and the root user) can change the permissions of a file.

Group owner—Group owners have special access to the file via permissions.

File size—The size of the file in bytes.5

Modification timestamp—The date and time the file was last modified.6

Filename—The name of the file.


Note

You can use many other options with the ls command. Recall the suggestion from Chapter 2, “Introduction to Linux,” regarding viewing the man page for each new command that you learn. This would be an excellent time to make use of that suggestion!


Managing Directories

To create a new directory, use the mkdir command:

julia@mintos:~ > ls
julia@mintos:~ > mkdir data
julia@mintos:~ > ls -l
total 4
drwxrwxr-x 2 julia julia 4096 Jul 15 09:34 data

Note a situation in which this could fail:

julia@mintos:~ > ls
data
julia@mintos:~ > mkdir test/samples
mkdir: cannot create directory 'test/samples': No such file or directory

This failure occurs because to make the samples directory in the test directory, the test directory must exist. To make both the samples and test directory, use the -p option to the mkdir command:

julia@mintos:~ > ls
data
julia@mintos:~ > mkdir -p test/samples
julia@mintos:~ > ls
data test
julia@mintos:~ > ls test
samples

To delete a directory that is empty, use the rmdir command:

julia@mintos:~ > ls
data test
julia@mintos:~ > rmdir data
julia@mintos:~ > ls
test

The rmdir command only works on empty directories:

julia@mintos:~ > ls
test
julia@mintos:~ > rmdir test
rmdir: failed to remove 'test': Directory not empty

To delete an entire directory structure, including all the files and subdirectories, use the rm command with the -r option:

julia@mintos:~ > ls
test
julia@mintos:~ > rm -r test
julia@mintos:~ > ls
julia@mintos:~ >


Note

The rm command is normally used to delete files. With the -r option, it can delete directories and files.



Be careful when using the rm -r command. You could accidentally delete files that you really need to keep. Consider using the -i option when executing the rm -r command because this enables you to pick which files to delete. When prompted, answer y for yes and n for no:7

julia@mintos:~ > rm -ri events
rm: descend into directory 'events'? y
rm: remove regular file 'events/mate-battstat_applet.soundlist'? y
rm: remove directory 'events'? n

Managing Files

To copy a file, use the cp command. You should have two arguments: what file to copy and where to copy it:

julia@mintos:~ > ls
events
julia@mintos:~ > cp /etc/hosts.
julia@mintos:~ > ls
events hosts

Recall that . represents the current directory.

Be careful when using the cp command because you can accidentally overwrite an existing file. This happens when the destination (where to copy the file) contains a file with the exact same name as an existing file:

ulia@mintos:~ > ls -l hosts
-rw-r--r-- 1 julia julia 221 Jul 15 10:47 hosts
julia@mintos:~ > cp /etc/passwd hosts
julia@mintos:~ > ls -l hosts
-rw-r--r-- 1 julia julia 2074 Jul 15 10:52 hosts

You can tell that the original file was overwritten because the file size changed (221 bytes to 2074 bytes) and the modification timestamp changed. To avoid overwriting an existing file, use the -i option:

julia@mintos:~ > ls -l hosts
-rw-r--r-- 1 julia julia 221 Jul 15 10:56 hosts
julia@mintos:~ > cp -i /etc/passwd hosts
cp: overwrite 'hosts'? n
julia@mintos:~ > ls -l hosts
-rw-r--r-- 1 julia julia 221 Jul 15 10:56 hosts

The -i option stands for “interactive” mode and prompts you if the cp command will end up overwriting an existing file.

Some additional useful commands to manage files include the following:

mv—Used to move files or directories

rm—Used to delete files

touch—Creates an empty file or updates the modification timestamp of an existing file

Wildcards

Suppose you want to copy all the files that end in .conf from the /etc directory to a directory called config in your home directory. You look in the /etc directory and realize that there are about 20 of these files. You don’t want to have to type in the name of each file. This is a case in which you want to use wildcards.

With wildcards, you can make use of special characters to match file or directory names. For example, you can list all the files in the /etc directory that end in .conf by executing the following command:8

julia@mintos:~ > ls -d /etc/*.conf
/etc/adduser.conf          /etc/insserv.conf     /etc/pam.conf
/etc/apg.conf              /etc/inxi.conf        /etc/pnm2ppa.conf
/etc/avserver.conf         /etc/kernel-img.conf  /etc/request-key.conf
/etc/blkid.conf            /etc/kerneloops.conf  /etc/resolv.conf
/etc/brltty.conf           /etc/ld.so.conf       /etc/rsyslog.conf
/etc/ca-certificates.conf  /etc/libao.conf       /etc/sensors3.conf
/etc/casper.conf           /etc/libaudit.conf    /etc/sysctl.conf
/etc/colord.conf           /etc/logrotate.conf   /etc/ts.conf
/etc/debconf.conf          /etc/ltrace.conf      /etc/ucf.conf
/etc/deluser.conf          /etc/mke2fs.conf      /etc/uniconf.conf
/etc/fuse.conf             /etc/mtools.conf      /etc/updatedb.conf
/etc/gai.conf              /etc/netscsid.conf    /etc/usb_modeswitch.conf
/etc/hdparm.conf           /etc/nsswitch.conf    /etc/wodim.conf
/etc/host.conf             /etc/ntp.conf         /etc/wvdial.conf

The * character represents “zero or more characters in a filename.” So, you are asking to see files in the /etc directory that begin with “zero or more characters, followed by .conf.” Using the * character, you can copy these files into a directory under your home directory (don’t worry if you get an error message for some of the files):1

julia@mintos:~ > mkdir config
julia@mintos:~ > cp /etc/*.conf config
cp: cannot open '/etc/fuse.conf' for reading: Permission denied
cp: cannot open '/etc/wvdial.conf' for reading: Permission denied
julia@mintos:~ > ls config
adduser.conf          gai.conf         logrotate.conf    resolv.conf
apg.conf              hdparm.conf      ltrace.conf       rsyslog.conf
avserver.conf         host.conf        mke2fs.conf       sensors3.conf
blkid.conf            insserv.conf     mtools.conf       sysctl.conf
brltty.conf           inxi.conf        netscsid.conf     ts.conf
ca-certificates.conf  kernel-img.conf  nsswitch.conf     ucf.conf
casper.conf           kerneloops.conf  ntp.conf          uniconf.conf
colord.conf           ld.so.conf       pam.conf          updatedb.conf
debconf.conf          libao.conf       pnm2ppa.conf      usb_modeswitch.conf
deluser.conf          libaudit.conf    request-key.conf  wodim.conf

Use the ? character to represent a single character. So, to display any files in the /etc directory that have filenames that are exactly four characters in length, execute the following command:

julia@mintos:~ > ls -d /etc/????
/etc/acpi  /etc/dkms  /etc/init  /etc/mono  /etc/perl  /etc/udev
/etc/cups  /etc/dpkg  /etc/kde4  /etc/mtab  /etc/sgml  /etc/xrdb
/etc/dhcp  /etc/gimp  /etc/ldap  /etc/newt  /etc/skel

The ? character will match any single character. If you want to match a specific character, you can use a set of square brackets, []. For example, to match a file in the /etc directory that begins with an a, b, or c character, execute the following command:

julia@mintos:~ > ls -d /etc/[abc]*
/etc/acpi          /etc/avserver.conf           /etc/chatscripts
/etc/adduser.conf  /etc/bash.bashrc             /etc/chromium-browser
/etc/adjtime       /etc/bash_completion         /etc/colord.conf
/etc/akonadi       /etc/bash_completion.d       /etc/compizconfig
/etc/alternatives  /etc/bindresvport.blacklist  /etc/console
/etc/anacrontab    /etc/blkid.conf              /etc/console-setup
/etc/apg.conf      /etc/blkid.tab               /etc/cracklib
/etc/apm           /etc/bluetooth               /etc/cron.d
/etc/apparmor      /etc/brlapi.key              /etc/cron.daily
/etc/apparmor.d    /etc/brltty                  /etc/cron.hourly
/etc/apport        /etc/brltty.conf             /etc/cron.monthly
/etc/apt           /etc/ca-certificates         /etc/crontab
/etc/at.deny       /etc/ca-certificates.conf    /etc/cron.weekly
/etc/at-spi2       /etc/calendar                /etc/cups
/etc/avahi         /etc/casper.conf             /etc/cupshelpers


Using a Range in [ ]

Note that [abc] is the same as [a-c]. Using a - enables you to specify a range of permitted characters. Just be sure it is a valid range according to the ASCII text table. You can view this table by executing the command man ascii.


The wildcard characters are not specific to any particular command. Instead, they are part of the BASH shell. This is important to note because it means you can use wildcard characters with any command. The BASH shell interprets the wildcard characters before the command runs.

In fact, the command doesn’t even know that wildcard characters are being used. For example, consider the following command:

julia@mintos:~ > ls -d /etc/[xyz]*
/etc/xdg  /etc/xemacs21  /etc/xml  /etc/xrdb  /etc/zsh

The argument /etc/[xyz]* isn’t passed into the ls command. BASH first converts the wildcard pattern into the filename(s) that match. So, if you execute the ls -d /etc/[xyz]* command, the command that is really executed is the ls -d /etc/xdg /etc/xemacs21 /etc/xml /etc/xrdb /etc/zsh command.

This is also why when you use wildcards with the ls command, you should use the -d option. When the ls command runs and is passed a directory name as an argument, the contents of the directory are listed:

julia@mintos:~ > ls /etc/xdg
autostart  menus  Trolltech.conf  user-dirs.conf  user-dirs.defaults

When you use wildcard characters, some of the matching files could actually be directories. This produces output that you likely don’t want to receive:

julia@mintos:~ > ls /etc/[xyz]*
/etc/zsh

/etc/xdg:
autostart  menus  Trolltech.conf  user-dirs.conf  user-dirs.defaults

/etc/xemacs21:
site-start.d

/etc/xml:
catalog          docbook-xml.xml.old  rarian-compat.xml  xml-core.xml
catalog.old      docbook-xsl.xml      sgml-data.xml      xml-core.xml.old
docbook-xml.xml  docbook-xsl.xml.old  sgml-data.xml.old

/etc/xrdb:
Editres.ad Emacs.ad General.ad Motif.ad Tk.ad Xaw.ad

To avoid having the contents of all of these directories, use the -d option to the ls command. The -d option tells the ls command “if any argument happens to be a directory, don’t display what is in that directory, just show the directory name.”

Redirection

Suppose you run a command and you decide that you want to store the output of the command into a file for future use. In a situation like this, you can use a process called redirection. The idea is to redirect the output of a command into a file or another process. You can also redirect the input to a command from a file.

Each command has three data streams:

Standard input (stdin)—The data that is sent into the command. This is not an argument but rather additional information that is being sent into a command. Typically, this data comes from a user who is executing the command. The user provides this data via the keyboard. This input could be redirected from a file or another process.

Standard output (stdout)—The data that is sent from the command when all goes well. Typically, this appears on the screen, but it can be sent to a file or to another command.

Standard error (stderr)—The data that is sent from the command when an error occurs. Typically, this appears on the screen, but it can be sent to a file or to another command.

To redirect stdout, use the > character after the command:

julia@mintos:~ > cal 12 2000 > mycal
julia@mintos:~ > cat mycal
   December 2000
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31


Note

You use the cat command to display small files; it is covered in Chapter 4.


You use the > character when you want to create a new file or overwrite the contents of an existing file. If you want to append to an existing file, use two > characters:

julia@mintos:~ > cat mycal
   December 2000
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
julia@mintos:~ > date >> mycal
julia@mintos:~ > cat mycal
   December 2000
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Sun Jul 17 07:24:51 PDT 2016

The output of the cal and date commands in the previous examples is considered standard output because the command ran successfully. Notice that if a command fails for some reason (such as improper options or arguments), the command’s output is not redirected to a file when you use the > or >> characters:

julia@mintos:~ > cal -5 12 2000 > mycal
cal: invalid option -- '5'
Usage: cal [general options] [-hjy] [[month] year]
       cal [general options] [-hj] [-m month] [year]
       ncal [general options] [-bhJjpwySM] [-s country_code] [[month] year]
       ncal [general options] [-bhJeoSM] [year]
General options: [-NC31] [-A months] [-B months]
For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]
julia@mintos:~ > cat mycal
julia@mintos:~ >

When an error occurs, the command sends output to stderr. You can redirect this output by using the 2> characters:10

julia@mintos:~ > cal -5 12 2000 > mycal 2> error
julia@mintos:~ > cat error
cal: invalid option -- '5'
Usage: cal [general options] [-hjy] [[month] year]
       cal [general options] [-hj] [-m month] [year]
       ncal [general options] [-bhJjpwySM] [-s country_code] [[month] year]
       ncal [general options] [-bhJeoSM] [year]
General options: [-NC31] [-A months] [-B months]
For debug the highlighting: [-H yyyy-mm-dd] [-d yyyy-mm]

 

Important notes regarding redirecting stderr include the following:

The 2> characters either create a new file or overwrite the contents of an existing file. To append stderr messages to an existing file, use 2>>.

To send all output, both stdout and stderr, to a single file, use the following:11

cmd > file 2>&1

Sometimes you will want to run a command without seeing any of the error messages. To discard the output of a command, send it to the file. This file is called the “bit bucket” or “black hole” because whatever you send to the file will be discarded.

Redirecting stdout and stderr is a fairly common practice. Conversely, redirecting stdin (standard input) is much rarer. Before demonstrating redirecting stdin, consider the following exercise, which demonstrates where stdin comes from by default. Start by executing the following command:

julia@mintos:~ > tr 'a-z' 'A-Z'

It looks like this program hangs, but it is just waiting for stdin. You can provide stdin from the keyboard. For example, type a sentence and then press the Enter key:

julia@mintos:~ > tr 'a-z' 'A-Z'
today is a good day to learn linux
TODAY IS A GOOD DAY TO LEARN LINUX

You can see what the tr command does with the input. It translates all lowercase letters to uppercase letters. Clearly, performing this task on a file, not input from the keyboard, would be much more useful. Unfortunately, the tr command doesn’t allow you to provide a filename (By the way, to stop the current tr command, hold down the Ctrl key and press the C key. This is normally written as Ctrl+C or just ^C):

julia@mintos:~ > tr 'a-z' 'A-Z' mycal
tr: extra operand 'mycal'
Try 'tr --help' for more information.

The tr command only accepts input from stdin. So, you need to tell the BASH shell to pull stdin data from a file rather than from the keyboard. To do this, use the < character:

julia@mintos:~ > tr 'a-z' 'A-Z' < mycal
   DECEMBER 2000
SU MO TU WE TH FR SA
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

Note that the output of the tr command is sent to the screen via stdout. Essentially, it is now lost, but you can also run the command and redirect stdout to a file (just make sure it is a different file than the original):

julia@mintos:~ > tr 'a-z' 'A-Z' < mycal > mynewcal
julia@mintos:~ > cat mynewcal
   DECEMBER 2000
SU MO TU WE TH FR SA
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

Most commands that require input accept a file as an argument, so you won’t redirect stdin nearly as often as you will redirect stdout or stderr. However, some advanced scenarios exist in which knowing how to redirect stdin is very useful.

You can also redirect stdout into another command instead of a file. This is useful because many of the commands on the system perform filtering or paging functions.12

For example, execute the following command: ls -l /etc. Typically, the /etc directory contains hundreds of files, so the output of this ls command will quickly scroll by on the screen. This can make viewing large amounts of information difficult. The solution is to send the output of the ls command into another command that will display one page of data at a time: ls -l /etc | more.

The | character is used to redirect stdout to another command. It becomes the stdin of the command to the right of the | character, in this case the more command, which displays data one page at a time. Chapter 4 covers the more command in greater detail. For now, use the spacebar to scroll one page at a time and the q character to end displaying the output and return to a prompt.

This process, called piping, is useful when you work in a command-line environment. Understanding it might be a bit difficult if this is your first exposure, so a couple of diagrams might be helpful. First consider Figure 3.5, which shows how the ls command functions when piping is not used.

Figure 3.5 The ls -l command without piping

Now compare this to Figure 3.6 in which the stdout from the ls command is piped into the more command.

Figure 3.6 The ls -l command with piping

Notice in Figure 3.6 that only stdout is redirected when using piping. The command’s stderr will still be displayed on the screen directly.


Linux Humor

There is no place like ~


Summary

In this chapter you learned how to manage the Linux filesystem, including how to handle files and directories. The concept of using wildcards to match file and directory names was introduced. In addition, you learned how to redirect the output and input of commands into either files or other commands.

1 I realize this part might be a bit confusing because there are really three “roots” in Linux. The /directory is called the root directory because it is the start of the filesystem. The system administrator is called the root user and the home directory for the root user is the /root directory. Linux users often refer to the root directory as the “slash” directory to avoid confusion with /root and the root user.

2 pwd stands for print working directory. In early Unix times, monitors were rare, so output was often sent to an actual printer. Because Unix is the precursor to Linux, many of the commands are named the same.

3 You may be wondering if I accidently called . and .. a file. In Linux, everything is considered a file, including directories. A directory is just a special file that holds other directories. Normally I would refer to these as directories, but I wanted to take the opportunity to make this point as it could prove important as you dive deeper into Linux.

4 You may also want to know about l, which stands for symbolic link. A symbolic link is a file that points to another file. If you have experience in Windows, symbolic links are akin to shortcuts on your desktop.

5 File sizes in bytes can be difficult to comprehend, especially for large files. Consider including the -h option to show file sizes in “human readable” sizes.

6 If the file was last modified within the past six months, the timestamp includes a month, day, and time. If the file was last modified more than six months ago, the time is replaced with the year.

7 The rm-ri command is the same as the rm -ir command, which is the same as the rm -r -i command. In most cases, single character options can be combined and order doesn’t matter.

8 You may be wondering why I used the -d option with the ls command. Be patient; I will explain this soon.

9 The cp command in this example generates some error messages because of file permission issues. Chapter 4 covers file permissions. For now, don’t worry if your command generates a few error messages like these.

10 It might seem strange that you use > to redirect stdout and 2> to redirect stderr. However, the official way you redirect stdout is by using the 1> characters. Because stdout is more commonly redirected than stderr, the BASH shell permits you to drop the 1 before the > character.

11 You can also use the following syntax in the BASH shell: cmd &> file

12 Paging is displaying one page or screen of data at a time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.43.216