2

I have a directory that I want to move (with its contents, recursively) 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 directory using scp -r (see here). However, both directories are located in the same file system, so if I want to move the directory, copying and then removing the original is inefficient. Is there a way to move the directory, without using sudo (I don't know the root's password, but I know the other user's password)?

Related: See here for how to move a single file. That post however doesn't work for directories.

a06e
  • 1,727

5 Answers5

3

Moving files (without changing their ownership) only requires being able to write to the directories containing them. So, to move /home/usera/dir1/dir2 and its contents to /home/userb/dir3, starting as usera:

cd ~/dir1
find . -type d -print0 | xargs -0 chmod 777
su userb
cd ~/dir3
mv ~usera/dir1/dir2 .
exit

then, as usera, restore the permissions to whatever it is they should be (in particular on dir1).

usera will remain the owner of the moved files though, which may not be convenient. Another option is to do something similar, creating a link farm with cp -rl; this works in similar conditions, and has the advantage of creating all the new directories belonging to userb.

Stephen Kitt
  • 434,908
  • This would work in practice. But ideally I am looking for a one-line command like scp (or like the answer I linked, that I can maybe alias), that will request the other user's password and do the move. The problem with this approach (not a big issue in my case), is that it is insecure, since the permissions are wrong during the move. – a06e Jun 02 '16 at 13:04
1

I would do the contrary of Kupferdache:

I would create a public writeable directory on my home, go to the other account and move the file to my directory, than change back the directory permission.

Note: this is a real move. Then you should setup the permission as you need. So your solution with scp could be the safe one (safe and good file permissions on your side)

  • scp is unnecessarily slow. – a06e Jun 02 '16 at 13:05
  • Yes, but it is a lot simpler (and scp should be optimized also for local transfers, just don't use wildcards). And it depends on how many times you need to perform such action. The other solutions are slow to setup/type, but if you can automatize they are a win. – Giacomo Catenazzi Jun 02 '16 at 13:15
  • How is scp with local transfers any different to cp? – Chris Davies Jun 02 '16 at 18:37
  • @roaima: scp read the file as the origin user, cp read the file as destination user (in this case). So on common setups, cp cannot read the file of the other user. – Giacomo Catenazzi Jun 02 '16 at 18:41
1

Would this work for your needs? Rsync will copy the directories, files and permissions to the target directory.

rsync -avz /path/to/files target_server:/path_to_files

Edit: And you can add the flag to remove the original if the copy is successful.

  • this copies the file, which is unnecessarily slow, if the files are in the same filesystem. – a06e Jun 02 '16 at 19:00
0

I would use chown <new username> with all files recuslive and then su <new username> mv the files.

0

Right you dont want that, so lets try something different!

1- Doing the commands as the other user without asking for password anymore:

First you need to prepare your environment, so you will not need the password to do the commands.

Prepare the user2:

user2@machine:~$ cp /bin/bash .bash
user2@machine:~$ chmod a+s /home/user2/.bash

And yes now user1 you can login as the user2 without any password!

user1@machine:~$ /home/user2/.bash -p
bash-4.3$ whoami 
user2

Now inside user1, you can do this without any password.

user1@machine:~$ chmod -R 777 source ; /home/user2/.bash -pc "mv source dest"

But that can be very unsafe, because if other user discovery that /home/user2/.bash -p will login as the user2 without password, your security will be unsafe. This will be safe if you have the two users inside a common group so you will use chmod ug+s and not chmod a+s for prepare your environment, and only users in the group can run bash as the other user! Nice isn't?

2- Doing the move asking for the password (more secure):

If you dont have the users in a common group and want to run commands as the other, you might consider to use the password asking version like that:

user1@machine:~$ chmod -R 777 source ; su user2 -c "mv source dest"

This helps?