home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 23.16 Removing a Strange File by its I-number Chapter 23
Removing Files
Next: 23.18 How Making and Deleting Directories Works
 

23.17 Problems Deleting Directories

rmdir
What if you want to get rid of a directory? The standard way, and the safest way, to do this is to use the UNIX rmdir "remove directory" utility (or the GNU version on the CD-ROM):

% rmdir files

The rmdir command often confuses new users. It will only remove a directory if it is completely empty; otherwise, you'll get an error message:

% rmdir files


rmdir: files: Directory not empty
% ls files


%

As in the example, ls will often show that the directory is empty. What's going on?

It's common for editors and other programs to create "invisible" files (files with names beginning with a dot). The ls command normally doesn't list them; if you want to see them, you have to use ls -a (16.11 ) :

% rmdir files


rmdir: files: Directory not empty
% ls -a files


.    ..    .BAK.textfile2

Here, we see that the directory wasn't empty after all: there's a backup file that was left behind by some editor. You may have used rm * to clean the directory out, but that won't work: rm also ignores files beginning with dots, unless you explicitly tell it to delete them. We really need a wildcard pattern like .??* (or more) (15.5 ) :

% rmdir files


rmdir: files: Directory not empty
% ls -a files


.    ..    .BAK.textfile2
% rm files/.??*


% rmdir files


%

Other pitfalls might be files whose names consist of "non-printing" characters or blank spaces - sometimes these get created by accident or by malice (yes, some people think this is funny). Such files will usually give you "suspicious" ls output (16.13 ) (like a blank line).

If you don't want to worry about all these special cases, just use rm -r :

% rm -r files

This command removes the directory and everything that's in it, including other directories. A lot of people warn you about it; it's dangerous because it's easy to delete more than you realize. Personally, I use it all the time, and I've never made a mistake. I never bother with rmdir .

- ML


Previous: 23.16 Removing a Strange File by its I-number UNIX Power Tools Next: 23.18 How Making and Deleting Directories Works
23.16 Removing a Strange File by its I-number Book Index 23.18 How Making and Deleting Directories Works

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System