Skip to content

Instantly share code, notes, and snippets.

@ElliotGluck
Last active August 13, 2023 18:06
Show Gist options
  • Save ElliotGluck/97e7e051f180ee452391440a5e160329 to your computer and use it in GitHub Desktop.
Save ElliotGluck/97e7e051f180ee452391440a5e160329 to your computer and use it in GitHub Desktop.
TLW Filename Randomizer
#!/bin/bash
# Check if the directory was passed as an argument
if [ "$#" -ne 1 ]; then
echo "Usage: $0 /path/to/photo/directory"
exit 1
fi
DIRECTORY="$1"
# Check if the provided argument is a directory
if [ ! -d "$DIRECTORY" ]; then
echo "Error: $DIRECTORY is not a directory."
exit 2
fi
# Change to the specified directory
cd "$DIRECTORY" || exit
# Generate a random name that doesn't already exist
generate_random_name() {
local ext="$1"
local rand_name
while true; do
rand_name=$(uuidgen | tr -d '-')
if [ ! -e "${rand_name}.${ext}" ]; then
echo "${rand_name}.${ext}"
return
fi
done
}
# Rename files to a random name
for file in *; do
# Extract file extension
extension="${file##*.}"
new_name=$(generate_random_name ".${extension}")
mv "$file" "$new_name"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment