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 ?
|
|
|
You could alias the 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. |
|||
|
|
|
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. |
|||
|
|
|
If you are trying to selectively remove items, you can use
You can check what you are deleting by running:
For example, create a some directories and files:
To see the files and directories:
returns:
To verify what is being deleted:
returns:
To delete the files:
To verify that only a* files have been delete:
returns:
|
||||
|
|
|
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 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. |
|||
|
|