Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So let's assume I have:

dest/dir/file1
dest/dir/file2
dest/dir/subdir/subfile1
dest/dir/subdir/subfile2

and

src/dir/file3
src/dir/subdir/subfile3
src/dir/newdir/anotherfile

... and let's assume there are thousands of files and subdirectories.

Now I want to move all the new files from src to dest, to their respective directories, creating new subdirectories when needed. This would be the equivalent of "cp -a src/dir dest/", but as a move operation. The obvious command "mv src/dir dest/" fails as the directory already exists in the destination.

How can I do this with one command?

share|improve this question
add comment

5 Answers 5

up vote 6 down vote accepted

You can use rsync.

rsync -av --remove-source-files src/ dest/

Unfortunately that won't remove the directories though. You could just add a further command to remove them.

share|improve this answer
add comment

While the rsync option is better for simply moving files, here is another approach; using find to repeatedly invoke mv:

# First, move directories that don't exist on the destination:
(cd src/dir ; find * -type d -print) | while read f; do
   [ ! -d "src/dir/$f" ] || [ -d "dest/dir/$f" ] || mv "src/dir/$f" "dest/dir/$f"
done
# Then move individual files:
(cd src/dir ; find * -type f -print0) | xargs -0 -n1 -i@ /bin/mv src/dir/@ dest/dir/@

# Alternate to the xargs usage, to allow prompting before overwriting files:
(cd src/dir ; find * -type f) | while read f; do
    /bin/mv -i "src/dir/$f" "dest/dir/$f" </dev/tty
done

Note that:

  • _find * will skip any files/directories that start with a dot (".").
  • this will not remove the directories, either.

I use something along these lines, by nightly cron, to create any thumbnails of new images in my personal pictures archive, then use rsync to make a backup of all images on a 2nd machine.

share|improve this answer
add comment

It's not cp -a, the -a switch is the archive switch.

It should be cp -R, the recursive switch.

You can also add the -f switch to force overwrite.

share|improve this answer
add comment

As far as I know a recursive mv is not possible. I would suggest to perform a cp -R followed by a rm -r.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2014 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2014.7.22.1721\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Super User works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscrip"], "copyright__copyright__copyright": ["tch is the archive switch.\n\nIt should be cp -R, the recursive switch.\n\nYou can also add the -f switch to force overwrite.\n\nshare|improve this answer\nadd comment\n\nAs far as I know a recursive mv is not possible. I would suggest to perform a cp -R followed by a rm -r.\n\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}