I am attempting to rename all files (.jpg,.jpeg,.mov,etc) in parent directory and all subdirectories to date_time_7digitnumber without changing the file extension. The below script accomplishes this task but it runs numerous times, changing what was already correct. Here is the complete script. Any help would be greatly appreciated!
System: MacOS Monterey v12.5 Homebrew Installed with coreutils for gdate usage
#!/usr/bin/env bash
clear
RED='\033[0;31m'
NC='\033[0m'
shopt -s globstar nocaseglob nocasematch nullglob
echo
echo
echo -e "${RED}********** Your current directory path is " $PWD " **********${NC}"
read -p "
********** This function is going to rename all media files with **********
********** Date-Time-7digit number **********
********** !!THIS ACTION CAN NOT BE UNDONE!! **********
********** !!MAKE SURE TO CHANGE TO THE CORRECT DIRECTORY!! **********
*** ARE YOU ABSOLUTELY SURE YOU ARE IN THE CORRECT DIRECTORY AND WISH TO PROCEED? ***
[Y]es or [N]o " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
n=1
for f in * ; do
find . -type f -print0 |
while IFS= read -r -d '' f ; do
dd=$(dirname "$f")
ext=${f##*.}
new=$(gdate -r "$f" +"%Y%m%d_%H%M%S")-$(printf "%07d%s" "$n")
mv -vn "$f" "$dd"/"$new"."$ext" ; ((n++))
done
done
Echo
Echo Renaming Process Complete!
Echo
afplay /System/Library/Sounds/Ping.aiff
afplay /System/Library/Sounds/Ping.aiff
afplay /System/Library/Sounds/Ping.aiff
fi
find
are redundant operations. Cleaning up your code, something like this should work: ix.io/47Ee. However, I assumedgdate
can be replaced withdate
.find
insidefor
loop. Drop thefor
loop and it should do it.