So I'm trying to recursively search folder, and as you might imagine it will be the "Photos"-folder in MacOS.
And in the process, I would like to change the file name, why you might ask? Because Nextcloud is for some reason changing the image name to year-month-day hour-minute-second $(increment number)
. So I need to be able to define this starting increment number.
This is what I have so far:
# Lets say that starting increment number should be I=7398
# name="$(stat -f '%Sm' -t '%y-%m-%d %H-%M-%S' "$file").$(ls "$file"|cut -d . -f2)"
# echo $name
find . -type f -iname '*.jpg' -size +500k -exec sh -c '
for file in "$@"; do
folder="/path/to/backup/folder/$(stat -f '%Sm' -t '%Y/%m' "$file")"
echo mkdir -p "$folder"
echo cp "$file" "$folder"
done
' sh {} +
This works perfect! But I would like to in the process change the name from the files EXIF data, to be '%y-%m-%d %H-%M-%S {increment number}'.ext
, 18-08-15 18-32-29 3453.JPG
or whatever it is. Is this possible?
This above script reads a JPG, creates directory of the year the file image was created, created another folder corresponding to the month, copies the file to that year folder.
Expected result: also change name '%y-%m-%d %H-%M-%S {increment number}'.ext
Best Answer
The following is a proof of concept to show how I'd build out the target_directory and target_filename from the source_filename, based on information in your OP and comments:
Obviously with the
echo
command in place, the output shown is not quoted and would fail if that was the actual command executed while copying; however, with theecho
command removed, the actual executed command has quotes around the arguments and would not fail because of the spaces in the target_filenames.Using
stat
instead ofexiftool
:Note: Both of these worked when run as either a script, or, sans the shebang, copied and pasted into Terminal. That said, I'm not sure I'd run this on 30K files all at once without adding some error handling as appropriate for what this code will do. I'll leave that up to you to decide how, or if, you want to implement any error handling in the example code shown.