1

I am trying to sort files on my NAS using the Terminal from Mac OSX. I am planning on using the following steps:

  1. create folder for specific year
  2. create folder for each month
  3. use the "Find" command to identify the year and month and move the file to relevant folder.

I have created 2 executable files for steps 1 to 2. I am still looking into setting up the 3rd file for step 3 but am running into problems.

For information:

File 1: "MakeMediaYearFolders.command" has the following code lines

#! /bin/bash
xargs -tI % mkdir % < MediaYearFolders.csv

where: "MediaYearFolders.csv" has the following lines

Media - 1999
Media - 2000
...
Media - 2020

File 2: "MakeMonthsFolder.command" has the following code lines

#! /bin/bash
cd /Users/Name/Desktop/DROBO TEST/2. SORTED/Media - 1999/
xargs -tI % mkdir % < MonthsFolders.csv

With "MonthsFolders.csv" has the following lines

01 - January
...
12 - December

File 3: currently has the following

#! /bin/bash
cp -fv /Users/name/Desktop/DROBO TEST/2. SORTED/MonthsFolders.csv /Users/name/Desktop/DROBO TEST/2. SORTED/Media - 1999
cp -fv /Users/name/Desktop/DROBO TEST/2. SORTED/MakeMonthsFolder.command /Users/name/Desktop/DROBO TEST/2. SORTED/Media - 1999
Cd /Users/name/Desktop/DROBO TEST/2. SORTED/Media - 1999
xargs -tI % mkdir % < MonthsFolders.csv

The problems I'm facing are multiple and I'm not quite sure how to tackle them:

  • Problem 1: I'm unable to create a single file for steps 1 and 2 as I am not sure how to force the creation of the "months" folders in each "media - year" folder. when I tried, the files were simply not being copied and no error message was displayed. also, the "month" folders were being created in the root folder containing the "media - Year" folders
  • Problem 2: I have to manually launch the creation of "months" folders for each "media - Year" folder. on top of things, I also have to copy the "CSV" file and executable file in each "media - Year " folder! really time consuming. I'm not quite sure how to force the creation of the "months" folders in each of the "media - Year". in fact, to create the folders, I have to launch the process using "./MakeMonthsFolder.command" from each folder.
  • Problem 3: when using the "find" command, I am unable to move the relevant files. I use the following command: find /Users/name/Desktop/DROBO\ TEST/1.\ SOURCE -name "201704*" -exec mv -v -- /Users/name/Desktop/DROBO\ TEST/1.\ SOURCE /Users/name/Desktop/DROBO\ TEST/Media\ -\ 2017/04\ -\ April. I get the following message from the system: find: -exec: no terminating ";" or "+". I'm not quite sure how to interpret this let alone act upon it ;-)

Cherry on top of the cake: How do I get the mail for the message "You have new mail in /var/mail/name" ? basically I guess my question is how do I get there ?

Any help, suggestions ?

Thanks all

1 Answer 1

0

This shell script, although not much efficient, achieves steps 1 and 2:

#!/bin/sh
while read -r year; do
    if [ -n "$year" ]; then
        mkdir "$year"
    else
        continue
    fi
    while read -r month; do
        if [ -n "$month" ]; then
            mkdir "$year/$month"
        else
            continue
        fi
    done < MonthsFolders.csv
done < MediaYearFolders.csv

It simply reads each line from MediaYearFolders.csv, makes a directory with that name and then make directories for the months inside it reading from MonthsFolders.csv. The if-clauses guarantee that neither year nor month variables are empty when making directories.

find syntax can be a bit tricky. The correct way of writing it is

find /Users/name/Desktop/DROBO\ TEST/1.\ SOURCE -name "201704*" -exec mv -v -- {} /Users/name/Desktop/DROBO\ TEST/Media\ -\ 2017/04\ -\ April \;

As you see, each found file goes in {} and the -exec bit is terminated by \;. See man find for more information.

Beware: You need to protect strings that contain spaces in quotes or escape the space characters:

cd /Users/.../DROBO TESTcd "/Users/.../DROBO TEST"

cd /Users/.../DROBO TESTcd /Users/.../DROBO\ TEST

4
  • Oh Dear, I think i just had tears of joy when seeing the results for the folder creation script. Why did i not learn scripting rather than a 3rd language ?! Thanks ever so much for the support. i will now try to find a way of using the "FIND .../... -name" set of commands to read the name of the files and send the file to the relevant directory... Wish me luck. Commented Feb 23, 2020 at 23:01
  • Thanks for the EDITORS who reformatted my original text. Looks much better and also makes much more sense Commented Feb 24, 2020 at 9:30
  • Glad to help! If you can't sort it out, I think your third step (with find) deserves a question in its own right. IMO it's very advisable to describe the whole problem, as you did, but tackling it in one single answer may be a daunting task. And don't worry, you will gradually improve your shell witchcraft. Good luck ;)
    – user596332
    Commented Feb 24, 2020 at 10:54
  • Thanks for the support Quasimodo. will follow your advice and present the FIND issue as a request for support or entirely new question. Commented Feb 25, 2020 at 11:33

You must log in to answer this question.

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