Skip to content

Instantly share code, notes, and snippets.

@Linda-chan
Created June 21, 2018 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Linda-chan/342a2b35ee32178d11e68982aa4f1479 to your computer and use it in GitHub Desktop.
Save Linda-chan/342a2b35ee32178d11e68982aa4f1479 to your computer and use it in GitHub Desktop.
#!/bin/bash
SRC_FILE_NAME="$1"
if [[ "$SRC_FILE_NAME" = "" ]] ; then
echo "Usage: $0 FILE"
exit
fi
if [[ ! -f "$SRC_FILE_NAME" ]] ; then
echo "Not a regular file: $SRC_FILE_NAME"
exit
fi
for TMP in {00..99}
do
BAK_FILE_NAME="$SRC_FILE_NAME.bak$TMP"
if [[ ! -e "$BAK_FILE_NAME" ]] ; then
echo "Backing up file: $SRC_FILE_NAME ==> $BAK_FILE_NAME"
cp --preserve=all -- "$SRC_FILE_NAME" "$BAK_FILE_NAME"
if [[ "$?" == "0" ]] ; then
chmod -x -- "$BAK_FILE_NAME"
fi
exit
fi
done
echo "Can't find free file name: $SRC_FILE_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment