Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preserving the file directory in copy(cp) 2

Status
Not open for further replies.

inunix

Technical User
Jul 30, 2002
53
US
Is it possible to preserve the file directory along wiht the file name..?

for eg:
I've few files in source dir called "srcdir"
/usr/srcdir/abc/123.xml
/usr/srcdir/abc/good/123.xml
/usr/srcdir/abc/223.xml
/usr/srcdir/abc/good/223.xml

I've to copy all the above files to a directory called 'destdir'. Finally i should get in this way..

/usr/destdir/abc/123.xml
/usr/destdir/abc/good/123.xml
/usr/destdir/abc/223.xml
/usr/destdir/abc/good/223.xml

What can i achieve this..?
 
depending on if you want all files copied you could use find and cpio.

cd /usr/srcdir
find . -print | cpio -pdmv /usr/destdir

you could then put some pattern or filter on if you don't want all files copied.
 
tar the files under srcdir and untar them in destdir, or (from srcdir) use cp -r * destdir for a recursive copy.

HTH.
 
create /usr/destdir.
cd into /usr/srcdir.
then issue this command cp -pR abc /usr/destdir

the -p saves the permissions and timestamps of the orignal files. the -R says to copy recursively -- you'll get all subdirectories in srcdir (if any).

 
Thanks for all the responses.. one thing i missed ,
I don't want all the files in the directory.. only few files that i know, say i've the list of files with full path.
 
Yeah, silly me, should have been /usr/destdir in my cp -r command. As bi says, adding the -p switch will preserve creation times and ownerships etc.
 
If you have a text file with the list of files (including path) you want to copy to the new directory:

for line in `cat list.txt`
do
cp -pR $line /usr/destdir
done
 
Thanks bi.. I already tried that.. it will copy but won't preserve the directory hierarchy.
 
As StanHubble said

cd /usr/srcdir
find . -print | cpio -pdmv /usr/destdir

would do all files so you can just change the "find . -print" with another find or with a "cat list.txt"


Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
Says you have a file called /tmp/filelist with:
Code:
/usr/srcdir/abc/123.xml
/usr/srcdir/abc/good/123.xml
/usr/srcdir/abc/223.xml
/usr/srcdir/abc/good/223.xml
You can then try something like this:
Code:
cd /usr/srcdir
sed 's!/usr/srcdir/!! /tmp/filelist |
  cpio -pdmv /usr/destdir


Hope This Help
PH.
 
Hello PH,

Your's works perfect for me. Thanks.

SSK
 
To copy only the files that have been modified since 4 days ago, with a filter for a pattern to ignore, and a log file:
find . -name '*' -mtime -4 2>/dev/null | grep -v 'pattern to ignore'||cpio -pdmv /path/to/dest/dir |2>&1 1>> copy.log

 
> How to preserve times on directories?
Code:
touch -r /path/to/dir /tmp/tmp.$$
<Your stuff here>
touch -r /tmp/tmp.$$ /path/to/dir
rm /tmp/tmp.$$

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top