Created
November 8, 2024 02:26
-
-
Save charlesrc019/2c87b5ac99f0ee21cae5e9dc510d9252 to your computer and use it in GitHub Desktop.
endpoint_rename_to_numbers.sh
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/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