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.
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.
- 1 chown command syntax
- 2 Changing the ownership of all sub-directories
- 3 Changing the owner of a file
- 4 Changing the owners of multiple files
- 5 Changing the owner of directory and file at once
- 6 Changing the owner using wildcards
- 7 Conclusion
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.
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 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.
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.
How do you change the owner of the file in Linux?
How to Change the Owner of a File. Become superuser or assume an equivalent role.. Change the owner of a file by using the chown command. # chown new-owner filename. new-owner. Specifies the user name or UID of the new owner of the file or directory. filename. … . Verify that the owner of the file has changed. # ls -l filename..
Which command is used for change the owner permissions of file?
The chmod command enables you to change the permissions on a file. You must be superuser or the owner of a file or directory to change its permissions.
How do you change the owner of a file?
Here’s how to take ownership and get full access to files and folders in Windows 10.. MORE: How to Use Windows 10.. Right-click on a file or folder.. Select Properties.. Click the Security tab.. Click Advanced.. Click “Change” next to the owner name.. Click Advanced.. Click Find Now..
Which command is used to change the ownership?
The command chown /ˈtʃoʊn/, an abbreviation of change owner, is used on Unix and Unix-like operating systems to change the owner of file system files, directories.