Navigating the Linux File System
In the Linux file system, as with its predecessor UNIX, everything is a file: data files, binary files, executable programs, even input and output devices. These files are placed in a series of directories that act like file folders. A directory is nothing more than a special type of file that contains a list of other files/directories. These files and directories are used to create a hierarchical structure that enables logical placement of specific types of files. Later this chapter discusses the standard hierarchy of the Linux file system. First, you learn how to navigate and interact with the file system.
Listing the Contents of a Directory with ls
The ls command lists the contents of the current directory. It is commonly used by itself, but a number of options (also known as switches) are available for ls and give you more information. If you have just logged in as described earlier, this command lists the files and directories in your user’s home directory:
matthew@seymour:~$ ls Documents Music file.txt Pictures Music
By itself, the ls command shows just a list of names. Some are files, some are directories. This is useful if I know what I am looking for but cannot remember the exact name. However, using ls in this matter has some limitations. First, it does not show hidden files. Hidden files use filenames that start with a period (.) as the first character. They are often used for configuration of specific programs and are not accessed frequently. For this reason, they are not included in a basic directory listing. You can see all the hidden files by adding a switch to the command like this:
matthew@seymour:~$ ls -a . .bash_logout Documents Music .. .bashrc file.txt Pictures .bash_history .config .local .profile
There is still more information available about each item in a directory. To include details such as the file/directory permissions, owner and group (all of which are discussed later in this chapter), as well as the size, and the date and time it was last modified, enter the following:
matthew@seymour:~$ ls -al total 608 drwxr-xr-x 38 matthew matthew 4096 2011-06-04 08:20 . drwxr-xr-x 3 root root 4096 2011-05-16 16:48 .. -rw------- 1 matthew matthew 421 2011-06-04 10:27 .bash_history -rw-r--r-- 1 matthew matthew 220 2011-05-16 16:48 .bash_logout -rw-r--r-- 1 matthew matthew 3353 2011-05-16 16:48 .bashrc drwxr-xr-x 13 matthew matthew 4096 2011-05-21 10:42 .config drwxr-xr-x 2 matthew matthew 4096 2011-05-16 17:07 Documents -rw-r--r-- 1 matthew matthew 335 2011-05-16 16:48 file.txt drwxr-xr-x 3 matthew matthew 4096 2011-05-16 17:07 .local drwxr-xr-x 2 matthew matthew 4096 2011-05-16 17:07 Music drwxr-xr-x 3 matthew matthew 4096 2011-05-16 18:07 Pictures -rw-r--r-- 1 matthew matthew 675 2011-05-16 16:48.profile
The listing (abbreviated here) is now given with one item per line, but with multiple columns. The listing starts with the number of items in the directory. (Both files and subdirectories are included; remember that the listing here is abbreviated.) Then, the details are as shown in Figure 9.1.
Figure 9.1. Decoding the output of a detailed directory listing.
These details are discussed more completely later in the chapter in the “Working with Permissions” section.
Another useful switch is this:
matthew@seymour:~$ ls -R
This command scans and lists all the contents of the subdirectories of the current directory. This is likely to be a lot of information, so you might want to redirect the output to a text file so that you can browse through it at your leisure by using the following:
matthew@seymour:~$ ls -laR > listing.txt
Changing Directories with cd
Use the cd command to move within the file system from one directory to another. It might help you remember this command to think of it meaning “change directory.” The most basic usage of cd is this:
matthew@seymour:~$ cd somedir
That looks in the current directory for the somedir subdirectory, and then moves you into it. You can also specify an exact location for a directory, like this:
matthew@seymour:~$ cd /home/matthew/stuff/somedir
You can also use the cd command with several shortcuts. For example, to quickly move up to the parent directory, the one above the one you are currently in, use the cd command like this:
matthew@seymour:~$ cd ..
To return to your home directory from anywhere in the Linux file system, use the cd command like this:
You can also use the $HOME shell environment variable to accomplish the same thing. Environment variables are discussed in greater detail in Chapter 10, “Command-Line Master Class.” Type this command and press Enter to return to your home directory:
matthew@seymour:~$ cd $HOME
You can accomplish the same thing by using the tilde (~) like this:
matthew@seymour:~$ cd ~
Finding Your Current Directory with pwd
Use pwd to remind you where you are within the file system.