Skip to content

Instantly share code, notes, and snippets.

@charlesrc019
Created November 8, 2024 02:26
Show Gist options
  • Save charlesrc019/2c87b5ac99f0ee21cae5e9dc510d9252 to your computer and use it in GitHub Desktop.
Save charlesrc019/2c87b5ac99f0ee21cae5e9dc510d9252 to your computer and use it in GitHub Desktop.
endpoint_rename_to_numbers.sh
#!/bin/zsh
# Check if directory argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 /path/to/directory"
exit 1
fi
# Set the directory
directory="$1"
# Initialize a counter
counter=35
# First pass: rename files to a temporary name
for file in "$directory"/*; do
if [ -f "$file" ]; then
# Extract the file extension
extension="${file##*.}"
# Rename the file to a temporary name (prefix with "temp_")
mv "$file" "$directory/temp_$counter.$extension"
((counter++))
fi
done
# Reset counter for the final renaming pass
counter=35
# Second pass: rename temporary files to final sequential names
for temp_file in "$directory"/temp_*; do
if [ -f "$temp_file" ]; then
# Extract the extension from the temporary file
extension="${temp_file##*.}"
# Rename to the final sequential name
mv "$temp_file" "$directory/$counter.$extension"
((counter++))
fi
done
echo "Renaming completed safely in $directory!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment