Bash scripts for taking backups of files and comparing the current file with the latest backed up version of the file.

From: Amit
Date: Thu Jan 08 2026 - 00:24:23 EST


Bash scripts for taking backups of files and comparing the current
file with the latest backed up version of the file.

-------------------
mybackup.sh
-------------------
--------------------------------------------------------------------------------------------
#!/bin/bash

backup_dir=~/backups

date=`date "+%Y-%m-%d-%H-%M-%S"`

set -x

cp $1 $backup_dir/$1.$date
--------------------------------------------------------------------------------------------

Example usage: mybackup.sh myfile.c

-------------------------------------------------------------------
compare_file_with_lastest_backed_up_version.sh
-------------------------------------------------------------------
--------------------------------------------------------------------------------------------
#!/bin/bash

ls -tr -1 ~/backups/$1.*

last=`ls -tr -1 ~/backups/$1.* | tail -n 1`

echo

set -x

diff -u $last $1
--------------------------------------------------------------------------------------------

Example usage: compare_file_with_lastest_backed_up_version.sh myfile.c

----