This section discusses how to create directories (mkdir), switch between directories (cd), remove directories (rmdir), use pathnames to make your work easier, and move and copy files and directories between directories.
mkdir: Creates a Directory
The mkdir utility creates a directory. The argument (page 921) to mkdir becomes the pathname of the new directory. The following examples develop the directory structure shown in Figure 4-7. In the figure, the directories that are added appear in a lighter shade than the others and are connected by dashes.
Figure 4-7. The file structure developed in the examples
In Figure 4-8, pwd shows that Zach is working in his home directory (/Users/zach) and ls shows the names of the files in his home directory: demo, names, and temp. Using mkdir, Zach creates a directory named literature as a child of his home directory. He uses a relative pathname (a simple filename) because he wants the literature directory to be a child of the working directory. Of course, Zach could have used an absolute pathname to create the same directory: mkdir/Users/zach/literature.
Figure 4-8. The mkdir utility
$ pwd /Users/zach $ ls demo names temp $ mkdir literature $ ls -F demo literature/ names temp $ ls literature $
The second ls in Figure 4-8 verifies the presence of the new directory. The F option to ls displays a slash after the name of each directory and an asterisk after each executable file (shell script, utility, or application). When you call it with an argument that is the name of a directory, ls lists the contents of the directory. The final ls does not display anything because there are no files in the literature directory.
The following commands show two ways to create the promo directory as a child of the newly created literature directory. The first way checks that /Users/zach is the working directory and uses a relative pathname:
$ pwd /Users/zach $ mkdir literature/promo
The second way uses an absolute pathname:
$ mkdir /Users/zach/literature/promo
Use the p (parents) option to mkdir to create both the literature and promo directories with one command:
$ pwd /Users/zach $ ls demo names temp $ mkdir p literature/promo
$ mkdir p /Users/zach/literature/promo
cd: Changes to Another Working Directory
The cd (change directory) utility makes another directory the working directory but does not change the contents of the working directory. Figure 4-9 shows two ways to make the /Users/zach/literature directory the working directory, as verified by pwd.
Figure 4-9. cd changes your working directory
$ cd /Users/zach/literature $ pwd /Users/zach/literature $ cd $ pwd /Users/zach $ cd literature $ pwd /Users/zach/literature
First Zach uses cd with an absolute pathname to make literature his working directory. It does not matter which is your working directory when you give a command with an absolute pathname. A pwd command confirms the change. When used without an argument, cd makes your home directory the working directory, as it was when you first logged in. The second cd command in Figure 4-9 does not have an argument and makes Zach's home directory the working directory. Finally, knowing that he is working in his home directory, Zach uses a simple filename to make the literature directory his working directory (cd literature) and confirms the change with pwd.
The . and .. Directory Entries
The mkdir utility automatically puts two entries in each directory you create: a single period (.) and a double period (..). The . is synonymous with the pathname of the working directory and can be used in its place; the .. is synonymous with the pathname of the parent of the working directory. These entries are invisible because each of their filenames begins with a period.
Tip: The working directory versus your home directory
The working directory is not the same as your home directory. Your home directory remains the same for the duration of your session and usually from session to session. Each time immediately after you log in, you are working in the same directory: your home directory.
Unlike your home directory, your working directory can change as often as you like. You have no set working directory, which explains why some people refer to it as the current directory. When you log in and until you change directories by using cd, your home directory is your working directory. If you were to change directories to Max's home directory, then Max's home directory would be your working directory.
With the literature directory as the working directory, the following example uses .. three times: first to list the contents of the parent directory (/Users/zach), second to copy the memoA file to the parent directory, and third to list the contents of the parent directory again.
$ pwd /Users/zach/literature $ ls memoA promo $ ls .. demo literature names temp $ cp memoA .. $ ls .. demo literature memoA names temp
After using cd to make promo his working directory, Zach can use a relative pathname to call vim (page 147) to edit a file in his home directory:
$ cd promo $ vim ../../names
You can use an absolute or relative pathname or a simple filename virtually anywhere that a utility or program requires a filename or pathname. This usage holds true for ls, vim, mkdir, rm, and most other Mac OS X utilities.
rmdir: Deletes a Directory
The rmdir (remove directory) utility deletes a directory. You cannot delete the working directory or a directory that contains files other than the . and .. entries. If you need to delete a directory that has files in it, first use rm to delete the files and then delete the directory. You do not have to (nor can you) delete the . and .. entries; rmdir removes them automatically. The following command deletes the promo directory:
$ rmdir /Users/zach/literature/promo
The rm utility has a r option (rm r filename) that recursively deletes files, including directories, within a directory and also deletes the directory itself.
Caution: Use rm r carefully, if at all
Although rm r is a handy command, you must use it carefully. Do not use it with an ambiguous file reference such as *. It is frighteningly easy to wipe out your entire home directory with a single short command.
Use a text editor to create a file named letter if you want to experiment with the examples that follow. Alternatively, you can use touch (page 877) to create an empty file:
$ cd $ pwd /Users/zach $ touch letter
With /Users/zach as the working directory, the following example uses cp with a relative pathname to copy the file letter to the /Users/zach/literature/promo directory (you will need to create promo again if you deleted it earlier). The copy of the file has the simple filename letter.0610:
$ cp letter literature/promo/letter.0610
If Zach does not change to another directory, he can use vim to edit the copy of the file he just made:
$ vim literature/promo/letter.0610
If Zach does not want to use a long pathname to specify the file, he can use cd to make promo the working directory before using vim:
$ cd literature/promo $ pwd /Users/zach/literature/promo $ vim letter.0610
To make the parent of the working directory the new working directory, Zach can give the following command, which takes advantage of the .. directory entry:
$ cd .. $ pwd /Users/zach/literature
mv, cp: Moves or Copies a File
Chapter 3 discussed the use of mv to rename files. However, mv works more generally than that: You can use this utility to move files from one directory to another (change the pathname of a file) as well as to change a simple filename. When used to move one or more files to a new directory, the mv command has this syntax:
mv existing-file-list directory
If the working directory is /Users/zach, Zach can use the following command to move the files names and temp from the working directory to the literature directory:
$ mv names temp literature
This command changes the absolute pathnames of the names and temp files from /Users/zach/names and /Users/zach/temp to /Users/zach/literature/names and /Users/zach/literature/temp, respectively (Figure 4-10). Like most OS X commands, mv accepts either absolute or relative pathnames.
Figure 4-10. Using mv to move names and temp
As you work with Mac OS X and create more files, you will need to create directories using mkdir to keep those files organized. The mv utility is a useful tool for moving files from one directory to another as you develop your directory hierarchy.
The cp utility works the same way that mv does, except that it makes copies of the existing-file-list in the specified directory.
mv: Moves a Directory
Just as it moves ordinary files from one directory to another, so mv can move directories. The syntax is similar except that you specify one or more directories, not ordinary files, to move:
mv existing-directory-list new-directory
If new-directory does not exist, the existing-directory-list must contain just one directory name, which mv changes to new-directory (mv renames the directory). Although directories can be renamed using mv, their contents cannot be copied with cp unless you use the r option. Refer to the sections on ditto (page 44), cpio (page 693), pax (page 809), and tar (page 862) for other ways to copy and move directories.