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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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).
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 at 19:29
mkdir -p
didn't fail on existing directories. Thanks for pointing this out. I corrected the answer accordingly.
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
.