Move files and change ownership at the sametime
24,071
Solution 1
Use rsync(1)
:
rsync \
--remove-source-files \
--chown=unicorn:unicorn \
/home/poney/folderfulloffiles /home/unicorn/
Solution 2
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
Solution 3
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
.
Related videos on Youtube
Comments
-
S edwards almost 2 years
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
-
Jenny D about 10 yearsThanks @dawud - this is my daily "stuff I didn't know and can't understand how I missed it"
-
dawud about 10 years@JennyD you might want to take a look at the
usermap
andgroupmap
options as well. -
mikeserv about 10 yearsBut this doesn't
mv
it, right? Only copies? Or does itmv
it? -
dawud about 10 years@mikeserv duly noted, see my edit
-
S edwards about 10 yearsIt isn't a
:
instead of a.
when dealing withchown
? -
goldilocks about 10 yearsHmmm -- 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.
-
slm about 10 years
chown
typically takes both:
and.
. -
S edwards about 10 years@mikeserv let's make it a codegolf :D
-
mikeserv about 10 years@Kiwy, we'd need to do a little
eval printf
for that, I think. -
Kevin about 10 yearsPretty sure the
-f -
is implied on both ends. -
mikeserv about 10 years@Kevin Not here. Here it's specified.
-
Kevin about 10 yearsYes, you specified it, but it's not necessary. Your command works fine without the
f -
part. -
Gilles 'SO- stop being evil' about 10 yearsThis 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.
-
Henk Langeveld about 10 yearsPlease 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 :-) -
S edwards about 10 years@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
-
goldilocks about 10 yearsYeah, 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. -
Henk Langeveld about 10 years@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.