How to move a file to another user’s directory

filespermissions

I'm using CentOS 6 and I'm trying to move a file from USER A to USER B but I keep getting permission denied.

What do I need to edit to allow to me to put files from one user's directory into another's?

Best Answer

You need the rights to read the file from user A, and to write the file to user B's location.

There are several ways to archive this:

1)
As user A (assumed she can read her own files) get WRITE permission to user B's location. This means being in a group which is allowed to w to the destination directory and which is allowed to access (x) the directory path to the destination.

Example:

cd /home/user/A/
ls -l testfile  
-rw-------  1 userA  users  18 Dec 31 16:31 testfile

Notice the r. User A is allowed to read her own file

cp testfile /home/userB/testdir

User A will need to traverse to the folders /home, /home/userB and /home/userB/testdir. All of these need to be 'x' for her. She wants to write to the folder 'testdir' and thus she also needs 'w' on that folder.

That 'w' can be in the user, group or other part. But if you set it in 'other' then every user can write to it. Thus you usually make shared files or shared folders part of a group. (e.g. in /etc/group create a group 'sales' and add both users to that).


2)
As user B: (which is assumed to be able to write in his own folder).
Get read permission on the file in userA's folder and the right to traverse there. (same as in 1).

You now can copy the file to your own location. You can not delete it from user A's folder unless you also have the rights to do that. So you can copy but not move.


3)
As a user with uid 0 (usually root, toor, admin):

mv /home/userA/testfile /home/userB/

Notice that the file is still owned by userA, and that user B might not have the rights to work on the file. You can correct this with chown.


4)
Without the heavy guns, as often done by two users:

As UserA: cp testfile /tmp/
As userB: cp /tmp/testfile ~
As userA: rm /tmp/testfile

This is not the most secure way (other can see the file while it is in /tmp), but I think that this is the most common solution.


If you want to do this often then I strongly suggest creating a group and giving all users in that group rights to a shared location.

Not asked, but a solution like this is often practical (shown with music, but it might as well be sales reports).

vim /etc/group and add a line like this: shared_music:*:3141:userA,UserB,UserC. This will create a group called shared_music, give it a number, and select which users will belong to that group.

Then make folder owned by that group.

cd /usr/local/
md our_ultimate_shared_music_collection
chgrp shared_music our_ultimate_shared_music_collection
chmod g+rwx our_ultimate_shared_music_collection
chmod g+S our_ultimate_shared_music_collection

Make a folder in a sane location. Set the group and then allow all people in that group to read, write and cd into that folder. The last command (make group setuid) means that newly created subfiles inherit the same group as the directory, and newly created subdirectories inherit the set-group-ID bit of the parent directory. This prevent userA from writing files to that directory which USerB (or anyone but A) could not deleted or rename.