I have a couple of files that I want to move to another’s user home directory. I don’t have permissions to write to that user’s home directory, but I know his password.
I know how to copy the file using scp (see here). However, if I want to move the file, copying and then removing the original file is inefficient. Is there a way to move the file, without using sudo (I don’t know the root’s password)?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Subject to certain assumptions that the target user can actually access the file in its original location, the following approach could work:
SRC='/path/to/existing/file' DST='/path/to/new/file' su target_user sh -c "ln -f '$SRC' '$DST'" && rm -f "$SRC"
This “moves” the file to the new user’s location, but does not change the ownership or permissions.
Method 2
You can su to any user, if you know there password. (for sudo you need to be a sudoer, and know your own password).
So make the files readable and directory writeable(for deletion) by the other user, add files to a shared group, or use access-control-lists (ACLs) setfacl (What are the different ways to set file permissions etc on gnu/linux)
Then su other user
Then do the move.
Also look at @roaima ‘s answer for how to do it without giving write access away to receiving user.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0