Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created May 22, 2024 08:17
Show Gist options
  • Save Integralist/1d11348ef687d6de453f2f9b7a223e61 to your computer and use it in GitHub Desktop.
Save Integralist/1d11348ef687d6de453f2f9b7a223e61 to your computer and use it in GitHub Desktop.
[Rename files matching specific pattern] #shell #bash #macos #files #rename
#!/bin/bash
# Iterate over each MP4 file in the current directory
for file in *.mp4; do
# Check if the file matches the pattern: MMDDYYYY <NAME>.mp4
if [[ "$file" =~ ^([0-9]{2})([0-9]{2})([0-9]{4})\ (.*)\.mp4$ ]]; then
# Extract the parts of the filename
MM="${BASH_REMATCH[1]}"
DD="${BASH_REMATCH[2]}"
YYYY="${BASH_REMATCH[3]}"
# Construct the new filename (YYYY.MM.DD.mp4)
new_filename="${YYYY}.${MM}.${DD}.mp4"
# Rename the file
mv "$file" "$new_filename"
# Print a message indicating the rename
echo "Renamed '$file' to '$new_filename'"
else
echo "File '$file' does not match the expected pattern."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment