Aliases You Didn’t Know You Wanted: Options `-iv`
--
Today we’ll look at aliasing two options, -i
and -v
, to three common Unix commands, cp
, mv
, and rm
.
TL;DR
Add the following lines to the file where you keep your aliases:
alias cp="cp -iv"
alias mv="mv -iv"
alias rm="rm -iv"
Be sure to source the file into an existing session with source <path/to/your/aliases>
or instantiate a new session by running zsh
or by opening a new terminal window.
The Options
The option flags we will be using are very comparable between commands cp
, mv
, and rm
.
-i
has a slightly different behavior depending on the command that precedes it, but I generally like to think of it in this context as an option to prompt before destructive actions are allowed. For example, the documentation for the mv
command states that -i
will “cause `mv` to write a prompt to standard error before moving a file that would overwrite an existing file…”
-v
causes the command to be verbose, providing valuable feedback to those who haven’t quite learned to trust their terminal…wink wink. Jokes aside, feedback about command execution can be really nice.
As always, exploring available options for a given command is as simple as entering man <command>
into your terminal.
The Aliases
Open the file where you keep your aliases. If you aren’t quite sure what this means have a quick look at this short primer on simple and advanced setups. Adding the following aliases means that anytime we use the standard commands, the options will always be included. Pretty handy huh?
Copy
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 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.
Move
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.
Remove
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 useful feedback though I’ve personally found that recursive actions on large directories is where having them aliased is not something I prefer. But the possibility is there so try the alias out for a while and see what you think.
Wrapping Up
And there you have it! Three aliases you didn’t even know you wanted. I hope they make your life in the terminal easier. Would love to hear the ways you’ve used or extended the 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 personal aliases in my dotfiles repo if you need a little inspiration. Happy aliasing!