Using the Linux “chown” command, you can assign a new owner and new groups to files or folders. The order can be adjusted using different options.
What is the chown command?¶
Generally, the person who creates a file or folder retains ownership of it. However, if you need to modify it or if you want to modify the group of a file or folder, Linux provides you with the appropriate tool: “chown”. The chown command takes its name from the English terms “ CHANGE OWNer » (Change user). In the majority of Linux distributions, such as Debian or Ubuntu, the command is part of the standard tools.
How does the chown command work?¶
To define a new owner for a file or folder, this person is mentioned in the command. For this purpose, the name of the person or their identifier is authorized. The relevant information accessed by the chown command is logged under /etc/passwd And /etc/group. The group membership can therefore also be changed later.
What is the syntax of Linux chown?¶
The chown command always has the same basic syntax. It appears as shown below:
$ chown [Options] [Propriétaire] [:[Groupe]] Fichier
bash
However, there are a few different possibilities for combining the name of the new owner and the group. The chown command in Linux allows the following combinations:
- owner_name:group_name : this syntax logs both the owner and the group
- :group_name : only group membership is modified; the owner remains unchanged
- owner_name : only the owner is redefined
- owner_name: : the owner is redefined; the standard group of the currently logged in user serves as the group
What are the chown command options?¶
There are different options through which you can further specify the order. Here are the main options:
- -c or –changes : this option means that only files whose owner has really been modified will be displayed
- -f or –force : When you use this option, error messages relating to aborted modification attempts are eliminated
- -R or –recursive : With this option you ensure that similar subfolders and subdirectories are also changed when running the chown command
- -v or –verbose : this option displays the changes that the Linux chown command has just made
Examples using the Linux chown command¶
In order to get a clearer idea of the exact usage of the chown command, we present a few more examples.
$ chown Peter fichierexemple.txt
bash
With this chown command, the user Peter becomes the new owner of the file examplefile.txt.
$ chown $USER fichierexemple.txt
bash
In this example, the current user becomes the owner of the file examplefile.txt.
$ chown -cR Peter:Données/médias/documents
bash
User Peter becomes owner of the directory /media/documents and all related subdirectories. Furthermore, these directories and subfolders are assigned to the “Data” group.