When running cp
command to copy files, I would like to be prompted for overwrite confirmation, and, if possible, view the file diff confirming.
Is this possible? And if yes, how?
Ideal example:
$ cp file1.txt file2.txt
0a1,2
> 1.
>
2a5,6
> 2.
>
4a9,10
> 3.
>
Overwrite file2.txt? [Yes/No/Keep both] (default Y):
Best Answer
Placing alias commands in your bash profile will gain you partial overwrite protection. As others mentioned in your comments, you will need to write a script add the diff function.
I placed the commands below in my
~/.bash_profile
.Bash on macOS determines what file is your bash profile in this order:
~/.bash_profile
~/.bash_login
~/.profile
These commands tell cp, mv and rm to give you a warning when a file is to be overwritten or deleted:
This example, assumes you have placed the above commands in your bash profile.
These alias command do not protect you in all circumstances like when you invoke a new shell or run cp, mv or rm from within other commands like find.
P.S.: The aliases mentioned above use the same name as the original command, thereby shadowing it. To access the original un-aliased command, prepend it with a
\
character (E.g.,\cp
,\mv
,\rm
etc.).