All files and directories that exist on your Linux system have an owner, a group, and permission access rights that are granted to the file owners, its group members, or others. The chown command permits a Linux user to change any directory or file’s ownership. In this post, we will demonstrate the usage of the chown command for changing ownership through practical examples. But firstly, you should understand why you would want to change the ownership of any directory or file?
Here is the list of few scenarios in which you want to do this:
- For example, you want an already created directory or file to be accessible to a particular user.
- When a user leaves an organization, all of his data comes under the responsibility of another employee. Therefore, you want to change the ownership of the new team member.
- Changing directory or file ownership is also necessary when you are writing a script that has to be used by only a specific person.
- Transferring files between different Linux systems also requires changes in files and directories ownership.
Now let’s check out the syntax of the chown command.
chown command syntax
chown [Options] User[:Group] File(s)
Utilize the “User” for the username or replace it using the user ID, also known as UID. Add the group name in the “Group” part of the GID (group ID). At the end of the chown command, add files or directories for which you want to change the ownership.
Changing the owner of a directory
We have created a separate user named “utest” and a group “ugroup” for demonstrating the examples. To change the ownership of any directory, utilize the chown command with the username and path of the directory.
$ sudo chown utest ./samplefolder/
Now, write out the “ls” command and check if your directory ownership is updated or not.
$ ls -l -d ./samplefolder/
Changing the ownership of all sub-directories
Utilize the “-R” option of the chown command for changing the owners of all the files or folders present in a directory.
$ sudo chown -R utest ./samplefolder/
List out the directory content to view the results.
Changing the owner of a file
First of all, list out the file content to know about its ownership.
After that, specify the name of the user who you want to be the new owner of this file. In our case, we have chosen “utest” to avail the ownership of “samplefile.txt.”
$ sudo chown utest samplefile.txt
Again, verify the updated ownership changes.
You can also use the “User ID” or “UID” of any user for this purpose. For that, retrieve the user ID of the new user by utilizing the “id” command with the “-u” option.
Add the User ID instead of the username to make the ownership changes.
$ sudo chown 1001 samplefile.txt
Changing the owners of multiple files
Specify the file names at the end of the chown command to change the ownership of multiple files at once.
$ sudo chown utest samplefile.txt samplefile2.txt
Write out the below-given command to confirm the changes.
$ sudo ls -al samplefile.txt samplefile2.txt
Changing the owner of directory and file at once
Follow the below-given method for changing the owners of the file and directory instantly.
$ sudo chown utest samplefile.txt test
This command will make “utest” the new owner of the “test directory” and the “samplefile.txt.”
Changing the owner using wildcards
Wildcards are used to select a specific file group according to the given pattern. The chown command will then change the owner of the files after retrieving them from the wildcard execution.
$ sudo chown utest s*.*
$ sudo ls -l s*.*
For data security concerns, you may want to specify the ownership of files and directories. In Linux, we use the chown command-line utility for changing the ownership of directories or files. You can also utilize this command for changing directories and multiple file ownership at once. In this post, all of these statements are justified by providing practical examples.