30

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
Kiwy
  • 9,415
  • 13
  • 49
  • 79

3 Answers3

37

Use rsync(1):

rsync \
  --remove-source-files \
  --chown=unicorn:unicorn \
    /home/poney/folderfulloffiles /home/unicorn/
Henk Langeveld
  • 752
  • 5
  • 16
dawud
  • 2,179
  • 19
  • 20
11

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
mikeserv
  • 57,448
  • 9
  • 113
  • 229
9
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.

goldilocks
  • 86,451
  • 30
  • 200
  • 258
  • 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](https://lists.gnu.org/archive/html/bug-coreutils/2005-04/msg00097.html) 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
  • @mikeserv let's make it a codegolf :D – Kiwy Apr 15 '14 at 17:15
  • @Kiwy, we'd need to do a little `eval printf` for that, I think. – mikeserv Apr 15 '14 at 17:51
  • 4
    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
  • Please split the line and don't make us click on a scroll bar to view the code. Oh, and use `shopt -s lithist` before you copy and paste code samples from bash history. It really helps :-) – Henk Langeveld Apr 17 '14 at 08:19
  • @HenkLangeveld I disagree, spleeting the line makes less usable and copy/paste not proof, I definitely prefer to scroll, though in this case it is relevant, one liners should stay on one line – Kiwy Apr 22 '14 at 18:30
  • Yeah, I actually changed it back (all the assignments were on separate lines, I had everything on one), then I decided two lines is reasonable enough here for readability so made it that way. In real life I'd use one or two lines but probably not more than that (doing each assignment separately is more confusing, and using goofy '\' stuff is not necessary for this). @HenkLangeveld: I have no idea what you are referring to WRT `shopt -s`: I did not copy paste anything from bash history here. – goldilocks Apr 22 '14 at 18:39
  • @TAFKA'goldilocks' By default, bash does not preserve newlines in history, so most people do not bother with indentation when editing history. `shopt -s lithist` allows you to copy/paste code snippets with newlines and indentation preserved. I make it a habit to run most code snippets that I post on SE, just to catch the silly mistakes. As for `\`, in your example, there's no need for that. It looks good this way. Personally I would continue the code after `&&` with some indentation on the next line to provide a visible clue of the conditional statement. – Henk Langeveld Apr 22 '14 at 20:35