Using mv to move the contents of one directory into another
Linux - SoftwareThis 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.
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.
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.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.