I have a folder structure of thousands of pictures and videos. They are all exported and therefore have the same creation date, time. I would like to set the creation date for all the files according to the names the have. All files follow the same naming convention and have the date and time in the file name.


Pictures (top folder)
-- Folder 1
---- 2010-05-03
------ 2010-05-03 at 11-58-32.jpg
------ 2010-05-03 at 11-59-32.jpg
------ 2010-05-03 at 12-58-32.jpg
---- 2010-07-04
------ 2010-07-04 at 11-58-32.jpg
------ 2010-07-04 at 12-01-32.mov
-- Folder 2
---- 2090-03-03
------ 2010-03-03 at 10-01-32.jpg
------ 2010-03-03 at 11-01-32.jpg

My current version of the code includes the touch command and I go recursive through the folder structure, but this seems to get stuck after the first time I reach the lowest folder path.

for dir in Pictures/*; do
    if [[ -d "$dir" ]]; then
    cd "$dir"
        for dir2 in *; do
            if [[ -d "$dir2" ]]; then
            cd "$dir2"
            for f in *; do
                t=$(echo $f | sed -E 's/([A-z]*-)|([A-z ,;])|(\..*)//g' | sed -E 's/(.*)(..)/\1.\2/')
                touch -t $t "$f"
            cd -
    cd -

I also have thought about the find command, but I am not sure how to then extract the date and time from the filename for the touch command.


I am not directly answering your question, but you may want to consider another approach. Most cameras record creation date within the file in Exif data.

There are many ways of accessing this, but there is a command line tool jhead http://www.sentex.net/~mwandel/jhead/ This works on many platforms, and has the option to extract Exif times and perform modifications based on these.

I have an Automator Workflow (using jhead) to do this for selected items Workflow (I would have pasted the workflow, but can't seem to do so.)

  • Thanks, good idea. That does work for the majority of the pictures (except some old ones) but it wouldn't work for the video files. Therefore I will need to stick with the file name ... – hausii Feb 9 '15 at 8:52

You must log in to answer this question.

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