Navigating the File System with Linux Commands


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.

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).

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 ...

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:

  1. Type cd to change to your home directory.

  2. Type ls -F to see the files and directories in your home directory.

  3. 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.