Aliases to make the mv and cp commands more user friendly
- Published at
- Updated at
- Reading time
- 2min
I added new aliases to my dotfiles after watching this video by "Tom on the internet".
Tom explains the -iv options of mv and cp, which led me to add these two aliases:
alias mv="mv -iv"
alias cp="cp -iv"
Most commands, by default, don't give much information on what's happening or what has happened. I hated this when I started using the terminal. The reason behind this is that if you're automating things with shell scripts, you don't deal with additional log lines in your terminal.
The thing is, I don't write complex scripts, though. I use the terminal to start commands, create files, and move them around. That's it and this is why I appreciated to get. more information.
Many commands take your instructions very seriously, too. If you're moving, copy or rename a file to a target that already exists, it overwrites it. No questions asked. This behavior can lead to unintended data loss.
I added the above aliases to make the mv and cp command more user friendly.
To move files, you usually use the mv command. I learned that it supports two flags that make it easier to use. Applying the -iv options to mv makes the command more verbose. It will also tell you and double-check if you want to overwrite another file.
# log more information to the terminal
mv one.txt two.txt
three.txt -> one.txt
# ask before overwriting a file
mv two.txt three.txt
overwrite three.txt? (y/n [n]) n
not overwritten
The same options work for to the cp command to copy files.
# log more information to the terminal
cp one.txt two.txt
one.txt -> two.txt
# ask before overwriting a file
cp two.txt three.txt
overwrite three.txt? (y/n [n]) n
not overwritten
I'm aware that it can be dangerous to get used to overwritten default commands like cp and mv. If you're in a remote shell, you might overwrite files because you are used to the safety net. However, you don't need to overwrite the defaults. You could also go for smv and scp (safe copy and safe move?)... 🤷
In my case, I rarely deal with remote servers, so overwriting the default commands is fine.
Join 6.1k readers and learn something new every week with Web Weekly.
