Add the following lines to the file where you keep your aliases to (1) prompt prior to destructive actions and (2) give verbose feedback:
alias cp="cp -iv" alias mv="mv -iv" alias rm="rm -iv"
Today we’ll look at aliasing two options:
-v, to three common Unix commands:
rm, so they are always called by default. The option flags are comparable across all three commands.
-i option has a slightly different behavior depending on the command that precedes it, but I generally think of it as an option to prompt before destructive actions are allowed. For all three commands, the
-v option causes them to be verbose, providing valuable feedback about the files affected by the command’s execution.
As always, exploring available options for a given command is as simple as entering
man <command> into your terminal.
Open the file where you keep your aliases. Adding the following aliases means that anytime you type the commands (without options), the option flags will always be included.
First we’ll alias the
cp command. Add the following to your alias file:
alias cp="cp -iv"
Source the alias file and let’s test the alias. For our purposes here, I’ll create two dummy files in ~/desktop/ called foo.txt and bar.txt by entering
cd ~/Desktop; touch foo.txt bar.txt.
If we copy foo.txt to a new directory, we will see confirmation via the
-v option printed to standard output. Now if we copy foo.txt to bar.txt, we will see the
-i option give a prompt asking to overwrite bar.txt and if we confirm, another verbose confirmation printed to standard output.
Next we’ll alias the
mv command. Add the following to your alias file:
alias mv="mv -iv"
Source the alias file and let’s test the alias. If we move bar.txt into a new directory, we will again see confirmation via
-v. Now if we move foo.txt into the same directory as the version copied from the previous section, we will see a prompt via the
-i option and if we confirm, confirmation of the move.
Lastly we’ll alias the
rm command. Add the following to your alias file:
alias rm="rm -iv"
Source the alias file and let’s test the alias. If we remove foo.txt, the
-i option prompts confirmation of the deletion. Confirm and see verbose confirmation of the deletion (although admittedly vague). For giggles, let’s remove bar.txt but decline when prompted. Because no action was taken, there is no
-v info provided to standard output.
The options give very useful feedback when dealing with directories and recursive deletion, so try the alias out for a while and see what you think.
And there you have it! Three aliases you didn’t even know you wanted. I hope they make your life in the terminal easier. I would love to hear about the ways you implemented options for these commands or any aliases you can’t live without! Keep an eye out for more stories in this series and take a look at my aliases in my dotfiles repo if you need a little inspiration. Happy aliasing!