MacOS – Shell script: Move folders + preserve structure

bashcommand lineerrorfoldersmacos

My goal

I organize my projects into folders. These folders all live in my Completed projects folder. I am beginning to archive them into an Archived Projects folder, and I want to archive them in sub-folders by client. I always use the client name in the project folder name. For example, I have project folders named Appeal [May 2014] - Development and Identity Package [2014] - Communications. (My internal clients are department names: Academics, Communications, Development…) Here's how I want them to be archived:

Archived Projects >> Development >> Appeal [May 2014] - Development
Archived Projects >> Communications >> Identity Package [2014] - Communications

Each project folder also has a unique set of sub-folders. I want to preserve that structure.

My approach

I have been selecting one client at a time, using this script:

#!/bin/bash
# Go to the Completed Projects folder.

cd /Volumes/communications/Projects/Completed\ Projects/

# Find a folder with a specified string (e.g. "Academics") in its name.
# Move (not copy) the folder to its corresponding sub-folder of the Archived Projects folder. (e.g. /Academics)

find . -type d | while read folder ; do
if [[ "$folder" = *Academics* ]]; then
    echo "Moving $folder";
    mv "$folder" /Volumes/communications/Projects/Archived\ Projects/Academics/;
fi
done

I have been calling the script like by navigating, in the Terminal, to the directory with my scripts by typing cd ~/Dropbox/scripts./move_project_academics.sh

My problem

The rudimentary pattern-matching works, but mv is throwing an error:

mv: rename /Appeal [May 2014] - Development/SUB1/SUB2 to
/dest/Archived Projects/Development/SUB2: No such file or directory.

Clearly I'm giving it the wrong set of instructions. (Although, oddly enough, this script has worked some of the time.) SUB2 does not belong in /dest/Archived Projects/Development/ at all. SUB2 should be nested inside of SUB1, which should be nested inside of the project folder.

My questions

What shell command would move a folder and preserve its internal structure? And how might it select a folder by client name?


By request, here is the entire output printed to the Terminal:

n$ ./move_project_academics.sh
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry -
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - to /Volumes/communications/Projects/Archived Projects/Academics/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry -: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Contracts
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Contracts to /Volumes/communications/Projects/Archived Projects/Academics/Contracts: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Correspondence
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Correspondence to /Volumes/communications/Projects/Archived Projects/Academics/Correspondence: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Invoices
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Invoices to /Volumes/communications/Projects/Archived Projects/Academics/Invoices: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Planning Documents
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Planning Documents to /Volumes/communications/Projects/Archived Projects/Academics/Planning Documents: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Quotes
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Documentation/Quotes to /Volumes/communications/Projects/Archived Projects/Academics/Quotes: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry to /Volumes/communications/Projects/Archived Projects/Academics/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Audio Files
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Audio Files to /Volumes/communications/Projects/Archived Projects/Academics/Audio Files: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Fade Files
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Fade Files to /Volumes/communications/Projects/Archived Projects/Academics/Fade Files: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Session File Backups
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Drafts/[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry/Session File Backups to /Volumes/communications/Projects/Archived Projects/Academics/Session File Backups: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Final
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/Copy
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/Copy to /Volumes/communications/Projects/Archived Projects/Academics/Copy: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/Design
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/Design to /Volumes/communications/Projects/Archived Projects/Academics/Design: No such file or directory
Moving ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/RAW
mv: rename ./[2013.04.05] - Audio - Academics - Course - Stephanie Hubach - Disability Ministry - /Source Files/RAW to /Volumes/communications/Projects/Archived Projects/Academics/RAW: No such file or directory

Best Answer

Assuming that all the folders to be archived are directly in .../Completed Projects, the use of find (which traverses the whole folder hierarchy) might actually do more harm than good.

A revised version could look like

#!/bin/bash

# Go to the Completed Projects folder.
cd /Volumes/communications/Projects/Completed\ Projects/

# Find a folder with a specified string (e.g. "Academics") in its name.
# Move (not copy) the folder to its corresponding sub-folder of the Archived Projects folder. (e.g. /Academics)

for folder in *; do
    if [[ -d "$folder" ]]; then
        echo "Moving $folder";
        if [[ "$folder" == *Academics* ]]; then
            mv "$folder" /Volumes/communications/Projects/Archived\ Projects/Academics/
        fi
    fi
done