File Management  «Prev  Next»
Lesson 4 Removing directories quickly
Objective Use the rm command to remove directories.

Removing Directories quickly in Unix

You use the rmdir command to remove directories that are empty. However, if the directory contains many subdirectories, you must use repeated combinations of rm * and rmdir to do the job. In addition, rm * does not remove hidden files. To do that, you must use rm with a pattern like .* instead. A more efficient way to remove a directory and all of its contents, including subdirectories, is to use rm with the -r option, as follows: % rm -r directory
Once you delete a directory hierarchy, it is gone forever, unless there is a backup copy. If you want rm to prompt you for confirmation before deleting files use the -i option.

For an even more efficient approach, use rm -rf.

The -f option forces deletion of files to which you lack write access as long as you own those files. Normally, rm -r prompts you for confirmation before deleting files that are read-only or read/execute-only. For example, suppose a directory contains a file named myfile, with an access mode of
. If you attempt a rm -r on this directory, you will get the following prompt: Override protection 444 for myfile? You either can answer yes each time you get this prompt, or you can use rm -rf and suppress the prompt.
In the next lesson, the creation of a symbolic link using the ln -s command will be discussed.

Copying Removing Directories Exercise

Click the Exercise link below to practice some of the new options you’ve learned for ls, cp, and rm.
Copy Removing Directories - Exercise