Solved

file copy

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Currently, there is not an RPM package available under the RHEL/Fedora/CentOS distributions that gives you a quick and easy way to allow PHP to interface with Oracle. As a result, I have included a set of instructions on how to do this with minimal …
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now