Skip to content

Instantly share code, notes, and snippets.

@Khazbs
Last active September 29, 2022 11:34
Show Gist options
  • Save Khazbs/d84f8771967ac4f4d22f0fcd98500442 to your computer and use it in GitHub Desktop.
Save Khazbs/d84f8771967ac4f4d22f0fcd98500442 to your computer and use it in GitHub Desktop.
Add this to your .bashrc to get a handy bak function for backing up files and directories
# Create backup copies of files or directories
bak() {
local name="$1.bak"
if [ -e "$name" ]; then
local -i num=2
while [ -e "$name$num" ]; do
num+=1
done
cp -rv "$1" "$name$num"
else
cp -rv "$1" "$name"
fi
}
@Khazbs
Copy link
Author

Khazbs commented Sep 29, 2022

As always, don't forget to re-login or source .bashrc after adding.

Usage example:

$ mkdir thesis
$ echo "ABSTRACT" > thesis/abstract.txt
$ bak thesis
'thesis' -> 'thesis.bak'
'thesis/abstract.txt' -> 'thesis.bak/abstract.txt'
$ echo "CONCRETE" > thesis/abstract.txt
$ bak thesis
'thesis' -> 'thesis.bak2'
'thesis/abstract.txt' -> 'thesis.bak2/abstract.txt'
$ echo "INSTANCE" > thesis/abstract.txt
$ tail -vn+1 thesis*/*
==> thesis/abstract.txt <==
INSTANCE

==> thesis.bak2/abstract.txt <==
CONCRETE

==> thesis.bak/abstract.txt <==
ABSTRACT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment