Navigating the File System with Linux Commands
Although graphical shells such as Nautilus are easy to use, you can use them only if you have a graphical login. Sometimes, you may not have a graphical environment to run a graphical file manager. For example, you might be logged in through a text terminal, or X might not be working on your system. In those situations, you have to rely on Linux commands to work with files and directories. Of course, you can always use Linux commands, even in the graphical environment-all you have to do is open a terminal window and type the Linux commands. Click the terminal icon in the GNOME Panel (refer to Figure 7-2) to start a terminal emulation program that displays a terminal window.
Using Directory Navigation Commands
In Red Hat Linux, when you log in as root , your home directory is /root . For other users, the home directory is usually in the /home directory. My home directory (when I log in as naba) is /home/naba . This information is stored in the /etc/passwd file. By default, only you (and root , of course) have permission to save files in your home directory, and only you can create subdirectories in your home directory to further organize your files.Linux supports the concept of a current directory, which is the directory on which all file and directory commands operate. After you log in, for example, your current directory is the home directory. To see the current directory, type the pwd command.To change the current directory, use the cd command. To change the current directory to /usr/share/doc , type the following:
cd /usr/share/doc
Then, to change the directory to the bash-2.05b subdirectory in /usr/share/doc , type this command:
cd bash-2.05b
Now, if you use the pwd command, that command shows /usr/share/doc/bash-2.05b as the current directory. Therefore, you can refer to a directory's name in two ways:
An absolute pathname (such as /usr/share/doc ), which specifies the exact directory in the directory tree
A relative directory name (such as bash-2.05b , which represents the bash-2.05b subdirectory of the current directory)
If you type cd bash-2.05b in /usr/share/doc , the current directory changes to /usr/share/doc/bash-2.05b; the same command in /home/naba tries to change the current directory to /home/naba/bash-2.05b .
Use the cd command without any arguments to change the current directory to your home directory. Actually, the lone cd command changes the current directory to the directory listed in the HOME environment variable; that environment variable contains your home directory by default.Notice that the tilde character (~ ) also refers to the directory the HOME environment variable specifies. Thus, the command cd ~ changes the current directory to whatever directory the HOME environment variable specifies.You can use a shortcut to refer to any user's home directory. Prefix a user's login name with a tilde (~ ) to refer to that user's home directory. Therefore, ~naba refers to the home directory of the user naba, and ~root refers to the home directory of the root user. If your system has a user with the login name emily, you can type cd ~emily to change to Emily's home directory.The directory names . and .. have special meanings. A single period (. ) indicates the current directory, whereas two periods (.. ) indicate the parent directory. If the current directory is /usr/share/doc , for example, you can change the current directory to /usr by typing the cd .. command (if you are a DOS/Windows user, note that there is a space between cd and the two periods). Essentially, this command takes you up one level in the file-system hierarchy.
Showing the Current Directory in the Shell Prompt
Note that you can display the current directory in the shell prompt. The Bash shell uses the value of the environment variable PS1 as the primary prompt. Another variable, PS2, functions as the secondary prompt when a command requires further input from the user. You can view these variables with the echo command (for example, type echo $PS1 to view the setting of PS1).
By default, the PS1 environment variable is defined in Red Hat Linux as follows:
echo $PS1
[\u@\h \W]\$
With this setting for PS1, the prompt looks like this:
[username@hostname dirname]$
In this example, username is the login name of the user; hostname is the system's name (Excluding the domain name), and dirname is the last part of the current working directory. Thus, if the current directory is /usr/src/linux , the dirname is linux . Thus, when a usernamed joe on a system with hostname mycompany.com changes the directory to /usr/share/doc , the shell prompt appears as follows:
[joe@mycompany doc]$
For the root user, the $ sign changes to a number sign (# ) and the prompt looks like this:
[root@mycompany doc]#
Table 7-5 shows the character sequences you can use in the PS1 environment variable to customize your prompt.
Code | What Appears in Prompt |
---|---|
\t | Current time in HH:MM:SS format |
\d | Date in 'Weekday Month Date' format, such as 'Sun Oct 26' |
\n | Newline |
\s | Name of the shell, such as bash |
\w | Full name of the current working directory, such as /usr/src/linux |
\W | Basename of the current working directory, such as linux for /usr/src/linux |
\u | User name of the current user |
\h | Hostname |
\# | Command number of this command |
\! | History number of this command |
\$ | # if the effective user ID is 0 (indicating the user is root); otherwise, a $ |
\ nnn | Character corresponding to the octal number nnn |
\\ | A backslash |
\[ | Begins a sequence of nonprinting characters, which could be used to embed a terminal control sequence into the prompt |
\] | Ends a sequence of nonprinting characters |
Given this information, you can show the full directory name (enclosed in square brackets) in the prompt by using the following command:
export PS1="[\w]"
If the current directory is /usr/src/linux-2.4 , the prompt appears as follows:
[/usr/src/linux-2.4]
Interpreting Directory Listings and Permissions
As you move around the Linux directories, you may want to check the contents of a directory. You can get a directory listing by using the ls command. By default, the ls command-without any options-displays the contents of the current directory in a compact, multicolumn format. For example, type the following commands to see the contents of the /etc/X11 directory. (Type the commands shown in boldface; I have omitted the command prompts from the listing):
cd /etc/X11
ls
applnk prefdm sysconfig XftConfig.README-OBSOLETE xserver
desktop-menus proxymngr twm xinit xsm
fs rstart X xkb
gdm serverconfig xdm Xmodmap
lbxproxy starthere XF86Config Xresources
From this listing, you cannot tell whether an entry is a file or a directory. To tell the directories and files apart, use the -F option with ls , as follows:
ls -F
applnk/ prefdm* sysconfig/ XftConfig.README-OBSOLETE xserver/
desktop-menus/ proxymngr/ twm/ xinit/ xsm/
fs/ rstart/ X@ xkb@
gdm/ serverconfig/ xdm/ Xmodmap
lbxproxy/ starthere/ XF86Config Xresources
Now, the directory names have a slash (/ ) appended to them. Plain filenames appear as is. The at sign (@ ) appended to the first filename indicates that that file is a link to another file (in other words, this filename simply refers to another file). An asterisk (* ) is appended to executable files (see, for example, the prefdm file in the listing).You can see even more detailed information about the files and directories with the -l option:
ls -l
For the /etc/X11 directory, a typical output from ls -l looks like the following:
total 80
drwxr-xr-x 5 root root 4096 Jan 18 21:15 applnk
drwxr-xr-x 2 root root 4096 Jan 18 20:47 desktop-menus
drwxr-xr-x 2 root root 4096 Jan 18 21:36 fs
drwxr-xr-x 6 root root 4096 Jan 18 21:33 gdm
drwxr-xr-x 2 root root 4096 Jan 18 21:31 lbxproxy
-rwxr-xr-x 1 root root 1419 Jan 15 22:38 prefdm
drwxr-xr-x 2 root root 4096 Jan 18 21:31 proxymngr
drwxr-xr-x 4 root root 4096 Jan 18 21:31 rstart
drwxr-xr-x 2 root root 4096 Dec 2 12:03 serverconfig
drwxr-xr-x 2 root root 4096 Dec 2 12:03 starthere
drwxr-xr-x 2 root root 4096 Dec 2 12:03 sysconfig
drwxr-xr-x 2 root root 4096 Jan 18 21:33 twm
lrwxrwxrwx 1 root root 27 Jan 18 21:39
X ->../../usr/X11R6/bin/XFree86
drwxr-xr-x 3 root root 4096 Jan 18 21:32 xdm
-rw-r--r-- 1 root root 3184 Jan 18 21:39 XF86Config
... lines deleted ...
The detailed directory listing displayed by the ls -l command shows considerable information about each directory entry, which can be a file or another directory. A typical line in the detailed directory listing has the following appearance:
drwxr-xr-x 6 root root 4096 Oct 26 21:18 gdm
Looking at a line from the right column to the left, you see that the rightmost column shows the name of the directory entry. The date and time before the name show when the last modifications to that file were made. Before the date and time is the size of the file, in bytes.
The file's group and owner appear to the left of the column that shows the file size. The next number to the left indicates the number of links to the file. (A link is like a shortcut in Windows.) Finally, the leftmost column shows the file's permission settings, which determine who can read, write, or execute the file.The first letter of the leftmost column has a special meaning, as the following list shows:
- means an ordinary file
b means a block special file
c means a character special file
d means a directory
l means a symbolic link to another file
p means a first-in first-out (FIFO) special file (also known as a pipe file)
s means a local socketAfter that first letter, the leftmost column shows a sequence of nine characters, which appears as rwxrwxrwx when each letter is present. Each letter indicates a specific permission; a hyphen (- ) in place of a letter indicates no permission for a specific operation on the file. Think of these nine letters as three groups of three letters (rwx ), interpreted as follows:
The leftmost group of rwx controls the read, write, and execute permission of the file's owner. In other words, if you see rwx in this position, the file's owner can read (r ), write (w ), and execute (x ) the file. A hyphen in the place of a letter indicates no permission. Thus, the string rw- means that the owner has read and write permission but not execute permission. Typically, executable programs (including shell programs) have execute permission. However, for directories, execute permission is equivalent to use permission-a user must have execute permission on a directory to open and read the contents of the directory.
The middle three rwx letters control the read, write, and execute permission of any user belonging to that file's group.
The rightmost group of rwx letters controls the read, write, and execute permission of all other users (collectively referred to as the world).Thus, a file with the permission setting rwx--- is accessible only to the file's owner, whereas the permission setting rwxr-r- makes the file readable by the group and the world.
An interesting feature of the ls command is that it does not list any file whose name begins with a period. To see these files, you must use the ls command with the -a option, as follows:
ls -a
Try this command in your home directory, and compare the result with what you see when you don't use the -a option:
Type cd to change to your home directory.
Type ls -F to see the files and directories in your home directory.
Type ls -aF to see everything, including the hidden files.Insider Insight
Most Linux commands take single-character options, each with a minus sign (think of this sign as a hyphen) as a prefix. When you want to use several options, type a hyphen and concatenate the option letters one after another. Therefore, ls -al is equivalent to ls -a -l . However, if an option requires a value, then you have to first list the option and then its value.