-->
Page 66
directory. This is a safe, but not fail-safe, approach to deleting or recovering files. You may also be able to use the mc command, or Midnight Commander, discussed later in this chapter.
The mkdir command can create one or several directories with a single command line. You may also be surprised to know that mkdir can also create a whole hierarchy of directories, which includes parent and children, with a single command line.
This command is one of the basic tools (along with cp and mv) you'll use to organize your information. Now, take a look at some examples. The following simple command line creates a single directory:
# mkdir temp
But you can also create multiple directories with
# mkdir temp2 temp3 temp4
You'd think that you could also type the following to make a directory named child under temp:
# mkdir temp/child
And you can, because the temp directory exists (you just created it). But, suppose you type
# mkdir temp5/child mkdir: cannot make directory `temp5/child': No such file or directory
As you can see, mkdir complained because the temp5 directory did not exist. To build a hierarchy of directories with mkdir, you must use the -p, or parent option, for example:
# mkdir -p temp5/parent/child # tree temp5 temp5 `-- parent `-- child 2 directories, 0 files
As you can see, mkdir created not only the temp5 directory, but also a subdirectory called parent, and a subdirectory under parent called child.
Now that you know how to create directories, take a look at how to remove them.
Page 67
The rmdir command is used to remove directories. To remove a directory, all you have to do is type
# rmdir tempdirectory
But there's a catch: the directory must be empty first! If you try to delete a directory with any files, you'll get an error message like this:
# rmdir temp5 rmdir: temp5: Directory not empty
In this example, temp5 also contains other directories. The rmdir command would also complain if a directory contains only files and not directories. You can use the rm command to remove the files first (remember to be careful if you use the -fr option), or you can move the files somewhere else, or rename the directory, with the mv command, discussed next.
The rmdir command, like mkdir, also has a -p, or parent, option. You can use this option to remove directory hierarchies, for example:
# rmdir -p temp5 rmdir: temp5: Directory not empty
Hmm. That didn't work! How about
# rmdir -p temp5/parent rmdir: temp5/parent: Directory not empty
Hey! That didn't work either. Now try
# rmdir -p temp5/* rmdir: temp5/parent: Directory not empty
This is getting frustrating! Try it one more time:
# rmdir -p temp5/parent/child
Finally! As you can see, you must specify the complete directory tree to delete it. If you use the same command line, but without the -p option, only the child directory would be deleted. But what if there are two or more subdirectories, for example:
# mkdir -p temp5/parent/child # mkdir temp5/parent/child2 # tree temp5 temp5 `-- parent |-- child `-- child2 3 directories, 0 files
In order to delete the entire directory system of temp5, you'd need to use
Page 68
# rmdir temp5/parent/*
So far, you've seen how to create and remove directories. Next, you'll learn about the mv command, which you can use to move or rename files and directories.
The mv command, called a rename command but known to many as a move command, will indeed rename files or directories, but it will also move them around your file system.
Actually, in the technical sense, the files or directories are not really moved. If you insist on knowing all the gory details, read the Linux System Administrator's Guide, available through
http://sunsite.unc.edu/LDP/LDP/sag/index.html
In its simplest form, mv can rename files, for example:
# touch file1 # mv file1 file2
This command renames file1 to file2. However, besides renaming files, mv can rename directories, whether empty or not, for example:
# mkdir -p temp/temp2/temp3 # mv temp newtemp
Although mv has nine different options, this section concentrates on the two most commonly used. These options, -b and -i, allow you to use mv in a fairly safe way, because mv will not only rename, but overwrite silently and quickly! The first option, -b, creates a backup of any file or directory you rename to an existing name, for example:
# touch file1 file2 file3 # ls file* file1 file2 file3 # mv file1 file2 # ls file* file1 file2
As you can see, without using -b, mv not only renamed file1 to file2, but deleted file2 in the process. Is this dangerous? You bet! Now, try the -b option:
# touch file1 # ls file* file1 file2 file3 # mv -b file1 file2 # ls file* file2 file2~ file3
This example shows that although file1 has been renamed, replacing file2, a backup of file2 with a default extension of the tilde (~) has been created.