2

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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

3 Answers3

3

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}/
        done
    done
done
DopeGhoti
  • 76,081
1

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

#!/bin/bash
for file in ????-??-??_*.{nef,xmp}
do
    yyyymm=${file/-??_*}
    echo mkdir -p "$yyyymm"
    echo mv -f "$file" "$yyyymm/"
done

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
0

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!

kemotep
  • 5,280
  • 7
  • 21
  • 36