-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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