Skip to content

Instantly share code, notes, and snippets.

@shitchell
Created July 7, 2020 01:04
Show Gist options
  • Save shitchell/66ce1ca42515cc4762218bfec6109a15 to your computer and use it in GitHub Desktop.
Save shitchell/66ce1ca42515cc4762218bfec6109a15 to your computer and use it in GitHub Desktop.
Easily create backups of files with a .bak extension (also adds a trailing number if the file already exists)
#!/bin/bash
for filepath in $@; do
if [[ -f "$filepath" ]]; then
filepath_bak=$filepath.bak
if [[ -f "$filepath_bak" ]]; then
i=1
filepath_bak_i=$filepath_bak".$i"
while [[ -f "$filepath_bak_i" ]]; do
((i++))
filepath_bak_i=$filepath_bak".$i"
done
filepath_bak=$filepath_bak_i
fi
echo -n "$filepath => "
cp $filepath $filepath_bak
echo "$filepath_bak"
else
echo "$filepath: not a file"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment