LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Linux > 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
 
Thread Tools Search this Thread
Old 05-14-2006, 03:23 PM   #1
apachedude
Member
 
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356
Thanked: 0
Using mv to move the contents of one directory into another


[Log in to get rid of this advertisement]
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.
apachedude is offline     Reply With Quote
Old 05-14-2006, 03:29 PM   #2
meng
Member
 
Registered: Apr 2005
Location: Rochester, MN
Distribution: Ubuntu 7.04
Posts: 128
Thanked: 0
Edited (bogus reply).

Last edited by meng; 05-14-2006 at 03:41 PM.. Reason: bogus
meng is offline     Reply With Quote
Old 05-14-2006, 04:54 PM   #3
lnxconvrt
Member
 
Registered: Mar 2002
Location: Houston
Distribution: FC3, Manrake 10.x, various others at times
Posts: 113
Thanked: 0
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.
lnxconvrt is offline     Reply With Quote
Old 05-14-2006, 10:10 PM   #4
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Thanked: 0
# 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
maxfacta is offline     Reply With Quote
Old 05-15-2006, 12:51 AM   #5
apachedude
Member
 
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356
Thanked: 0

Original Poster
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.
apachedude is offline     Reply With Quote
Old 05-15-2006, 03:00 AM   #6
ayteebee
Member
 
Registered: Jul 2005
Location: Derbyshire
Distribution: Originally Suse 9.1 Professional, currently Knoppix 3.7, migrating to Slackware
Posts: 76
Thanked: 0
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
ayteebee is offline     Reply With Quote
Old 05-15-2006, 03:11 AM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: Dreams
Distribution: Gentoo (since 2004.3) (and Windows XP)
Posts: 360
Thanked: 0
Well, when I have to merge directory A into B, I rather make a tar ball of A and untar it in B
anupamsr is offline     Reply With Quote
Old 05-15-2006, 03: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
Thanked: 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
sdjf is offline     Reply With Quote
Old 05-15-2006, 03:26 AM   #9
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Thanked: 0
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.
maxfacta is offline     Reply With Quote
Old 05-15-2006, 11:06 AM   #10
bgurley
LQ Newbie
 
Registered: Mar 2004
Location: Knoxville, TN
Posts: 1
Thanked: 0
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.
bgurley is offline     Reply With Quote
Old 05-16-2006, 01:11 AM   #11
mkirc
Member
 
Registered: Apr 2006
Location: Vienna-Austria
Distribution: Suse 10.x, Fedora, DSL
Posts: 63
Thanked: 0
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 ?
mkirc is offline     Reply With Quote
Old 05-17-2006, 10:21 AM   #12
anupamsr
Member
 
Registered: Sep 2004
Location: Dreams
Distribution: Gentoo (since 2004.3) (and Windows XP)
Posts: 360
Thanked: 0
Btw, you can aswell use konqueror, if you want!
anupamsr is offline     Reply With Quote
Old 05-17-2006, 11:22 PM   #13
maxfacta
Member
 
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Thanked: 0
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.
maxfacta is offline     Reply With Quote
Old 01-16-2009, 09:57 AM   #14
euchrid9
LQ Newbie
 
Registered: Jan 2009
Location: UK
Distribution: Ubuntu Studio
Posts: 1
Thanked: 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.
euchrid9 is offline     Reply With Quote

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 12:41 PM
Directory contents in / wordlife Linux - General 2 12-11-2005 03:30 AM
write permissions for directory - not accidently move/deleted the directory linuxgamer Linux - Newbie 10 12-02-2003 04:04 AM
mv the contents of one directory to the parent directory warkrime Linux - Newbie 4 07-14-2003 08:03 PM
Get directory contents vank Programming 6 10-19-2002 11:13 AM


All times are GMT -5. The time now is 05:07 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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration