LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Using mv to move the contents of one directory into another (https://www.linuxquestions.org/questions/linux-software-2/using-mv-to-move-the-contents-of-one-directory-into-another-444738/)

apachedude 05-14-2006 02:23 PM

Using mv to move the contents of one directory into another
 
Is it possible to use mv to merge two directory structures together? From the root folder of each of these structures, there are several subfolders and perhaps subfolders within those folders (and obviously files in those folders). I'd like to be able to use mv to move all the contents of one source root folder into a destination root folder without overwriting anything from the destination.

I know rsync can do something like this, but I was wondering if mv could too.

meng 05-14-2006 02:29 PM

Edited (bogus reply).

lnxconvrt 05-14-2006 03:54 PM

Options
 
Might be easier to do a cp -r, which will do a recursive copy. If you use the -i flag it will prompt before overwriting. You would then have to manually delete the source if you want it removed.

There is a -u flag which the man page says does this: "copy only when the SOURCE file is newer than the destination file or when the destination file is missing".

You can probably also use find with the "-exec" option to do a mv -i or mv -u instead of cp.

maxfacta 05-14-2006 09:10 PM

# mv -i --reply=no SOURCE/ DEST/

This will put SOURCE into DEST, perhaps you want the contents of SOURCE:
# mv -i --reply=no SOURCE/* DEST

apachedude 05-14-2006 11:51 PM

Thanks, lnxcovrt, that worked fine. I might just write a script to delete later.

maxfacta, I tried your switches, but it didn't seem to work for me. It told me it couldn't overwrite the folder, which seemed to indicate to me that it wasn't doing a "recursive" move of the files.

ayteebee 05-15-2006 02:00 AM

Quote:

It told me it couldn't overwrite the folder, which seemed to indicate to me that it wasn't doing a "recursive" move of the files.
You could try

#mv -dR SOURCE DESTINATION

along with the stuff to not overwrite if the files are newer etc. This should tell the mv command to also copy directories and to do it recursively.

You might like to check the man page first though, I might be getting confused with rm -dR .

#man mv

anupamsr 05-15-2006 02:11 AM

Well, when I have to merge directory A into B, I rather make a tar ball of A and untar it in B :)

sdjf 05-15-2006 02:21 AM

If you decide to use the copy command, don't forget to use the -p option if you want to main the original dates of the files.

Cheers,

sdjf

maxfacta 05-15-2006 02:26 AM

yeah you're dead right. mv balks at overwriting an existing directory.
The concept of 'recursive' with mv is misleading; mv doesn't actually move any data around on the storage medium, it simply updates the inodes (references to where the data is) in the appropriate parent directories.

# rsync -a SOURCE/ DEST/ --remove-sent-files --ignore-existing --whole-file

might be what you're after - move the contents of one directory into another, but do not touch any existing files.
--whole-file is useful because you are not doing any updating of files, only moving new files.

bgurley 05-15-2006 10:06 AM

I found this little trick using tar years ago in an early revision of the O'Reilly book "Running Linux":

First cd to the source directory, then do this:

tar cf - . |(cd /targetdir; tar xvf -)

This is an amazing command: All it once, it creates a tar to standard input, then changes to the target directory and un-tars it on-the-fly. Since it is tar, it maintains all permissions and timestamps, etc. Any existing files in the target would not be affected unless they had the same names, in which case they would be overwritten. But you could tweak the tar switches to change that behavior. I use this all the time. Take care of the syntax: that's a "dash" or minus character after the 'cf', and then the dot character, for current directory. then the pipe character, etc.

mkirc 05-16-2006 12:11 AM

mv for dir consolidation
 
Hey, I would do the following:
1) mkdir /newdir
2) mv -r srcdir/* newdir
3) mv -r destdir/* newdir
4) mv newdir destdir

Does this help ?

anupamsr 05-17-2006 09:21 AM

Btw, you can aswell use konqueror, if you want!

maxfacta 05-17-2006 10:22 PM

Quote:

Originally Posted by mkirc
Hey, I would do the following:
1) mkdir /newdir
2) mv -r srcdir/* newdir
3) mv -r destdir/* newdir
4) mv newdir destdir

Does this help ?


Did you even try this, or read the man page?
There is no such -r option for mv.

The concept of a 'recursive mv' belies what mv is actually doing.

euchrid9 01-16-2009 08:57 AM

Yes, this is a long time after the original post, but if anyone else is searching for a suitable answer, this worked best for me:

Code:

for direct in `find -name '*' -printf '%h\n' > TEMPFILE.txt; cat TEMPFILE.txt |  sort -u`; do curdirect=`pwd`; cd "$direct"; mv -t "DESTINATION DIRECTORY" *; cd "$curdirect"; done
You run the above code from within the directory which contains the subfolders you wish to move. If you are moving the subfolders up into the directory you are working from (say, '/home/user/photos/holidays' to '/home/user/photos'), you can replace 'DESTINATION DIRECTORY' with the variable $curdirect.

This method avoids the problem of copying an entire directory's subfolders and potentially taking up too much disk space.

The other thing is, you can change the asterisk in the 'find' command to search for specific files, such as *.jpg, *.pdf, whatever. Hope this helps.


All times are GMT -5. The time now is 01:06 AM.