You can use cp –backup[=CONTROL] option to take a backup of each existing destination file. CONTROL can be none (Never make backups), numbered or t (Make numbered backups), existing or nil (Numbered if numbered backups exist, simple otherwise), and simple or never (Always make simple backups).
1 2 3 4 5 6 7 8 9 10 11 12 |
mkdir mydir1 ## create some files and directory echo "one" > mydir1/myfile1 echo "two" > myfile1 cp --backup=t myfile1 mydir1 ## creates a backup (myfile1.~1~) of myfile1 before overwriting echo "three" > myfile1 cp --backup=t myfile1 mydir1 ## creates a backup (myfile1.~1~) of myfile1 before overwriting cat mydir1/myfile1 ## returns three cat mydir1/myfile1.~1~ ## returns one cat mydir1/myfile1.~2~ ## returns two rm -r my* |