2 votes

Shell script : Déplacer les dossiers + préserver la structure

Mon objectif

J'organise mes projets en dossiers. Ces dossiers se trouvent tous dans mon Completed projects dossier. Je commence à les archiver dans un Archived Projects et je veux les archiver dans des sous-dossiers par client. J'utilise toujours le nom du client dans le nom du dossier du projet. Par exemple, j'ai des dossiers de projet nommés Appeal [May 2014] - Development et Identity Package [2014] - Communications . (Mes clients internes sont les noms des départements : Universitaires, Communication, Développement...) Voici comment je souhaite qu'ils soient archivés :

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

Chaque dossier de projet possède également un ensemble unique de sous-dossiers. Je souhaite préserver cette structure.

Mon approche

J'ai sélectionné un client à la fois, en utilisant ce 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

J'ai appelé le script comme en naviguant, dans le Terminal, vers le répertoire contenant mes script en tapant cd ~/Dropbox/scripts ./move_project_academics.sh

Mon problème

La recherche rudimentaire de motifs fonctionne, mais mv génère une erreur :

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

Il est clair que je ne lui donne pas les bonnes instructions. (Bien que, curieusement, ce script ait fonctionné une partie du temps .) SUB2 n'appartient pas à /dest/Archived Projects/Development/ du tout. SUB2 doit être imbriqué dans SUB1, qui doit être imbriqué dans le dossier du projet.

Mes questions

Quelle commande shell permet de déplacer un dossier en préservant sa structure interne ? Et comment sélectionner un dossier par nom de client ?


Sur demande, voici l'intégralité de la sortie imprimée sur le 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

1voto

nohillside Points 82672

En supposant que tous les dossiers à archiver se trouvent directement dans le dossier .../Completed Projects l'utilisation de find (qui traverse toute la hiérarchie des dossiers) pourrait en fait faire plus de mal que de bien.

Une version révisée pourrait ressembler à

#!/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

LesApples.com

LesApples est une communauté de Apple où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres utilisateurs d'appareils Apple, poser vos propres questions ou résoudre celles des autres.

Powered by:

X