You can take a backup of the destination file/directory before overwriting with the source file/directory using cp -b or –backup options. By default, the backup file name is given a tilde (~) at the end but you can overwrite the usual backup suffix (~) with cp -S or –suffix=SUFFIX option.
1 2 3 4 5 6 7 8 9 |
mkdir mydir1 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > myfile1 cp --suffix=.bak myfile1 mydir1 ## creates a backup (myfile1.bak) of myfile1 before overwriting cat mydir1/myfile1 ## returns two cat mydir1/myfile1.bak ## returns one rm -r my* |