I wanted to gift back to the author and the community, but could not add a comment to the article directly so here is my 2 cents worth of Bashery
for creating a backup file if a file is overwritten by cp
I use \cp to make sure I do not use the aliase verson of cp
alias cp=‘cp -i’
alias cpk=‘\cp --backup=numbered’
function bak() # will create a .bak copy of 1 or n files, safe even if created within a second
{
for file ; do
\cp --backup=numbered "$file" "$file".bak-`date +%y%m%d-%H:%M:%S`;
done
}
to test:
echo >1 “test” . echo >2 “test”
bak 1 2; bak 1 2; bak 1; 2
ll
-rw-r–r-- 1 root root 5 Nov 15 18:18 1.bak-181115-18:18:58
-rw-r–r-- 1 root root 5 Nov 15 18:18 1.bak-181115-18:18:58.~1~
-rw-r–r-- 1 root root 5 Nov 15 18:18 1.bak-181115-18:18:58.~2~
-rw-r–r-- 1 root root 5 Nov 15 18:18 2.bak-181115-18:18:58
-rw-r–r-- 1 root root 5 Nov 15 18:18 2.bak-181115-18:18:58.~1~
-rw-r–r-- 1 root root 5 Nov 15 18:18 2.bak-181115-18:18:58.~2~