I've got thousands of raw photos (.nef) and relative settings (same filename but .xmp extension) in a single directory on a QNAP nas. My goal is to automate the creation of subdirs named like yyyy-mm or else yyyy/mm and moving there all files accordingly.
All file names are like yyyy-mm-dd_hhmmss-###.nef or yyyy-mm-dd_hhmmss-###.xmp where ### are milliseconds.


If you know the years that these files' names span, you could just brute-force it:

for year in {1996..2018}; do
    for month in {01..12}; do
        mkdir -p ${year}-${month}
        for filetype in nef xmp; do
            mv ${year}-${month}*.${filetype} ${year}-${month}/
| improve this answer | |
  • 1
    Quoting the asterisk makes this not work (and quoting the rest is useless here) – dave_thompson_085 Mar 27 '18 at 7:11

Here's a loop based solution with the restricted command set available on a standard QNAP:

for file in ????-??-??_*.{nef,xmp}
    echo mkdir -p "$yyyymm"
    echo mv -f "$file" "$yyyymm/"

Put this into a file such as /root/fixup, change to the directory containing your many files, and run bash /root/fixup. It will create the yyyy-mm directories on demand, based on the filenames it's processing.

As written, it will make no changes. When you are happy that it looks like it's going to work, remove the word echo from the two lines near the end of the script.

| improve this answer | |

If you are looking to simply mass create directories following a pattern you can use:

mkdir -p ~/{0001,0002,0003,...,XXXX}/{01,02,03,...,XX}

Make sure you substitute the variables with values you actually want to use so an example to create a sub-directory for years, months, and days would look like this:

mkdir -p ~/{1998..2018}/{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}/{01..31}

Then you can search through the months that do not have 31 days and remove them as described in this post. After that you have the task of sorting the photos into the proper directories. This is done very similar to finding and deleting and is described in this post.

Please note that this will create the sub-directories in your current working directory. As mentioned by user DopeGhoti, you can create a for loop to complete each step at once. Best of Luck!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.