38

How can I copy files or directories from one user to another user on the same machine via the commandline?

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

9 Answers 9

38

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.

4
  • 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. Nov 18, 2014 at 15:29
  • Does doing this use twice the amount of space on the hard disk? Dec 20, 2016 at 17:32
  • @caffeinemachine Yes, as you are creating duplicates of the files in question. You could use links for the second user, assuming that permissions in the first users directory have been set in such a manner that the second user has read (and write?) permissions on the file itself. The manual pages (man ln) are a good place to start learning about links. Dec 21, 2016 at 4:32
  • i get this error: "cp: omitting directory _______" oh I just realized I was copying a whole folder not a file, note 'sudo cp -r' and 'chown -R' to recursively copy all contents Oct 12, 2017 at 18:07
18

As USER1:

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

As USER2:

cp /tmp/[filename] .

As USER1:

rm /tmp/[filename]
1
  • 1
    You deserver more upvotes :) Jun 14, 2019 at 8:19
9

If you don't have sudo privileges, an alternative may be to use scp with localhost:

scp file1 user2@localhost:/home/user2/

This assumes:

  • You have the login information for both users
  • An SSH server is enabled on the host
  • can login via ssh
2
  • 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? Jan 12, 2017 at 13:26
  • 1
    Assuming you have SSH installed
    – loudstil
    Feb 14, 2017 at 15:09
2

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.

1

Use the below commands to copy files

sudo cp /home/USER1/test.txt /home/USER2/test.txt 
sudo chown USER2:USER2 /home/USER2/test.txt 

Use the below commands to copy directories

sudo cp -r /home/USER1/directory1 /home/USER2/directory1 
sudo chown -R USER2:USER2 /home/USER2/directory1 
0

Every other answer here requires root access, allows any user on the same machine to copy the file, or requires password sharing. Here is a method that does not:

Have USER2 (let's call him Bob) run the following commands (you can replace /tmp with any directory that both users have permissions to write to, but /tmp is ideal because by default it is sticky, which prevents a malicious user from subverting this process. A directory owned by Bob that is world-readable also works):

[bob@computer ~]$ touch /tmp/test.txt
[bob@computer ~]$ chmod 622 /tmp/test.txt

This creates a file that is world-writable, but not readable.

Then have USER1 (let's call her Alice) run (if you're paranoid, Alice can check the permissions first to make sure the file is owned by Bob):

[alice@computer ~]$ dd if="$HOME/test.txt" of=/tmp/test.txt

This overwrites the contents of /tmp/test.txt. If you want to check integrity of the file, Alice should also generate a hash of the file. For instance:

[alice@computer ~]$ openssl sha1 < "$HOME/test.txt" > /tmp/test.txt.sha1

You can instead digitally sign the file, or any other method of guaranteeing its integrity.

And finally Bob moves the file and takes ownership of it:

[bob@computer ~]$ mv /tmp/test.txt "$HOME" 
[bob@computer ~]$ chmod 600 "$HOME/test.txt"

And Bob can check the integrity if he likes. If so, he should check to make sure only Alice can write to /tmp/test.txt.

[bob@computer ~]$ diff /tmp/test/txt.sha1 <(openssl sha1 < "$HOME/test.txt")

If the file was copied correctly, this should display no output.

0

The trick is to place the file in a subdirectory where all users has read and execute rights.

It will not work in $HOME as it has file mode 700.

mkdir -m 755 share              # 755 might be default with umask
cd share                        # you have to cd
touch file
sudo -u user2 cp file ~user2/
0

try install;

man install -o, --owner=OWNER -g, --group=GROUP

install -C -o ldap -g ldap /etc/letsencrypt/live/privkey.pem /etc/openldap/certs/tls/ldap_server.key
2
  • 1
    This answer seems to have nothing to do with the question.
    – David
    Sep 26, 2022 at 14:03
  • instead of copying the file and authorizing it with chown, I shared it because it finished the job in one command
    – mertyak0n
    Sep 29, 2022 at 8:42
-1

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
2
  • This won't transfer the ownership to USER2 but leave it as root though. -1 Nov 11, 2017 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 Nov 11, 2017 at 18:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .