Better safe than sorry right? To prevent yourself from stupid mistakes when removing files you can add the
-i option to the
rm command. This will ask for a confirmation before removing the file.
$ rm -i secrets.txt rm: remove regular file 'secrets.txt'?
You can now press
y to indeed remove the file. For directories this works the same.
$ mkdir secrets $ vim secrets/dirty.txt $ rm -ir secrets rm: descend into directory 'secrets'? y rm: remove regular file 'secrets/dirty.txt'? y rm: remove directory 'secrets'? y
Right, it works, but a confirmation for each individual directory and file is a bit overkill if you’d ask me. Using the capital
I option instead fixes this.
$ mkdir secrets $ vim secrets/dirty.txt $ rm -Ir secrets rm: remove 1 argument recursively? y
Typing out this
-I is something that I’d probably forget at some point. So instead I decided to make it an alias for the
To do this open up the
~/.zshrc config file, or wherever you add your aliases, and add the following:
# ~/.zshrc alias rm='rm -I'
Somewhat more trivial but maybe useful too is that the
mv commands both accept this
-i option too.
Now what if you unintentionally removed a file anyway? Then I hope you checked the file in to version control.