1

I'm trying to write a shell script to use in a Service I'm creating in Automator to change the "Date Modified" of all files selected in Finder to the date of the oldest modified file selected. The trouble is, I only want to process the files I have selected - not all files in a given folder.

I've been trying to do this using a "for" loop, but can't seem to get it to work. I've also tried using the following to sort the list of files received as input, but can't seem to figure out how to get it to work this way either.

oldest=$(stat -f "%m:%N" "$@" | sort -n | head -1 | cut -f2 -d:)

1 Answer 1

1

I figured it out! I needed to do both: sort the selected input files as arguments and use a for loop to modify each selected file.

Here's the shell script I ended up adding as a Service in Automator (receives files & folders in Finder.app, pass input as arguments):

oldest=$(stat -f "%m:%N" "$@" | sort -n | head -1 | cut -f2 -d:)

for f in "$@"; do

    touch -r "$oldest" "$f"

done

I also figured I would add a similar Service to do the same thing, except setting the date modified of all selected files to match that of the newest selected file. Here's the shell script I ended up adding as a Service in Automator for that (receives files & folders in Finder.app, pass input as arguments):

newest=$(stat -f "%m:%N" "$@" | sort -rn | head -1 | cut -f2 -d:)

for f in "$@"; do

    touch -r "$newest" "$f"

done

Hope this proves useful for others who may have a similar problem and stumble upon this question and the solution I found for the issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .