Home » Questions » Computers [ Ask a new question ]

Moving files on linux, appending existing directories in destination

Moving files on linux, appending existing directories in destination

So let's assume I have:

Asked by: Guest | Views: 85
Total answers/comments: 2
Guest [Entry]

"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."
Guest [Entry]

"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."