Shell script to cycle through a directory with subdirectories of images and copy some images to diff directory based on filename

Posted on 2008-11-04
Last Modified: 2013-12-26
I have a directory "multimedia/archive" with many subdirectories named like this: "00000", "00001", ... ,"01075".
The subdirectories contains different versions of many pictures. The directory "00000" might have a file "picture1a.jpg", "picture1b.jpg", "picture1c.jpg" and so on. As well as "picture2a.jpg", "picture2b.jpg", "picture2c.jpg" and so on.

I would like to keep my original multimedia/archive as it is, but make a copy of all my *a.jpg files to another main directory (e.g multimedia/archive2) with the same subdirectory structure...

Hence I want my files like this:

How do I do this with a shell script?

I have seen this article:, but I'm not able to make the correct adjustment...

Question by:kvaade
    LVL 19

    Expert Comment

    cp -r multimedia/archive multimedia/archive2 is the easy one if they already only contain those you wish to copy

    LVL 20

    Expert Comment

    How about :

    create the  directory structure you need in the desitantion :
    find /multimedia/archive/ -type d -exec mkdir -p /multimedia/archive2/{} \;  

    Then copy the files over :
    find /multimedia/archive/ -type f -name "*a.jpg" -exec cp -p {} /multimedia/archive2/{} \;
    LVL 40

    Accepted Solution


    cd multimedia
    find . -name "picture*a.jpg | while read file
       dir=`dirname $file`
       if [ -d archive2/$dir ]
             mkdir -p archive2/$dir
       cp $file archive2/$dir

    Author Closing Comment

    Thanx! Just what I had i mind!  :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now