LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-14-2006, 02:23 PM   #1
apachedude
Member
 
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356

Rep: Reputation: 31
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-14-2006, 02:29 PM   #2
meng
Member
 
Registered: Apr 2005
Location: Rochester, MN
Distribution: Ubuntu 7.04
Posts: 127

Rep: Reputation: 15
Edited (bogus reply).

Last edited by meng; 05-14-2006 at 02:41 PM. Reason: bogus
 
Old 05-14-2006, 03:54 PM   #3
lnxconvrt
Member
 
Registered: Mar 2002
Location: Houston
Distribution: FC3, Manrake 10.x, various others at times
Posts: 113

Rep: Reputation: 18
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.
 
3 members found this post helpful.
Old 05-14-2006, 09:10 PM   #4
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 21
# 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
 
2 members found this post helpful.
Old 05-14-2006, 11:51 PM   #5
apachedude
Member
 
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356

Original Poster
Rep: Reputation: 31
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.
 
1 members found this post helpful.
Old 05-15-2006, 02:00 AM   #6
ayteebee
Member
 
Registered: Jul 2005
Location: Derbyshire
Distribution: Originally Suse 9.1 Professional, currently Knoppix 3.7, migrating to Slackware
Posts: 75

Rep: Reputation: 16
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
 
1 members found this post helpful.
Old 05-15-2006, 02:11 AM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Well, when I have to merge directory A into B, I rather make a tar ball of A and untar it in B
 
Old 05-15-2006, 02:21 AM   #8
sdjf
LQ Newbie
 
Registered: Dec 2005
Location: Berkeley CA USA
Distribution: Qtopia 1.5.0, Embedix Plus PDA Platform, Sharp ROM 2.38
Posts: 4

Rep: Reputation: 0
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
 
Old 05-15-2006, 02:26 AM   #9
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 21
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.
 
4 members found this post helpful.
Old 05-15-2006, 10:06 AM   #10
bgurley
LQ Newbie
 
Registered: Mar 2004
Location: Knoxville, TN
Posts: 1

Rep: Reputation: 1
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.
 
Old 05-16-2006, 12:11 AM   #11
mkirc
Member
 
Registered: Apr 2006
Location: Vienna-Austria
Distribution: Suse 10.x, Fedora, DSL
Posts: 63

Rep: Reputation: 15
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 ?
 
Old 05-17-2006, 09:21 AM   #12
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 370

Rep: Reputation: 30
Btw, you can aswell use konqueror, if you want!
 
Old 05-17-2006, 10:22 PM   #13
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66

Rep: Reputation: 21
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.
 
Old 01-16-2009, 08:57 AM   #14
euchrid9
LQ Newbie
 
Registered: Jan 2009
Location: UK
Distribution: Ubuntu Studio
Posts: 1

Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Move contents of one disk to another and back again spinner_0 Slackware 4 01-20-2006 11:41 AM
Directory contents in / wordlife Linux - General 2 12-11-2005 02:30 AM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 03:04 AM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 07:03 PM
Get directory contents vank Programming 6 10-19-2002 10:13 AM


All times are GMT -5. The time now is 03:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration