1.4 Using Directory Commands

1.4 Using Directory Commands

Unix has a directory hierarchy that starts at /, sometimes called the root. The directory separator is the slash (/), not the backslash (\). There are several standard subdirectories in the root directory, such as /usr (you'll learn all about them in Section 2.1).

A directory specification is called a path, and one that starts at the root (such as /usr/lib) is a full or absolute path. Similarly, a filename with a full path in front (such as /usr/lib/libc.a) is a full pathname.

The path component identified by two dots (..) specifies the parent of your shell's current directory, and one dot (.) specifies the current directory. For example, if the current working directory of your shell is /usr/lib, the path ../bin refers to /usr/bin. A path beginning with .. or . is called a relative pathname.

The following sections describe the essential directory commands.

1.4.1 cd

The cd command changes the shell's current working directory to dir:

cd dir

If you omit dir, the shell returns to your home directory.

1.4.2 mkdir

The mkdir command creates a new directory, dir:

mkdir dir

1.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 -rf 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, and -f forces the delete operation. Don't use the -rf flags with wildcards such as a star (*). Above all, always double-check your command.

1.4.4 Shell Wildcards

The shell is capable of matching simple patterns with files in the current working directory. The simplest of these is the star character (*), which means match any number of arbitrary characters. For example, the following command prints a list of files in the current directory:

echo *

After matching files to wildcards, the shell substitutes the filenames for the wildcard in the command line and then runs the revised command line. Here are some more wildcard examples: at* matches all files starting with at; *at matches files that end with at; and *at* matches any files that contains at. If no files match a wildcard, the shell does no substitution, and the command runs with literal characters such as * (for example, try a command such as echo *dfkdsafh).

If you're used to MS-DOS, you might instinctively type *.* as a wildcard 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 wildcard character is the question mark (?), instructing the shell to match exactly one arbitrary character. For example, b?at matches boat and brat.

If you do not want the shell to expand a wildcard in a command, enclose the wildcard in single quotes (''). For example, the command echo '*' prints a star. You will find this handy in a few of the commands described in the next section, such as grep and find. Quoting is a somewhat tricky matter, so don't get too involved with it just yet — you'll learn more much later, in Section 7.2.

This isn't the end to a modern shell's pattern-matching capabilities, but * and ? are what you need to know.