Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-29-2004, 01:27 PM   #1
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Rep: Reputation: 17
How to do recursive file copy of directory for specific files?

Hey guys,
Basically I want to copy a directory and the directories underneath but only copy the files in those dirs that end with a certain extension. What's the unix command to do this? Thanks!
Old 06-29-2004, 01:30 PM   #2
LQ Guru
Registered: Jan 2001
Posts: 24,147

Rep: Reputation: 229Reputation: 229Reputation: 229
man cp tells you everything..

cp -R /directory/you/want/to/copy/from/*.ext /new/destination
Old 06-29-2004, 05:38 PM   #3
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 58
cp -R /directory/you/want/to/copy/from/*.ext /new/destination
Guess I'm missing something here because that ain't working for me.
The part that acts goofy is selecting just some file types. It works ok to copy everything like this...
cp -r /home/images/* /home/new_dir
But if I try to select just the jpg files, it only copies the jpg files which are in the /home/images and not the sub directories or files. For example...
cp -r /home/images/*.jpg /home/new_dir

I did find a couple of work arounds but I would like to know what I'm missing on the original cp -r command.

Example 1
cd /home/images
find . -depth -type f -name '*.jpg' | cpio --pass-through \
 --preserve-modification-time \
 --make-directories --verbose /home/new_dir
Example 2
cd /home/images
find . -type d -name '*' -exec mkdir -p /home/new_dir/{} \;
find . -type f -name '*.jpg' -exec cp {} /home/new_dir/{} \;
1 members found this post helpful.
Old 06-29-2004, 06:12 PM   #4
Registered: Apr 2004
Distribution: Centos, Fedora
Posts: 125

Original Poster
Rep: Reputation: 17
I actually tried cp -R however that only works if you want EVERYTHING copied from that directory and the subdirectories. For specific files (such as *.jpg), how do you do it?
Old 06-29-2004, 06:35 PM   #5
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I don't think it's possible without the find ...



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-08-2013 12:34 AM
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 19 12-29-2011 04:31 AM
Remove ALL files from specific Directory bianchi Programming 9 11-24-2005 12:27 AM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 12:51 PM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 01:27 PM

All times are GMT -5. The time now is 02:56 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration