Section 3.22. Deleting a File: rm


[Page 62 (continued)]

3.22. Deleting a File: rm

The rm utility allows you to remove a file's label from the hierarchy. When no more labels reference a file, Linux removes the file itself. In most cases, every file only has one label, so the act of removing the label causes the file's physical contents to be deallocated. However, in Chapter 4, "GNU Utilities for Power Users," I'll show you some occasions where a single file has more than one label. In these cases, a label may be removed without affecting the file that it refers to. Figure 3-25 describes rm.


[Page 63]

Figure 3-25. Description of the rm command.

Utility: rm -fir {fileName} *

The rm utility removes a file's label from the directory hierarchy. If the filename doesn't exist, an error message is displayed. The -i option prompts the user for confirmation before deleting a filename; press y to confirm, and n otherwise. If fileName is a directory, the -r option causes all of its contents, including subdirectories, to be recursively deleted. The -f option inhibits all error messages and prompts.


To remove every file in the "lyrics.draft" directory, I moved into the "lyrics.draft" directory and used rm:

$ cd lyrics.draft       ...move to "lyrics.draft" dir. $ rm heart.ver1 heart.ver2 heart.ver3 heart.ver4 heart.ver5 $ ls                    ...nothing remains. $ _ 


Now that all the files were erased, I moved back to my home directory and erased the draft directory:

$ cd                   ...move to my home directory. $ rmdir lyrics.draft   ...this time it works. $ _ 


As you'll see in Chapter 5, "The Linux Shells," there's a much easier way to erase a collection of files when you're using a shell. I could have written the following instead:

$ cd lyrics.draft       ...move into "lyrics.draft" directory. $ rm *                  ...erase all files in current dir. 


Even better, I could have used the more advanced -r option of rm to delete the "lyrics.draft" directory and all of its contents with just one command:

$ cd                    ...move to my home directory. $ rm -r lyrics.draft    ...recursively delete directory. $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net