< Previous | Contents | Next >
Useful Options And Examples
Here are some of the common options for rm:
Table 4-8: rm Options
Option Meaning
Option Meaning
-i, --interactive Before deleting an existing file, prompt the user for
confirmation. If this option is not specified, rm will silently delete files.
-r, --recursive Recursively delete directories. This means that if a
directory being deleted has subdirectories, delete them too. To delete a directory, this option must be specified.
-f, --force Ignore nonexistent files and do not prompt. This
overrides the --interactive option.
-v, --verbose Display informative messages as the deletion is
performed.
Table 4-9: rm Examples
Command Results
Command Results
rm file1 Delete file1 silently.
rm -i file1 Same as above, except that the user is prompted for confirmation before the deletion is performed.
rm -r file1 dir1 Delete file1 and dir1 and its contents.
rm -rf file1 dir1 Same as above, except that if either file1 or dir1 do
not exist, rm will continue silently.
Be Careful With rm!
Unix-like operating systems such as Linux do not have an undelete command. Once you delete something with rm, it's gone. Linux assumes you're smart and you know what you're doing.
rm – Remove Files And Directories
Be particularly careful with wildcards. Consider this classic example. Let's say you want to delete just the HTML files in a directory. To do this, you type:
rm *.html
which is correct, but if you accidentally place a space between the “*” and the “.html” like so:
rm * .html
the rm command will delete all the files in the directory and then complain that there is no file called “.html”.
Here is a useful tip. Whenever you use wildcards with rm (besides carefully checking your typing!), test the wildcard first with ls. This will let you see the files that will be deleted. Then press the up arrow key to recall the command and replace the ls with rm.