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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

moving files/folders

Status
Not open for further replies.

nix45

MIS
Nov 21, 2002
478
US
I'm drawing a blank right now and am looking for an answer to a simple question...

How do you move folders to a new location with the same name, combining the contects of each folder into one?

For example, lets say you had this directory structure with files inside each folder....

/cd1/RedHat/RPMS/
/cd1/base/

/cd2/RedHat/RPMS/
/cd2/base/

How would you move everything under /cd2/ to /cd1/, including any folders, subfolders, and files in one command? You could do this with mv, but you'll need a separate command for every single folder like this....

mv /cd2/RedHat/RPMS/* /cd1/RedHat/RPMS/
mv /cd2/base/* /cd1/base/

You can't just do this...

[root@tux tmp]# mv cd2/* cd1/
mv: cannot overwrite directory `cd1/base'
mv: cannot overwrite directory `cd1/RedHat'

I could use 'cp -a cd2/* /cd1/' and then delete cd2/, but I'm looking for a way to do this with a single command.

Thanks
 
try:

mv cd1 /newlocation/.
mv cd2 /newlocation2/.

this will move the contents of the cd1 folder into /newlocation/cd1 and cd2 into /newlocation2/cd2 ...

You may need to create the newlocation and newlocation2 folders...

SCOTT
 
No, thats not what I'm asking. I need to move the contents of a folder into another folder with subfolders of the same name in each. Take a look at the directory structure...

[root@tux tmp]# ls -lR cd*
cd1:
total 8
drwxr-xr-x 2 root root 4096 Nov 21 09:22 base
drwxr-xr-x 3 root root 4096 Nov 21 09:21 RedHat

cd1/base:
total 0
-rw-r--r-- 1 root root 0 Nov 21 09:22 base1
-rw-r--r-- 1 root root 0 Nov 21 09:22 base1b

cd1/RedHat:
total 4
drwxr-xr-x 2 root root 4096 Nov 21 09:22 RPMS

cd1/RedHat/RPMS:
total 0
-rw-r--r-- 1 root root 0 Nov 21 09:22 cd1.rpm

cd2:
total 8
drwxr-xr-x 2 root root 4096 Nov 21 09:21 base
drwxr-xr-x 3 root root 4096 Nov 21 09:21 RedHat

cd2/base:
total 0
-rw-r--r-- 1 root root 0 Nov 21 09:21 base2
-rw-r--r-- 1 root root 0 Nov 21 09:21 base2b

cd2/RedHat:
total 4
drwxr-xr-x 2 root root 4096 Nov 21 09:21 RPMS

cd2/RedHat/RPMS:
total 0
-rw-r--r-- 1 root root 0 Nov 21 09:21 cd2.rpm


I could do it the following way, but would rather not...

[root@tux temp]# cp -a cd2/* cd1/
[root@tux temp]# rm -fr cd2/
[root@tux temp]# ls -lR cd1/
cd1/:
total 8
drwxr-xr-x 2 root root 4096 Nov 21 10:31 base
drwxr-xr-x 3 root root 4096 Nov 21 10:31 RedHat

cd1/base:
total 0
-rw-r--r-- 1 root root 0 Nov 21 10:31 base1
-rw-r--r-- 1 root root 0 Nov 21 10:31 base1b
-rw-r--r-- 1 root root 0 Nov 21 10:31 base2
-rw-r--r-- 1 root root 0 Nov 21 10:31 base2b

cd1/RedHat:
total 4
drwxr-xr-x 2 root root 4096 Nov 21 10:31 RPMS

cd1/RedHat/RPMS:
total 0
-rw-r--r-- 1 root root 0 Nov 21 10:31 cd1.rpm
-rw-r--r-- 1 root root 0 Nov 21 10:31 cd2.rpm

 
how about:
cd /cd2
find . -print | cpio -pdmv /cd1

note: this will not overwrite anything of the same name that is newer or the same age.
 
That accomplishes the same exact thing as " cp -a cd2/* cd1/ ". I'm looking for a way to move them, not copy. Lets pretend that each directory is 500GB and we're low on disk space. Copying isn't an option in that situation, so I need to move them. Plus copying takes additional time.
 
Here is a quick but dangerous script to do what you want:
it is not pretty but should be self explanitory.
usage: mvtree /sourcedir /targetdir

#!/bin/sh
if [ x$2 = x ] ; then
exit
fi
if [ x$1 = x ] ; then
exit
fi
CWD=`pwd`
cd $1
for i in `find . -print`
do
mkdir -p $2/`dirname $i` 2> /dev/null
mv -f $i $2/`dirname $i` 2> /dev/null
done
cd $CWD
rm -rf $1
 
What I'm looking for is a single command that will move them, if one exists.

stanhubble, I did run your script just to see what it would do (in a test environment) and it deleted the first variable (source) and did nothing to the second (destination). I ran it with "yourscript ./cd2 ./cd1".

Thanks.
 
khurramriaz, I don't mean to be rude or anything, but did you even read the previous posts?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top