Skimming subfolders for files to copy

Basic tip today, this time for sifting through a directory tree and plucking out files that match a certain filter.

I needed to move some of the office photo collection off CD and USB into a different location to sort them manually. A lot of international characters and intermingled, unwanted files were complicating things.

I’m sure there are lots of ways to do this, but this is what I did.

find /media/ -iname "*jpg" -exec cp '{}' /home/kmandla/hold/ \;

Obviously you can change that to move the files, or anything else you like.

The regular cp command — or as I was trying to use it, cp -Rv /media/*jpg /home/kmandla/hold — wasn’t working with me, and my variations on that line were likewise failures.

find did the job right the first time. And was quicker than pointing and clicking, that’s for sure.

About these ads

7 Responses to “Skimming subfolders for files to copy”

  1. 1 wolfo 2011/03/21 at 9:39 PM

    My approach would have been just ‘cp -Rv /media/**/*jpg /destination’.
    /media/*jpg only substitutes files that are directly inside /media. I use zsh, but it worked with bash too.

    On the other note, I should try to learn more about ‘find’. So far, it rarely behaved the way I anticipated =)

  2. 2 Cosay Nold 2011/03/21 at 10:55 PM

    For some reason, I have never found the Find command particularly easy to learn, and I’ve used the GNU and BSD versions of the command. It’s probably because I need the command so infrequently.

  3. 3 IsaacG 2011/03/21 at 11:16 PM

    For the ** to work in bash, you first need: shopt -s globstar

  4. 4 Jose Catre-Vandis 2011/03/21 at 11:49 PM

    I use a similar command:

    find /pathtoinputdir/ -name “*.*” -type f -exec cp -urvp ‘{}’ /pathtooutputdir \;

    • 5 IsaacG 2011/03/22 at 12:02 AM

      Two more comments on the find syntax:
      – The {} don’t need quoting
      – With GNU find you can replace the \; with + to pass many filenames to one cp command opposed to fork()ing a cp for each file

    • 6 Gabe 2011/03/23 at 3:35 AM

      Also “*.*” is unnecessary (reminds me of DOS syntax), “*” will do.

      • 7 Kevin 2011/03/23 at 6:51 AM

        Actually, “*.*” and “*” are different. “*.*” only matches files with a dot in the name. And, if you are going to do -name “*”, you may as well leave the -name predicate out all together:

        find /pathtoinputdir -type f -exec cp -urvp {} \;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Visit the Wiki!

Some recent desktops

May 6, 2011
Musca 0.9.24 on Crux Linux
150Mhz Pentium 96Mb 8Gb CF

May 14, 2011
IceWM 1.2.37 and Arch Linux
L2300 core duo 3Gb 320Gb

Some recent games

Apr. 21, 2011
Oolite on Xubuntu 11.04
L2300 core duo 3Gb 320Gb

Enter your email address to subscribe to this blog and receive notifications of new posts.

Join 405 other followers


This work is licensed under the GNU Free Documentation License. Please see the About page for details.

Blog Stats

  • 3,963,682 hits



Get every new post delivered to your Inbox.

Join 405 other followers

%d bloggers like this: