Hello Sean!
On 19 Dec 2024, Sean Dennis said the following...
## Rename files
cd ./pics
for i in *.*; do
new=$(printf "%04d.*" "${a}") #04 pad to length of 4
mv -i -- "$i" "$new"
((a++))
done
How about this one (assuming bash as the shell):
a=0
while IFS= read -r -d $'\0' old; do
new_path="${old%%/*}"
new_name="${old##*/}"
new_name="$(printf "%04d" "$a").${new_name##*.}"
echo mv -i -- "$old" "${new_path}/${new_name}"
((a++))
done < <(find pics/ -xdev -mindepth 1 -maxdepth 1 -type f -print0)
(Remove the "echo" to activate.)
It will find files in the pics subdirectory -- only there, not deeper, and not traversing into any other file systems -- and feed their full paths to the while loop, separated by NUL characters. The while loop will extract the path and the filename, and construct the new desired full path, using the padded counter as the first part of the filename.
The process substitution/redirection feeding the while loop will ensure that no command inside the while loop (e.g. the "mv" command) can interfere with its input (e.g. interpreting a full path on the input as a user response to overwriting a destination file that already exists).
The IFS stuff and the NUL separation should help for paths/filenames containing (very) special characters.
Hope this helps, or at least sparks some ideas! =)
Best regards
Zip
--- Mystic BBS v1.12 A49 2024/05/29 (Linux/64)
* Origin: Star Collision BBS, Uppsala, Sweden (618:500/27)