How to change the file owner and group in Linux?

LearnTips LearnTips How to change the file owner and group in Linux?

Tagged: 

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #3823
    Santhosh Kumar D
    Keymaster
    @santhosh

    In Linux, every file and directory is managed by a specific user and a specific group. File system ownership is fundamental to the Linux operating system. Please note that only the root user and members of the sudo group can change the ownership of a file in Linux for security purposes.

    How to change the file owner and group in Linux?

    For instance, let’s say we have a file named file.pdf. We want to change the ownership of this file to a user named santhosh and also change the group ownership of this file to a group called digitash. Let’s see how to do just that.

    Find the file owner and group

    You can find the current owner of the file and the group using the following command.

    sudo ls -l file.pdf

    You will get an output like this.

    -rw-rw-r-- 1 root www-data 1847 Oct 9 2019 file.pdf

    This means that the file is currently owned by the root user and belongs to the www-data group.

    Change the file owner of a specific file

    You can change the file owner using the chown command.

    sudo chown santhosh file.pdf

    Check if the file owner has been changed.

    sudo ls -l file.pdf
    -rw-rw-r-- 1 santhosh www-data 1847 Oct 9 2019 file.pdf

    You will now see that the file ownership has been changed. The file file.pdf is now owned by the user Santhosh.

    Change the ownership of all files inside a directory

    To change the ownership of all files inside a directory, you can use the -R option as follows.

    sudo chown -R user directory/

    Change the group ownership of specific a file

    You can change the group ownership of a specific file using the chgrp command.

    sudo chgrp digitash file.pdf

    Check if the group ownership of the file has been changed.

    sudo ls -l file.pdf
    -rw-rw-r-- 1 santhosh digitash 1847 Oct 9 2019 file.pdf

    You will now see that the group ownership of the file has been changed. The file file.pdf now belongs to the digitash group.

    Change the group ownership of all files inside a directory

    To change the group ownership of all files inside a directory, you can use the -R option as follows.

    sudo chgrp -R group directory/

    Change both the file owner and the group

    You can change both the file owner and the group using just the chown command as follows.

    sudo chown santhosh:digitash file.pdf

    Check if the file owner and the group has been changed.

    sudo ls -l file.pdf
    -rw-rw-r-- 1 santhosh digitash 1847 Oct 9 2019 file.pdf

    The file file.pdf is now owned by santhosh and belongs to the digitash group.

    Learn more Linux commands.

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.