If you want to backup a file, with timestamps (atime,ctime,mtime) preserved, you can run this command:
cp -pf --backup=t filename filename
Course, this is a pain to write, so an alias is in order:
alias bk='cp -pf --backup=t'
To use this alias, you can type
bk myimportantfile.txt myimportantfile.txt
But then why type it twice? Use a shell function instead:
bk () {
[ "$1" == "-h" ] && \
echo "Backs up one or more files." && \
echo "usage: bk filename1 [filename2...]" && \
return
for f in $@ ; do
local f="$1"; shift
\cp -pf --backup=t "$f" "$f"
done
}
Oh, and this script assumes
- bash
- gnu ls
- gnu cp
- no whitespaces in words.