Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Occasionally I use command to delete some files. For example, the name might start with 'a', so I'll delete with the command rm -r a*, but I don't want to remove some other directory by accident. How can I lock a directory to prevent deletion by rm -r ?

share|improve this question

4 Answers 4

You could alias the rm command to rm -i in your shell, so it will ask your confirmation for each file.

However, some consider this to be harmful. See alias rm=“rm -i” considered harmful? on Superuser.com

Other thoughts are in How do I prevent accidental rm -rf /?* on StackOverflow.com.

share|improve this answer

The best advice is to grow to fear and respect the 'rm -r' or 'rm -rf' commands enough that, when you type them, you stop yourself and proceed slowly and with caution (reading over the command carefully a few times). The commands are useful and sometimes need to be used - but it's not a good idea to trick the system into saving you from this type of mistake; it's not a good idea to grow accustomed to such workarounds---consider building good habits that can be carried from machine to machine.

share|improve this answer

If you are trying to selectively remove items, you can use find .

find . -name "a*" \! -type d -delete

You can check what you are deleting by running:

find . -name "a*" \! -type d -print

For example, create a some directories and files:

mkdir -p testDir/a/b 
cd testDir
touch apple.txt banana.txt ant.tx a/antelope.txt a/bus.txt a/b/apostrophe.txt a/b/bug.txt

To see the files and directories:

find . -name "*" -print

returns:

.
./a
./a/antelope.txt
./a/b
./a/b/apostrophe.txt
./a/bus.txt
./ant.tx
./apple.txt
./banana.txt

To verify what is being deleted:

find . -name "a*" \! -type d -print

returns:

./a/antelope.txt
./a/b/apostrophe.txt
./ant.tx
./apple.txt

To delete the files:

find . -name "a*" \! -type d -delete

To verify that only a* files have been delete:

find . -name "*" -print

returns:

.
./a
./a/b
./a/bus.txt
./banana.txt
share|improve this answer
    
removed my comment. :) –  Evandro Silva Nov 15 '12 at 13:45

In addition to the other answers here, I recommend that you use tab completion more and globbing less. From your question, it appears that you're trying to delete a single file but don't want to type the full name. In that case, instead of * you should hit Tab. Tab completion is much safer.

If you actually do intend to delete multiple files whose name matches a glob, the best way, as already pointed out, is to think twice before hitting enter. In addition, you can gain a lot of peace of mind if you use a backup solution that backs up all changes as you make them so you can always restore any mistake without risk of data loss.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.