2.4 Navigating Directories
The Unix directory hierarchy starts at /, also called the root directory. The directory separator is the slash (/), not the backslash (\). There are several p. 17 standard subdirectories in the root directory, such as /usr, as you’ll learn in Section 2.19.
When you refer to a file or directory, you specify a path or pathname. When a path starts with / (such as /usr/lib), it’s a full or absolute path.
A path component identified by two dots (..) specifies the parent of a directory. For example, if you’re working in /usr/lib, the path .. would refer to /usr. Similarly, ../bin would refer to /usr/bin.
One dot (.) refers to the current directory; for example, if you’re in /usr/lib, the path . is still /usr/lib, and ./X11 is /usr/lib/X11. You won’t have to use . very often because most commands default to the current directory if a path doesn’t start with / (so you could just use X11 instead of ./X11 in the preceding example).
A path not beginning with / is called a relative path. Most of the time, you’ll work with relative pathnames, because you’ll already be in or near the directory you need.
Now that you have a sense of the basic directory mechanics, here are some essential directory commands.
2.4.1 cd
The current working directory is the directory that a process (such as the shell) is currently in. In addition to the default shell prompt in most Linux distributions, you can see your current directory with the pwd command, described in Section 2.5.3.
Each process can independently set its own current working directory. The cd command changes the shell’s current working directory:
$ cd dir
If you omit dir, the shell returns to your home directory, the directory where you started when you first logged in. Some programs abbreviate your home directory with the ~ symbol (a tilde).
The cd command is a shell built-in. It wouldn’t work as a separate program because if it were to run as a subprocess, it could not (normally) change its parent’s current working directory. This may not seem a particularly important distinction at the moment, but there are times when knowing this fact can clear up confusion.
2.4.2 mkdir
The mkdir command creates a new directory dir:
$ mkdir dir
2.4.3 rmdir
The rmdir command removes the directory dir:
$ rmdir dir
If dir isn’t empty, this command fails. However, if you’re impatient, you probably don’t want to laboriously delete all the files and subdirectories inside dir first. You can use rm -r dir to delete a directory and its contents, but be careful! This is one of the few commands that can do serious damage, especially if you run it as the superuser. The -r option specifies recursive delete to repeatedly delete everything inside dir. Don’t use the -r flag with globs such as a star (*). And above all, always double-check your command before you run it.
2.4.4 Shell Globbing (“Wildcards”)
The shell can match simple patterns to file and directory names, a process known as globbing. This is similar to the concept of wildcards in other systems. The simplest of these is the glob character *, which tells the shell to match any number of arbitrary characters. For example, the following command prints a list of files in the current directory:
$ echo *
The shell matches arguments containing globs to filenames, substitutes the filenames for those arguments, and then runs the revised command line. The substitution is called expansion because the shell substitutes all matching filenames for a simplified expression. Here are some ways to use * to expand filenames:
at*expands to all filenames that start withat.*atexpands to all filenames that end withat.*at*expands to all filenames that containat.
If no files match a glob, the bash shell performs no expansion, and the command runs with literal characters such as *. For example, try a command such as echo *dfkdsafh.
If you’re used to the Windows command prompt, you might instinctively type *.* to match all files. Break this habit now. In Linux and other versions of Unix, you must use * to match all files. In the Unix shell, *.* matches only files and directories that contain the dot (.) character in their names. Unix filenames do not need extensions and often do not carry them.
Another shell glob character, the question mark (?), instructs the shell to match exactly one arbitrary character. For example, b?at matches boat and brat.
If you don’t want the shell to expand a glob in a command, enclose the glob in single quotes (''). For example, the command echo '*' prints a star. You will find this handy for a few of the commands described in the next p. 19 section, such as grep and find. (You’ll learn more much about quoting in Section 11.2.)
It is important to remember that the shell performs expansions before running commands, and only then. Therefore, if a * makes it to a command without expanding, the shell won’t do anything more with it; it’s up to the command to decide what it wants to do.
There is more to the shell’s pattern-matching capabilities, but * and ? are what you need to know now. Section 2.7 describes glob behavior with those funny files that start with a dot.