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
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.
Now, write out the “ls” command and check if your directory ownership is updated or not.
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.
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.”
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.
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.
Write out the below-given command to confirm the changes.
Changing the owner of directory and file at once
Follow the below-given method for changing the owners of the file and directory instantly.
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 ls -l s*.*
Conclusion
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.