Everyone at some point has faced that horrible moment when you realize that you just deleted a critical file, and start scrambling to find ways to recover it before it is too late. If you haven’t faced this situation yet, you are the lucky few who have been spared from this horrible experience so far. Just the other day, I found a really creative solution in Linux that will prevent you from accidentally deleting files by mistake. It’s actually not that complicated either, and I am sure you will be using this technique to protect your most valuable files after your through reading my how-to.
Step 1 – Choose a Directory
As you can see, I have created a directory called “test” and have created a ton of empty files and directories for testing purposes. It was fun writing the Perl script to create all those “crap” files.
Step 2 – Create a empty file call “-i” in the chosen directory
To do this simply use the touch command to create the empty file “-i”, as such: “touch ./-i”
Step 3 – Verify that “-i” exists in the current directory
To make sure “-i” was created successfully, list the directory contents by invoking the “ls” command as show above. Once you do that you will notice a new file called “-i” listed in the current directory.
Step 4 – Try to delete the files in the current directory
Try deleting all of them at once by using “rm *”.
WARNING – make sure you have the “-i” the the directory before you try to remove any files! Without the “-i” file there will be nothing to stop the rm command from removing your precious files!!
Below is a screenshot of what should happen when you try to delete any file in that directory.
Did you notice what happened? When we tried to delete any file in that directory, It would prompt us to confirm if we really wanted to delete each file. This happens because the “-i” file is immediately interpreted by the Unix/Linux command line as a option for the “rm” command, which prompts before deleting a file. If you man rm and look through the man page you will find the following entry:
-i, --interactive prompt before any removal
So that is the magic behind creating the empty “-i” file and how it helps prevent you from deleting file by mistake. Now don’t forget that if you use the rm -f command, it will delete the file no matter what!! That’s why it’s called the FORCE option!
Use this technique to your advantage and spread the word!
Related posts:
- Understanding Unix/Linux Load Averages I was looking at some linux server the other day,...
Related posts brought to you by Yet Another Related Posts Plugin.