1

I have files that are named like "clip-2014-01-31 18;50;15.mp4", i.e. "clip-YYYY-MM-DD hh;mm;ss.mp4".

How can I set the creation date and time according to how it is specified in the file name for all 2000 files I have in a folder. I know the command touch -t changes the creation date, but I do not know how to extract the date/time information from the file name and put this into a command so this is done automatically for all files.

3

You're lucky because the numbers in your file names are in just the order touch -t needs.

This command in the terminal will work. You just need to make sure your working directory is set to the folder you want to do:

for f in *; do
    t=$(echo $f | sed -E 's/([A-z]*-)|([ ,;])|(\..*)//g' | sed -E 's/(.*)(..)/\1.\2/')
    touch -t $t "$f"
done

To break it down:

for f in * sets the variable f to the name of each file in the directory, in turn.

do puts everything until the done into the for loop.

t=$(…) sets the variable t to the output of the commands in the parentheses.

The first sed command matches any letters before a - symbol, the - ; and the space symbols, and the file extension, and deletes them.

The second sed command inserts a period between the mm and ss values, as touch requires.

touch -t $t $f changes the file modification and creation times to the value of t on the file f.

Tested on some dummy files with whatever version of sed ships with Mavericks.

You must log in to answer this question.

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