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.

  • Log in as them and give yourself write access on their directory ? – 123 Jun 2 '16 at 12:41
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.

| improve this answer | |
  • 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. – becko Jun 2 '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)

| improve this answer | |
  • scp is unnecessarily slow. – becko Jun 2 '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 2 '16 at 13:15
  • How is scp with local transfers any different to cp? – roaima Jun 2 '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 2 '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.

| improve this answer | |
  • this copies the file, which is unnecessarily slow, if the files are in the same filesystem. – becko Jun 2 '16 at 19:00
0

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

| improve this answer | |
  • If they're owned by the target user you then can't necessarily delete them from the original user's account. I'm also not sure how you'd chown without root permissions. – roaima Jun 2 '16 at 13:43
  • hm, have to check that, never used chown, without the user->root or root->user content. – Kupferdrache Jun 2 '16 at 13:50
  • This requires root access. – Gilles 'SO- stop being evil' Jun 3 '16 at 21:33
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?

| improve this answer | |
  • I'm not root, cannot edit /etc/sudoers – becko Jun 2 '16 at 13:02
  • And this solution is better? – Luciano Andress Martini Jun 2 '16 at 13:06
  • rsync copies the file (--remove-source-files removes the files after they were copied). This is unnecessarily slow if the source and destination are in the same filesystem. – becko Jun 2 '16 at 13:24
  • Answer me that, it is a bad thing if all users can have full access to the folder that you are copying in the destination? – Luciano Andress Martini Jun 2 '16 at 13:30
  • That would be insecure. There are many users. – becko Jun 2 '16 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.