Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

file copy

Posted on 2003-12-10
5
Medium Priority
?
463 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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

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