Linux Chown Command Tutorial for Beginners (7 Examples)
On this page
- Linux chown command
- Q1. How to change the owner of a file?
- Q2. How to change the group of a file?
- Q3. How to change both owner and group of a file?
- Q4. How to change owner or group (or both) after checking existing owner/group?
- Q5. How to pick owner/group information from a reference file?
- Q6. How to make chown operate on files and directories recursively?
- Q7. How to make chown suppress error messages?
In Linux, there may be times when you might want to change the owner and group-related information for a file or directory. If you are a command line newbie, and want to know how you can make such changes through the command line, you'll be glad to know that there exists a command - dubbed chown - that lets you do this.
Before we start with the chown command tutorial, it's worth mentioning that all examples and instructions mentioned here have been tested on Ubuntu 16.04 LTS. Also, the chown version we've used is 8.25.
Linux chown command
As already mentioned in the beginning, the chown command lets you change file owner and group through the command line. Following is the command's generic syntax:
chown [OPTION]... [OWNER][:[GROUP]] FILE...
Here's what the tool's man page says about it:
If only an owner (a user name or numeric user ID) is given, that user is made the owner of each
given file, and the files' group is not changed. If the owner is followed by a colon and a
group name (or numeric group ID), with no spaces between them, the group ownership of the files is
changed as well. If a colon but no group name follows the user name, that user is made the owner
of the files and the group of the files is changed to that user's login group. If the colon
and group are given, but the owner is omitted, only the group of the files is changed; in this case,
chown performs the same function as chgrp. If only a colon is given, or if the entire operand is
empty, neither the owner nor the group is changed.
The following Q&A-type examples will give you a good idea about how the chown command works:
Q1. How to change the owner of a file?
Consider the following example:
Here, the owner of the file is 'himanshu' and the group it belongs to is also 'himanshu'. Now, to change the owner to, say 'root,' use the following command:
chown root file1
The following screenshot confirms that the owner has now been changed to 'root'.
Q2. How to change the group of a file?
Changing group is similar to changing owner. The only difference is in the syntax of the command, which is as follows:
chown :[group-name] [file-name]
So suppose the requirement is to change the group of 'file1' to 'root'. Then the command would be:
chown :root file1
The following screenshot shows the group was successfully changed from 'himanshu' to 'root'.
Note: In case you're wondering why the 'sudo' command was used, or in case you're new to 'sudo', you should first go through our tutorial on this tool.
Q3. How to change both owner and group of a file?
To change both owner and group of file, use the following syntax:
chown [new-owner]:[new-group] [file-name]
So in our case, to change the existing owner and group from 'root' to 'himanshu', we'll use the following command:
chown himanshu:himanshu file1
The following screenshot shows the above command in action:
Q4. How to change owner or group (or both) after checking existing owner/group?
There might be situations where-in you may want to first cross-check existing owner/group of a file before making any change. So for those cases, you can use the --from command line option. This option requires you to mention the owner/group name that you want to verify.
chown --from=[curr-own]:[curr-group] [new-owner]:[new-group] [filename]
chown --from=root:himanshu himanshu:root file1
The above command will check whether the existing owner is 'root' and group is 'himanshu'. If yes, then owner will be changed to 'himanshu' and group will become 'root'.
Q5. How to pick owner/group information from a reference file?
There might also be situations where-in you may want chown to pick up owner and group-related information from a file, instead of manually entering it on the command line. For those cases, you can use the --reference command line option. This option requires you to enter the name of the reference file.
chown --reference=[ref-file-name] [filename]
chown --reference=file2 file1
So the above command will copy the owner and group information from file2 to file1.
Q6. How to make chown operate on files and directories recursively?
To make the chown command recursively operate on files and directories, use the -R command line option.
chown -R [new-owner]:[new-group] [directory-name-or-path]
For those who aren't aware, recursive means the operation will be performed for all files in the given directory, as well as for files and directories within all sub-directories.
Q7. How to make chown suppress error messages?
There may be times when the chown command you run gives error. For example, the following command when executed on my system:
chown --from=himanshu:himanshu himanshu:root file4
gave the following error:
chown: cannot access 'file4': No such file or directory
Now suppose the requirement is that the tool shouldn't display such errors. Then this can be made possible using the -f command line option.
The aforementioned screenshot confirms that the -f command line option suppresses errors/warnings.
The chown command, as most of you'd likely agree, isn't difficult to use. What's even better is that the tool's man page contains a lot of details that might be of help to users (especially newbies). Try out the examples we've explained here, and for the rest of the features/options, go through the chown man page. In case of any doubt or query, leave a comment below.