Last active
August 13, 2023 18:06
-
-
Save ElliotGluck/97e7e051f180ee452391440a5e160329 to your computer and use it in GitHub Desktop.
TLW Filename Randomizer
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 | |
# 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