Solved

file copy

Posted on 2003-12-10
5
455 Views
Last Modified: 2010-04-21
does anyone know of a nifty way to copy a directory with the directory structure intact, but only copy files with a certain mask (eg. EXP as last 3 letters of files).
ideally a shell script or a perl script.  
0
Comment
Question by:MitchBroadhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 50 total points
ID: 9916372
find /source/dir -name "*.EXP" -print | xargs tar cvf - | ( cd /target/dir ; tar xf - )

Replace /source/dir with our source directory, *.EXP is the reg. expression for your file name, and /target/dir is your target directory.
0
 
LVL 1

Author Comment

by:MitchBroadhead
ID: 9916460
sounds like the kind of command i am looking for.
would that still work (recursively) if the files were distributed in multiple directories?
e.g. two files: /src/a/file1.EXP and /src/b/file2.EXP need to go to /tgt/a/file1.EXP and /tgt/b/file2.EXP respectively
that is what i meant by directory structure intact - not sure if i expressed it very well :)
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9916785
That's the beauty of using tar, it's copying the directory structure and only the files that you specify with the find command.
In the case you've specified, I would first create /tgt, then cd into /src and issue the command
find . -name "*.EXP" -print | xargs tar cvf - | ( cd /tgt ; tar xf - )


BTW: My favourite command is
cd /source/dir ; tar cvf - . | ( cd /target ; tar xf - )
This copies the directory structure in /source/dir to /target, with all permissions intact, all links intact and so on.
0
 
LVL 1

Author Comment

by:MitchBroadhead
ID: 9919695
oops!
i know i just awarded the points, but there is one more thing...

if a directory has a space in it like: MY WINDOZE, it has a problem 'stat' ing.
is there any way to enclose all file names in quotes when printing the directory list?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 9919819
Try this:

find . -name "*.EXP" -print0 | xargs -0 tar cvf - | ( cd /tgt ; tar xf - )

The new parameter to xargs is "minus zero". This does only work with the GNU versions of find an xargs, and will use a NULL character to separate file names instead of spaces.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question