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

  • You would use the -r option with touch, such as touch -r /path/to/file.avi /path/to/file.mp4. This still will not change the creation/birth time., for that you would use SetFile. – fd0 Apr 4 '17 at 17:42
  • @fd0 thanks for that info. So is there a way I could, perhaps using "ls" plus the right option "ls -lT" create a list of the files in folder_1 and have the output as input for "touch" or "SetFile"? – reno Apr 5 '17 at 7:51

You need to process each mp4 file individually:

cd folder_2
for m in *.mp4; do
    if [[ -r "/path/to/folder_1/$a" ]]; then
         echo touch -r "/path/to/folder_1/$a" "$m"

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.

  • The line a="${a%.mp4}".avi should bea="${m%.mp4}".avi. – fd0 Apr 6 '17 at 14:19
  • @fd0 truely true. Feel free to fix any other errors you may find :-) – nohillside Apr 6 '17 at 14:33
  • I don't see any other errors though I will point out that the variable in the extended brackets ([[) doesn't need to be quoted nor the ${m%.mp4} in the variable a assignment. The -r test may follow a symlink or for the matter any file system object. Overall a good answer. – fd0 Apr 6 '17 at 15:12
  • @fd0 I know about the quoting, but it's easier to just quote filenames every time than to keep the special cases in mind. – nohillside Apr 6 '17 at 15:15

You must log in to answer this question.

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