Solved

file copy

Posted on 2003-12-10
5
453 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
  • 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

773 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