There are two basic navigational commands for getting around the shell. The cd command lets you navigate between directories. The ls command tells you the contents of a directory (including other directories). But before you run around different Linux directories, the pwd command can tell you where you are. Output from a navigational command depends on the absolute path , which specifies your directory location relative to the top-level root ( / ) directory.
The pwd command (short for present working directory ) is simple. Type it at the command-line interface, and you ll see the absolute path to your current directory. For example:
# pwd /etc/httpd/conf
The output tells you that you re currently in the /etc/httpd/conf directory, which happens to be the default location for Apache configuration files.
The change directory command is known as cd . Those of you familiar with MS-DOS may find a number of similarities between MS-DOS and Linux cd commands. Typical cd commands are shown in Table 6.1.
Note | Linux is case sensitive. Please note that the small capitals in the tables of this chapter represent lowercase letters . |
Command | Result |
---|---|
cd . . | Moves up one directory level. For example, if you re currently in the /home/mj directory, this moves you to the /home directory. |
cd ../. . | Moves up two directory levels. For example, if you re currently in the /etc/rc.d/rc0.d directory, this moves you to the /etc directory. You can move up additional directory levels, up to the root ( / ) directory. |
cd /home/mj | Navigates to the home directory of user mj. |
cd ~ | Navigates to your home directory. Works for any user. |
Note | If you re relatively new to Linux, remember to use the forward slash / , and not the backslash \ , when you cite directory, computer, or even domain names . |
The ls command is versatile. Not only does it allow you to list the files and directories in your current directory, but with the proper options, you can also find the permissions and size of a file. The command allows you to check ownership, differentiate between file types, and sort the result in several ways. You can review some examples of this command in Table 6.2.
Command | Result |
---|---|
ls | Lists in alphabetical order all nonhidden files in the current directory. |
ls -a | Lists all files in the current directory, including hidden files. |
ls -r | Lists in reverse alphabetical order all nonhidden files in the current directory. |
ls -F | Lists all files by type. The character at the end of each file indicates the file type. For example, a forward slash ( / ) represents a directory, an asterisk ( * ) is associated with an executable file, and an "at" ( @ ) represents a linked file. |
ls -i | Lists files with inode numbers . An inode number represents the location of a file on a volume. Two or more files with the same inode number are two different names for the identical file. |
ls -l | Lists all the files in the current directory, including the current directory ( . ) and the parent directory ( .. ). Also lists the size, owner, and permissions associated with each file in what is known as long listing format . |
ls -t | Lists files by the last time they were changed; most recent files are listed first. |
ls -u | Lists files by the last time they were accessed; most recent files are listed first. |
Perhaps the most important command in this series is ls -l , which lists all files in the current directory, including size, owner, and permissions. Figure 6.1 shows an example of the result of this command.
As you can see, the long listing includes the permissions, user owner, group owner, size, modification time, and name of each file in the current directory.
When you describe the location of a file, you specify either the absolute path or the relative path. An absolute path describes the location of a file relative to the root ( / ) directory. For example, you can type the following command to get to the scripts that start a number of Linux daemons:
# cd /etc/rc.d/init.d
The forward slash in front of the first directory makes this the absolute path. You can type this command from anywhere in Linux to get to this directory. Sometimes, you may accidentally type the command without the forward slash:
# cd etc/rc.d/init.d
In this case, Linux looks for these directories under your home directory. For example, if your home directory is /home/mj , this command makes Linux look for the /home/mj/etc/rc.d/init.d directory. Unless you keep a copy of these files deep in your home directory, Linux won t find anything.
Absolute and relative paths apply to other commands as well. For example, you can list the daemons in the /etc/rc.d/init.d directory with the following command:
# ls /etc/rc.d/init.d
However, if you use the relative path, your current directory matters. For example, if the output from the pwd command is /home/mj , the following command won t work unless you have an /home/mj/ etc/rc.d/init.d directory:
# ls etc/rc.d/init.d