Share your knowledge at the LQ Wiki.
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, 12: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, 12:30 PM   #2
LQ Guru
Registered: Jan 2001
Posts: 24,149

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

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

Rep: Reputation: 61
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, 05: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, 05:35 PM   #5
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
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
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 22 12-13-2019 05:20 PM
Delete/move/copy files of specific date imsajjadali Red Hat 26 11-07-2013 11:34 PM
Remove ALL files from specific Directory bianchi Programming 9 11-23-2005 11:27 PM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
recursive file permissions does not change new files in same directory PAB Linux - Newbie 2 03-08-2004 12:27 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:42 PM.

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
Open Source Consulting | Domain Registration