Skip to content

Instantly share code, notes, and snippets.

@WinterSnowfall
Created October 13, 2024 12:18
Show Gist options
  • Save WinterSnowfall/0277b97434d5a3f5178320734ea09890 to your computer and use it in GitHub Desktop.
Save WinterSnowfall/0277b97434d5a3f5178320734ea09890 to your computer and use it in GitHub Desktop.
Script to bulk rename all files in a folder to .bak. Optionally creates blank entries for those files as well. Includes undo operations.
#!/bin/bash
if [ $# -ge 1 ]
then
if [ "$1" == "add" -o "$1" == "zero" ]
then
for file in $(find . -maxdepth 1 -type f)
do
# exlude the script from renaming
if [ "$file" != "$0" ]
then
mv "$file" "$file.bak"
# generate empty files with the same name
if [ "$1" == "zero" ]
then
touch "$file"
fi
fi
done
elif [ "$1" == "remove" -o "$1" == "restore" ]
then
for file in $(find . -maxdepth 1 -type f -name "*.bak")
do
original=$(basename $file .bak)
# remove empty files with the same name
if [ "$1" == "restore" ]
then
# only remove empty placeholder files
if [ -s "$original" ]
then
rm -f "$original"
fi
fi
mv "$file" "$original"
done
else
echo "Invalid operation mode!"
exit 2
fi
else
echo "Please specify operation mode!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment