Operating System - Linux
1753505 Members
4951 Online
108794 Solutions
New Discussion юеВ

Re: Move file but keep directory tree

 
SOLVED
Go to solution
Mike Ehrman
New Member

Move file but keep directory tree

I am writing a script that searches for files with specific extensions and moves them to another directory.

I'd like to be able to keep the directory tree of the file when the file is in it's new location.

Example: I'd like to move the file stuff.gz from /home/jerry to /var/tmp/home/jerry.

Any suggestions on how to do this within the structure of my already written script?

(Main part of my script is below)


find / -name \*.gz >$OUTPUT

for i in $(<$OUTPUT)
do
{insert command here}
done



Thank you for any suggestions you may have.
Mike
Today I will know the joy of uninterrupted productivity!
9 REPLIES 9
Alan Meyer_4
Respected Contributor

Re: Move file but keep directory tree

I would do a cp -rp and then remove the source file. The cp -rp would create the necessary directory structure and keep the same file protection as the source file. So the script would look like;

find / -name \*.gz >$OUTPUT

for i in $(<$OUTPUT)
do
cp -rp $i /var/tmp/$i
rm $1
done
" I may not be certified, but I am certifiable... "
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Move file but keep directory tree

Easy:

find /home/jerry -name '*.gz' | cpio -pudvm /var/tmp
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor

Re: Move file but keep directory tree

The cpio passthru option as listed by Clay.
As the root user;

# cd /home/jerry
# find . -depth | cpio -pmuldv /var/tmp/

This will keep perms and owners as well.
Mike Ehrman
New Member

Re: Move file but keep directory tree

Thank you Alan, Clay and Rick.

Alan - the cp command with the rp options did not create the directory structure on my HP-UX 11i box. The man page says p is for keeping permissions.

Clay & Rick - the cpio command works.

Here is how I incorporated it into my scripts...

find / -name \*.gz >$OUTPUT

cat $OUTPUT | cpio -dumpv /var/tmp



If anyone has a better way to incorporate it into my script I'd love to hear it.

Mike
Today I will know the joy of uninterrupted productivity!
Rick Garland
Honored Contributor

Re: Move file but keep directory tree

Not exactly sure what you are looking for but give it try:

Instead of the 2 lines you have, just have the single line as listed previously posted.

find /home/jerry -name '*.gz' | cpio -pmuldv /var/tmp


You can have just the 1 line doing the copies.

Alan Meyer_4
Respected Contributor

Re: Move file but keep directory tree

Hmm it's the -r that's suppose to create the subtrees though... In anycase, what's important is that you got it solved... :)
" I may not be certified, but I am certifiable... "
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Move file but keep directory tree

Lets's try this way out,

for filename in `find / -name \*.gz`;
do
newdir=dirname $filename
mkdir -p /var/tmp/$newdir
mv $filename /var/tmp/$newdir
done
Vibhor Kumar Agarwal
Arunvijai_4
Honored Contributor

Re: Move file but keep directory tree

Put your $OUTPUT in a loop and pass through it, It would be efficient and good way of doing.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Muthukumar_5
Honored Contributor

Re: Move file but keep directory tree

You can do it simply as,

# find / -name "*.gz" | awk '{ print "cp -R "$0" /var/tmp" }' | ksh

It will copy directory sturcture to /var/tmp. You can change location also with awk statement.

hth.
Easy to suggest when don't know about the problem!