How the chown Command works on Linux
In previous articles, we've covered how
chmod works. In this article, we'll be covering how to use
chown is closely connected to
chmod, since it affects who owns a file, and which group it belongs to.
In our article on
chmod we cover how different user types (i.e. owners, groups, and all other users) have different permission sets. That means who owns or in which group a file belongs to, can affect who can run it, and might even affect if a system process can run or open a file.
The syntax for
chown is shown below, where
[OPTIONS] are optional settings,
[USER[:GROUP]] are the user and group, and
[FILE] is the file, files, directory or directories we wish to affect with the command.
chown [OPTIONS] [USER[:GROUP]] [FILE]
If we specify only one
[USER[:GROUP]], then it is assumed that we are trying to change the owner only, whereas if we only type
:GROUP with a colon followed by the group name, then it is assumed that only the group is changing.
How to change the owner of a file with chown
At its most basic, we can change the owner of a particular file with
chown. For example, the following will change the owner for file.txt to
chown someOwner file.txt
If we want to change the group to
someGroup, and owner to
someOwner, then the following command will do that:
chown someOwner:someGroup file.txt
And if we want to change just the group to
someGroup, then this will do it:
chown :someGroup file.txt
This code will also work for directories.
Change the owner or group for all files in a directory
If we wish to change the owner or group for files and directories within a directory, we have to use the
For example, the below code will change the owner and group for all files in the
./test directory to someOwner and someGroup respectively:
chown -R someOwner:someGroup ./test
Symbolic links and chown
There are three options which affect how symbolic links are affected by
chown, and whether they are followed. These are
-L- follow every symbolic link.
-H- if the command line contains reference to a specific symbolic link, then follow it.
-P- do not follow symbolic links (default).
For example, the below will change all owners and groups within the
./test directory, and will traverse every symbolic link:
chown -RL someOwner:someGroup ./test
Using a reference file to change owners and groups
To use a reference file as a basis for the ownership and group of a file, use
--reference. For example, below the file new-file.txt will use the owner and group from file.txt. This will not work by default on MacOS.
chown --reference=file.txt new-file.txt
Change owner and group if it matches a specific owner and group
If we want to change only owners or groups which match a specific pair, we can use
--from. For example, the below will only change the user and group to
newOwner:newGroup if the file was originally set to
someOwner:someGroup. This will not work by default on MacOS.
chown --from=someOwner:someGroup newOwner:newGroup new-file.txt
This follows the same rules as before, so if we only want to match on
someGroup, we'd write
--from=:someGroup. If we only wanted to match on
someUser, we'd write
Other options for chown
As well as the already mentioned options, there are a few others which may be useful:
-v- verbose - will show a message after each file change.
-f- suppresses most error messages.
-h- changes the owner and group of a symbolic link, rather than the file or directory it links to.
-c- similar to verbose, but only reports when a change is made.
More Tips and Tricks for Linux
- Reference: Non Printable Characters List
- How to Remove Empty and Non Empty Directories in Linux
- Speed up your Website by Converting your Images to WebP from Terminal
- How to count all files in a directory in Linux
- How to Rename Files in Linux and MacOS Terminal
- How the which Command works on Linux
- How the mkdir command works on Linux
- How the cat Command works on Linux
- How the alias Command works on Linux
- How the chown Command works on Linux