How to copy any files or directories from one user to another user in a same machine via terminal?

Suppose there is one file test.txt in home directory of USER1. I need to copy that file to the home directory of USER2 in my machine. How can I do it via terminal?

share|improve this question
up vote 18 down vote accepted

Assuming that you have sudo privileges the following command will do.

sudo cp /home/USER1/FNAME /home/USER2/FNAME && sudo chown USER2:USER2 /home/USER2/FNAME

Will copy the file from USER1 to USER2, and then change the owner of the copy in /home/USER2 to USER2

If you do not have sudo privileges, then the two users will need to ensure that you have read permissions on the USER1 directory, and write access on the USER2 directory. If you have these accesses, you can enter the command:

cp /home/USER1/FNAME /home/USER2/FNAME

This will copy the file in question, but USER2 may not be able to manipulate the file until they have appropriate permissions.

share|improve this answer
    
This will work only if USER1 has sudo access, which might not be the case in a multiuser system. – BostonHiker Nov 18 '14 at 15:24
    
The other option would be to set the permissions on their own folder to allow the other user to grab them, but this option is much easier if possible. – Geary Shull Nov 18 '14 at 15:29
    
@BostonHiker I had made the assumption that the person doing the copy is neither USER1 or USER2, and has sudo priviledges. This is probably a poor assumption on my part. – Charles Green Nov 18 '14 at 16:10
    
@Charles Green - please edit your answer to state your assumptions, otherwise it is incomplete/misleading. – BostonHiker Nov 18 '14 at 16:19
    
@BostonHiker Done. Thanks for keeping me honest. – Charles Green Nov 18 '14 at 17:48

if you dont have sudo privileges but you can login with the two users then you can use scp with localhost :

scp file1 user2@localhost:/home/user2/
share|improve this answer
    
What makes you think that this is about a remote host or that there's an SSH/SFTP server running on the machine in question? – David Foerster Jan 12 '17 at 13:26
1  
Assuming you have SSH installed – loudstil Feb 14 '17 at 15:09

Assume you either do not have ssh installed, or cannot do do not want to share keys/secrets.

Suppose user1 is in group1, and user2 is in group2, and both user1 != user2 and group1 != group2.

Create a shared group, group3s.

addgrp group3

Add both user1 and user2 to group3s.

Create a directory in a mutually accessible place, where one user owns, but has group ownership of group3s.

#as user1,
mkdir $place/shared && chown user1.group3s $place/shared && chmod 770 $place/shared;
#as user1 or user2,
cp $file $place/shared && chgrp $place/shared/$file && chmod 660 $place/shared/$file

But, suppose you cannot create the new, shared group, and place both users in that group?

Create the directory and give it 770 permissions,

mkdir $place/shared && chown user1.group1 $place/shared && chmod 770 $place/shared;

Then, as root/admin, change the group ownership to the other user's group,

sudo bash
chgrp group2 $place/shared && chmod g+s $place/shared

The command chmod g+s sets the setgid bit so that files placed into the directory have the group ownership set to group2.

share|improve this answer

As USER1:

cp [filename] /tmp
chmod 777 /tmp/[filename]

As USER2:

cp /tmp/[filename] .

As USER1:

rm /tmp/[filename]
share|improve this answer

Before going to copy one user to another you need to login as su user and then you use the command cp

sudo cp /home/shyam/Desktop/sparkhadoop_2.11-1.0.jar /home/hadoop/Desktop
share|improve this answer
    
This won't transfer the ownership to USER2 but leave it as root though. -1 – David Foerster Nov 11 '17 at 17:49
    
@DavidFoerster we are not transferring the ownership, we copy the file from one user to another user. using root user, we can transfer the file. OK – Shyam Gupta Nov 11 '17 at 18:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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