Change file ownership, based on previous owner

9,146

Solution 1

If I understand you correctly, you want to change the owner of all files inside some directory (or the root) that are owned by user #500 to be owned by another user, without modifying files owned by any other user. You're in that situation because you've copied a whole directory tree from another machine, where files inside that tree were owned by many different users, but you're only interested in updating those that were owned by "your" user at the moment, and not any of the files that are owned by user #501 or any other.

GNU chown supports an option --from=500 that you can use in combination with the -R recursive option to do this:

chown -R --from=500 yourusername /path/here

This will be the fastest option if you have GNU chown, which on CentOS you should.

Alternatively can use find on any system:

find /path/here -user 500 -exec chown yourusername '{}' '+'

find will look at every file and directory recursively inside /path/here, matching all of those owned by user #500. With all of those files, it will execute chown yourusername file1 file2... as many times as required. After the command finishes, all files that were owned by user #500 will be owned by yourusername. You'll need to run that command as root to be able to change the file owners.

You can check for any stragglers by running the same find command without a command to run:

find /path/here -user 500

It should list no files at this point.


An important caveat: if any of the files owned by user #500 are symlinks, chown will by default change the owner of the file the symlink points at, not the link itself. If you don't trust the files you're examining, this is a security hole. Use chown -h in that case.

Solution 2

One problem with the chown command (but that's actually down to the chown() system call) is that it strips the setuid and setgid bits in the permissions as a security measure.

It also doesn't cover ACLs.

To work around that, an approach can be to use rsync on a directory over itself in update mode and with the --usermap option:

$ ls -ln
total 6732
-rwsr-xr-x  1 1002 1000 6888896 Nov  3 21:50 a
drwxr-xr-x+ 2 1002 1000    4096 Nov  3 21:46 x

$ getfacl -n x
# file: x
# owner: 1002
# group: 1000
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:1002:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

$ sudo rsync -rAau --usermap=1002:1000 . .

$ ls -ln
total 6732
-rwsr-xr-x  1 1000 1000 6888896 Nov  3 21:50 a
drwxr-xr-x+ 2 1000 1000    4096 Nov  3 21:46 x

$ getfacl -n x
# file: x
# owner: 1000
# group: 1000
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:1000:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

1002 changed to 1000 in both the files owners and ACL entries.

It will also process symlinks correctly.

Share:
9,146
waiting_for_peace
Author by

waiting_for_peace

Updated on September 18, 2022

Comments

  • waiting_for_peace
    waiting_for_peace almost 2 years

    I am a moderately new linux user. I changed my PC, and started using CentOS 7 from CentOS 6.

    So I attached my previous hard disk to my new pc to take backup of my files. Now, copying the files (and preserving the permissions and all), the files shows owner as 500 (I guess this is my previous UID).

    Is there any way I can change them to my new user name? I want to exclude the files which shows some other owners like 501.

    Edit:

    Example:

    ls -l
    total 3
    -rw-rw-r--.  1 500 500        210 Jan 10  2012 about.xml
    drwxr-xr-x.  2 500 500       4096 May 15  2013 apache
    drwxrwxr-x.  2 500 500       4096 Dec  9  2012 etc
    

    Now, I can do chown -R xyz:xyz . to make them look like:

    ls -l
    total 3
    -rw-rw-r--.  1 xyz xyz        210 Jan 10  2012 about.xml
    drwxr-xr-x.  2 xyz xyz       4096 May 15  2013 apache
    drwxrwxr-x.  2 xyz xyz       4096 Dec  9  2012 etc 
    

    But I just want to know if there are some kind of commands which can map user 500 to user "xyz".

    Thank you.

    • terdon
      terdon almost 10 years
      Are you simply asking how to change file permissions? What is your current UID? Why would you preserve ownership of files if you're just going to change it again? Why not just run a simple copy which will also update the file ownership? Please edit your question and clarify.
    • waiting_for_peace
      waiting_for_peace almost 10 years
      Running a simple copy (being root) would make the ownership of the files as root, right? I want to copy the files with owner 500 and change the ownership to "xyz". And the UID of "xyz" is 1000.
  • waiting_for_peace
    waiting_for_peace almost 10 years
    Thanks a lot.. :) It was that simple, and I was beginning to lose hope..
  • Stéphane Chazelas
    Stéphane Chazelas over 9 years
    Another caveat is that chown removes the setuid and setgid bits in the permissions.