MacOS – How to generate an automatic bash/script

bashmacosscriptterminal

I'm new to terminal and unix like systems. I'm searching a way to automatically create a batch file.

Let's say I have a bunch of files in a folder, and I want to change the creation dates. As it is now, I have to touch -t 201704011215.00 Desktop/Old_cam_vids_converted_from_avi_to_mp4/file0001.mp34 for each file. What I would like to do is something like this: ls -lT /Desktop/Old_avi_to_conv/*.avi and have the date and time for each file as time-date parameter for the touch command.

How can I accomplish this?

Edit:
I hope the explanation makes it clearer. This is the scenario:

  • Two folders on the desktop containing video files.
  • one (folder_1) contains the original files (.avi)
  • the second (folder_2) contains converted files (.mp4)

  • all the files in the second folder have a newer sequential date, since the conversion tool made worked through a list of files.

  • I need to take the date/time stamp from the original file, in the first folder and assign it to the converted file in the second folder.

  • each file in the first folder has a different date, possibly days and months apart.

  • the files in the second folder have the same names than the ones in the first folder, except for the file suffix, e.g.:
    folder_1/CIMG_0001.avi is the original of folder_2/CIMG_0001.mp4
    folder_1/PIC_0003.avi is the original of folder_2/PIC_0003.mp4
    folder_1/PIC_0015.avi is the original of folder_2/PIC_0015.mp4
    folder_1/CIMG_003.avi id the original of folder_2/CIMG_0003.mp4

Best Answer

You need to process each mp4 file individually:

cd folder_2
for m in *.mp4; do
    a="${m%.mp4}".avi
    if [[ -r "/path/to/folder_1/$a" ]]; then
         echo touch -r "/path/to/folder_1/$a" "$m"
    fi
done

Run once to verify that the generated output makes sense, then remove the echo and rerun.

PS: This assumes that the names of the video files don't contain the string ".mp4" as such.