Solved

file copy

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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 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…
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.
Suggested Courses

624 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