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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-14-2006, 03:23 PM
|
#1
|
Member
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356
Rep:
|
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.
|
05-14-2006, 03:29 PM
|
#2
|
Member
Registered: Apr 2005
Location: Rochester, MN
Distribution: Ubuntu 7.04
Posts: 127
Rep:
|
Edited (bogus reply).
Last edited by meng; 05-14-2006 at 03:41 PM.
Reason: bogus
|
|
|
05-14-2006, 04:54 PM
|
#3
|
Member
Registered: Mar 2002
Location: Houston
Distribution: FC3, Manrake 10.x, various others at times
Posts: 113
Rep:
|
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.
|
05-14-2006, 10:10 PM
|
#4
|
Member
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Rep:
|
# 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.
|
05-15-2006, 12:51 AM
|
#5
|
Member
Registered: Aug 2004
Location: California
Distribution: SuSE 10.0 (SUPER)
Posts: 356
Original Poster
Rep:
|
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.
|
05-15-2006, 03:00 AM
|
#6
|
Member
Registered: Jul 2005
Location: Derbyshire
Distribution: Originally Suse 9.1 Professional, currently Knoppix 3.7, migrating to Slackware
Posts: 75
Rep:
|
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.
|
05-15-2006, 03:11 AM
|
#7
|
Member
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371
Rep:
|
Well, when I have to merge directory A into B, I rather make a tar ball of A and untar it in B
|
|
|
05-15-2006, 03:21 AM
|
#8
|
LQ Newbie
Registered: Dec 2005
Location: California, United States
Distribution: Qtopia on Zaurus, ArchLinuxArm on RPi, Ubuntu variants on C.H.I.P.,
Posts: 6
Rep:
|
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
|
|
|
05-15-2006, 03:26 AM
|
#9
|
Member
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Rep:
|
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.
|
05-15-2006, 11:06 AM
|
#10
|
LQ Newbie
Registered: Mar 2004
Location: Knoxville, TN
Distribution: Ubuntu
Posts: 1
Rep:
|
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.
|
|
|
05-16-2006, 01:11 AM
|
#11
|
Member
Registered: Apr 2006
Location: Vienna-Austria
Distribution: Suse 10.x, Fedora, DSL
Posts: 63
Rep:
|
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 ?
|
|
|
05-17-2006, 10:21 AM
|
#12
|
Member
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371
Rep:
|
Btw, you can aswell use konqueror, if you want!
|
|
|
05-17-2006, 11:22 PM
|
#13
|
Member
Registered: Aug 2004
Location: Perth, Australia
Distribution: Debian @ home + work :)
Posts: 66
Rep:
|
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.
|
|
|
01-16-2009, 09:57 AM
|
#14
|
LQ Newbie
Registered: Jan 2009
Location: UK
Distribution: Ubuntu Studio
Posts: 1
Rep:
|
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 09:05 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|