Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: copying recursively and pruning  (Read 3816 times)

0 Members and 1 Guest are viewing this topic.

marksosson

  • Guest
copying recursively and pruning
« on: February 05, 2008, 08:41:28 AM »

hello all,
Ive got a simple question, but i havent found the simple answer, or any answer for that matter. 

I'd like to copy a hierarchy of files, but with a catch:  i only want the .txt files.  cp has the recursive
option -r which copies everything

Code: [Select]
cp -r foo/* bar
but i just want the .txt files.  i thought it might work to do this:
Code: [Select]
cp -r foo/*.txt barbut this doesnt recurse (obviously since my folders dont end with .txt) 

What can i do?

Thanks in advance!

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: copying recursively and pruning
« Reply #1 on: February 07, 2008, 06:01:12 AM »
Have a look at find (man find).  The -exec option should help.  Also have a look at xargs.  This is taken from my man pages:

Code: [Select]
EXAMPLES
       find /tmp -name core -type f -print | xargs /bin/rm -f

       Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any filenames
       containing newlines or spaces.

       find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

       Find files named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory  names
       containing spaces or newlines are correctly handled.

Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos

ghostdog74



    Specialist

    Thanked: 27
    Re: copying recursively and pruning
    « Reply #2 on: February 07, 2008, 08:12:58 AM »
    Code: [Select]
    cp -R *.txt  destination