Mac OS X Unleashed

Mac OS X Unleashed

By John Ray and William C. Ray

Deleting Files

Removing files and directories with Unix are fairly straightforward tasks, so you need to know only two commands to accomplish them.

Removing Files and Directories: rm, rmdir

Now that you have filled up your directory with lots of files, it is time to learn how to clean it up a bit. You can remove a file with rm.

Our user nermal has decided that her file called myfile is no longer needed. To remove it she does the following:

[localhost:~] nermal% ls -l myfile

     -rw-r--r--  1 nermal  staff  51 Apr 12 15:11 myfile
[localhost:~] nermal% rm myfile

     remove myfile? Y

As you notice in this example, rm prompts for confirmation before removing the file. Your system might not be configured to make rm prompt you for the removal of files. Information on how to make it ask you (rm's interactive mode) is provided in Appendix B, and we very highly recommend that you follow the instructions to make your rm command interactive at the first possible opportunity. After you're a seasoned Unix user, you're welcome to run with rm in noninteractive mode by default, but until you've been certain that you're ready for a few years, it's probably safest to have it operate interactively by default.

Our user nermal has also decided that she is done with the data that she copied from user joray's directory, and that she wants to remove the entire directory. The easiest way to do this is to force rm to recursively remove the directory, using options -r for recursive, and -f to override the interactive mode she has enabled by default, as shown here:

[localhost:~] nermal% ls -ld tests-for-nermal

     drwxr-xr-x  9 nermal  staff  262 Apr 23 11:29 tests-for-nermal

[localhost:~] nermal% rm -rf tests-for-nermal

[localhost:~] nermal% ls -ld tests-for-nermal

     ls: tests-for-nermal: No such file or directory

Removing a directory and all its contents using the recursive, and force options to rm is easy, but it is also silent and very, very fast. Remember to use those options only when you really mean it. Double-check everything before you run it. As you can see in the example, there is no recourse if you type the wrong thing.

The complete syntax and options for rm are in Table 13.11, the command documentation table.

Table 13.11. The Command Documentation Table for rm

rm Removes directory entries.
rm [-f | -i] [-dPRrW] <file1> 
               <file2> ...
-f Forces the removal of files without prompting the user for confirmation. If the file does not exist, no error diagnostic is displayed. The -f option overrides any previous -i options.
-i Invokes an interactive mode that prompts for confirmation before removing a file. The -i option overrides any previous -f options.
-d Attempts to remove directories as well as other types of files.
-P Overwrites regular files before deleting them. Files are overwritten three times before being deleted; first with byte pattern 0xff, and then 0x00, and then 0xff.
-R Attempts to recursively remove files. Implies -d option.
-r Same as -R.
-W Attempts to undelete files. This option can be used to recover only files covered by whiteouts.
rm removes symbolic links, but not the files referenced by the links.
Also, attempting to remove the files . and .. is an error.

There is also a command available for removing directories: rmdir. Unfortunately, it is only useful for removing empty directories. Its complete syntax and options are in the command documentation table, Table 13.12.

Table 13.12. The Command Documentation Table for rmdir

rmdir Removes directories.
rmdir [-p] <directory1> 
               <directory2> ...
rmdir removes each <directory> argument specified, provided it is empty. Arguments are processed in the order listed on the command line. To remove a parent directory and subdirectories of the parent directory, the subdirectories must be listed first.
-p Attempts to remove the specified directory and its parent directories, if they are empty.

Share ThisShare This

Informit Network