On Linux (Debian, Ubuntu Mint...),
Is there any option command or something that I can use to transfer files to another user without having to do :

sudo mv /home/poney/folderfulloffiles /home/unicorn/
sudo chown -R unicorn:unicorn /home/unicorn/folderfulloffiles

Use rsync(1):

rsync \
  --remove-source-files \
  --chown=unicorn:unicorn \
    /home/poney/folderfulloffiles /home/unicorn/
  • 4
    Thanks @dawud - this is my daily "stuff I didn't know and can't understand how I missed it" – Jenny D Apr 15 '14 at 10:04
  • 1
    @JennyD you might want to take a look at the usermap and groupmap options as well. – dawud Apr 15 '14 at 10:09
  • 2
    But this doesn't mv it, right? Only copies? Or does it mv it? – mikeserv Apr 15 '14 at 11:00
  • @mikeserv duly noted, see my edit – dawud Apr 15 '14 at 11:03

Per @Kevin in the comments below, the --file - |pipe syntax is redundant. So I've removed it.

This can also be done with tar:

sudo tar -C${SRC_DIR} --remove-files --group=unicorn --owner=unicorn -c ./* | 
    sudo tar -C${TGT_DIR} -pvx
  • Pretty sure the -f - is implied on both ends. – Kevin Apr 15 '14 at 21:09
  • 1
    @Kevin Not here. Here it's specified. – mikeserv Apr 15 '14 at 21:16
  • Yes, you specified it, but it's not necessary. Your command works fine without the f - part. – Kevin Apr 15 '14 at 21:31
s=/home/poney/; f=folderfulloffiles; d=/home/unicorn/ 
sudo mv $s$f $d && sudo chown -R unicorn:unicorn $d$f

About the same length as the other answers, and note since they're all using the same library calls under the hood, they're all doing exactly the same thing -- unless, as Gilles notes, this is on the same filesystem and device, in which case mv is really a rename, which makes it more efficient than rsync or tar.

  • It isn't a : instead of a . when dealing with chown ? – Kiwy Apr 15 '14 at 12:54
  • 2
    Hmmm -- interesting. It's that way in the man page, but I've always used a dot. Looks like they took it out of the GNU man page about a decade ago because it's not POSIX portable. Still works though (with the chown from GNU coreutils on linux), but I'll change that above. – goldilocks Apr 15 '14 at 13:03
  • 1
    chown typically takes both : and .. – slm Apr 15 '14 at 13:07
  • 1
    You could do it a little shorter: nu=unicorn h=/home f=folderfulloffiles ; sudo mv $h/poney/$f $h/$nu/$f ; sudo chown -R ${nu}:$nu $_ - though that's hardly the point of your answer, which is good and I've already upvoted. – mikeserv Apr 15 '14 at 13:13
  • 3
    This solution has the advantage that if the source and the destination are on the same filesystem, the file is moved rather than copied and the original erased. – Gilles 'SO- stop being evil' Apr 15 '14 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.