3

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)?

a06e
  • 1,727

2 Answers2

4

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • 1
    +1 This does link unlink (an implementation of move), as different users. So no write access of source files is needed by receiving user. – ctrl-alt-delor Feb 19 '15 at 23:42
  • I want to move a full directory (along with its contents). I get an error: ln: ‘$SRC’: hard link not allowed for directory – a06e Jun 02 '16 at 12:17
  • This answers my original question. I asked about directories here. – a06e Jun 02 '16 at 12:20
0

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.