LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 06-29-2004, 01:27 PM   #1
Arodef
Member
 
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
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

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

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

Rep: Reputation: 56
Quote:
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
Code:
#!/bin/bash
cd /home/images
find . -depth -type f -name '*.jpg' | cpio --pass-through \
 --preserve-modification-time \
 --make-directories --verbose /home/new_dir
Example 2
Code:
#!/bin/bash
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
Arodef
Member
 
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
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,004
Blog Entries: 11

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


Cheers,
Tink
 
  


Reply


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 12:12 AM.

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