1

I have a directory (currdir) with 24000 images on a CentOS/cPanel server.

I want to split this directory by moving images from this directory into other directories (or sub-directories inside currdir) based on image dates.

How to make it happen?

3
  • 1
    By "image date" you mean information from EXIF or file modification timestamp?
    – rush
    Jan 23, 2016 at 16:21
  • @rush file modification timestamp
    – R.Ta
    Jan 23, 2016 at 18:39
  • Here's a solution for visitors looking to sort images and videos based on the creation date read from EXIF metadata. May 23, 2023 at 14:26

2 Answers 2

2

This will run a find command that outputs a shell command moving the file in a directory based on the last change time of the file. It will create the target on the directory on the fly (mkdir -p will not fail if directory already exists). The generated commands are then directly piped into a shell interpreter.

find . -maxdepth 1 -type f -printf "mkdir -p %CY/%Cm/%Cd && mv -- '%f' %CY/%Cm/%Cd\n" | sh

(assumes GNU find for its -printf predicate).

Replace %C with %T for the last modification time (a generally more accurate representation of the age of the contents of the file as the change time is also updated when metadata of the file is modified).

Note that this may not be the most efficient way to do it, as it will create a lot of processes to create directories ad move files. As it is for a one-shot execution, I wouldn't focus too much on this...

It assumes file names don't contain ' characters (it constitutes a command injection vulnerability if you can't guarantee it).

You can pass the -x option (or -o xtrace) to sh for it to show you what it's doing, or -e (or -o errexit) for it to exit upon the first error (though with -e, you'd want to replace the && with ; as otherwise failures of mkdir would be ignored).

2
  • 1
    Re: your edit, mkdir -p dir will succeed if the directory already exist, and only fails if the file doesn't exist as a directory when it exits, in which case you do not want to run cp, hence the && I had edited in. The -- doesn't harm but is not necessary as the file paths will all start with ./. May 25, 2023 at 19:29
  • @StéphaneChazelas Didn't ever noticed that mkdir -p didn't fail on existing directories. Thanks for pointing this out. I corrected the answer accordingly.
    – Uriel
    May 27, 2023 at 11:50
1

With exiftool:

exiftool -ext jpg '-Directory<FileModifyDate' -d %Y/%m/%d .

Would move the files with jpg extension in the current working directory to YYYY/MM/DD subdirectories (creating them as needed) based on the FileModifyDate.

exiftool -args file.jpg will show you what other timestamps relevant to the files you may use instead of FileModifyDate, (including those from the EXIF metadata stored by the camera in the JPEG file) such as FileInodeChangeDate, FileAccessDate, ModifyDate, DateTimeOriginal, CreateDate.

You must log in to answer this question.

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