Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
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.


  Search this Thread
Old 06-22-2006, 04:01 AM   #1
Registered: Mar 2004
Posts: 32

Rep: Reputation: 15
copy specific files with directory stucture


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?

-- Rincewind
"If you run away, you live to run away another day..."
Old 06-22-2006, 07:58 AM   #2
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
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


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).


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-07-2013 11:34 PM
How do you copy hidden files from one directory to another? Akhran Linux - Newbie 19 12-29-2011 03:31 AM
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
How to do recursive file copy of directory for specific files? Arodef Linux - Newbie 4 06-29-2004 05:35 PM > Forums > Linux Forums > Linux - Software

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