How to use the Linux chown command to set new owners for files
With the Linux command ‘chown’ you can assign new owners or groups to files or folders. The command can be customised.
What is the Linux chown command?
Usually, whoever creates a file or folder remains the owner of the file. However, if that has to be changed or if you want to change the group of a file or folder, Linux has the right tool for you in the form of ‘chown’. Linux chown owes its name to the words CHange OWNer. With most Linux distributions such as Debian or Ubuntu the command is part of the standard.
How does the chown command work?
To designate a new owner for a file or folder using the Linux chown command, you can include the username or user ID of the new owner in the command. The Linux chown command retrieves the necessary information from the /etc/passwd and /etc/group files. Additionally, you can modify the group membership of the file or folder using this command.
What’s the syntax of Linux chown?
The chown command always has the same basic syntax. It looks like this:
$ chown [Options] [Owner] [:[Group] File
bashHowever, there are a few different ways in which the name of the new owner and the group can be combined. Linux chown allows these combinations:
- owner_name:group_name: this changes the owner and group at the same time.
- :group_name: only the group membership is changed; the owner name remains identical.
- owner_name: only the owner is changed.
- owner_name:: the owner is changed; the default group of the currently logged in user acts as the group.
What are the options of Linux chown command?
There are several options that you can use to further specify the command. The most important ones are:
- -c or –changes: By using this option, the Linux chown command will only display files that have had their owner changed.
- -f or –force: If you use this option, error messages about failed change attempts will be suppressed.
- -R or –recursive: With this option you ensure that all subfolders and directories are also changed when the chown command is executed.
- -v or –verbose: This option allows you to see what Linux chown is doing.
Examples of Linux chown
To provide a better understanding of how the Linux chown command is employed, here are a few examples:
$ chown Peter examplefile.txt
bashWith this Linux chown command, the user Peter becomes the new owner of the file examplefile.txt.
$ chown $USER examplefile.txt
bashThis means that the current user becomes the owner of the file examplefile.txt.
$ chown -cR Peter:files /media/documents
bashThe user Peter becomes the owner for the directory /media/documents and all associated subdirectories. In addition, these directories and the subfolders are transferred to the ‘files’ group.