LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 06-22-2006, 05:01 AM   #1
rincewind
Member
 
Registered: Mar 2004
Posts: 32

Rep: Reputation: 15
copy specific files with directory stucture


Hello

Is there a way in bash to copy all .jpg (or some other) files from a directory with all of its subdirs and keep the directory structure in the copied dir?

Regardz,
-- Rincewind
"If you run away, you live to run away another day..."
 
Old 06-22-2006, 08:58 AM   #2
penguintutor
Member
 
Registered: Jun 2006
Location: UK
Distribution: Ubuntu, Mandriva, Redhat and Fedora
Posts: 118

Rep: Reputation: 15
Here is a way of achieving that, all though I admit it's cheating a little.

First change to the directory where you want the copy and then
Code:
find /home/user -name *.jpg | xargs tar -c | tar -x
This will create the full directory structure in your current directory.

So if you are copying from user1 home directory into /tmp you will end up with

/tmp/home/user1/....

you could then move the new directory to where you really want it. If you want anything more sophisticated than that you'll need to write a full shell script rather than a one-liner.

It works by finding all the files in the /home/user directory, sending the files full path+filename into the pipe. The xargs means that it will run the command once for every file and then run the tar command, which is then untarred into the current directory. The tar command preserves the full path (but strips the initial / so that it is a relative directory rather than an absolute one).
 
  


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
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 06:35 PM


All times are GMT -5. The time now is 04:32 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