UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 23.6 A Faster Way to Remove Files Interactively Chapter 23
Removing Files
Next: 23.8 Safe Delete: Pros and Cons
 

23.7 Safer File Deletion in Some Directories

Using noclobber (13.6) and read-only files only protects you from a few occasional mistakes. A potentially catastrophic error is typing:

% rm * .o

instead of:

% rm *.o

In the blink of an eye, all of your files would be gone. A simple, yet effective, preventive measure is to create a file called -i in the particular directory in which you want extra protection:

touch ./- 
% touch ./-i

In the above case, the * is expanded to match all of the filenames in the directory. Because the file -i is alphabetically listed (51.3) before any file except those that start with one of these characters: !#$%&`()*+,, the rm command sees the -i file as a command-line argument. When rm is executed with its -i option (21.11), files will not be deleted unless you verify the action. This still isn't perfect. If you have a file that starts with a comma (,) in the directory, it will come before the file starting with a dash, and rm will not get the -i argument first.

The -i file also won't save you from errors like:

% rm [a-z]* .o

[Two comments about Bruce's classic and handy tip: first, if lots of users each make a -i file in each of their zillions of subdirectories, that could waste a lot of disk inodes (1.22). It might be better to make one -i file in your home directory and hard link (24.4) the rest to it, like:

~ 
% cd
% touch ./-i
% cd somedir
% ln ~/-i .
   ...

Second, to save disk blocks, make sure the -i file is zero-length - use the touch command, not vi or some other command that puts characters in the file. -JP ]

- BB


Previous: 23.6 A Faster Way to Remove Files Interactively UNIX Power ToolsNext: 23.8 Safe Delete: Pros and Cons
23.6 A Faster Way to Remove Files Interactively Book Index23.8 Safe Delete: Pros and Cons

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