chown is a command in the Linux system used to change the owner and group of a file or directory. Its name comes from “change owner”.
📌 Basic Syntax:
chown [options] [owner][:[group]] file or directory
🧠 Common Examples
1. Change the owner of a file only
sudo chown user1 filename.txt
Change the owner of
filename.txttouser1, keeping the group unchanged.
2. Change both owner and group
sudo chown user1:group1 filename.txt
Change the owner of
filename.txttouser1and the group togroup1.
3. Change only the group
sudo chown :group1 filename.txt
Only change the group of the file to
group1, keeping the owner unchanged.
4. Recursively change the owner and group of a directory and its subdirectories/files
sudo chown -R user1:group1 /path/to/directory
-Rindicates a recursive operation, applicable to directories and their contents.
🛠️ Common Options
| Option | Description |
|---|---|
-R |
Recursively process all files and subdirectories in the directory |
-v |
Display detailed change process (verbose) |
-c |
Similar to -v, but only displays information when changes occur |
-f |
Silent mode, does not display error messages |
🔐 Notes
- Regular users typically cannot change the owner of a file, requiring
sudoprivileges. - When changing the owner, the user and group must already exist in the system (in
/etc/passwdand/etc/group). - Use recursive operations (
-R) with caution, especially on system directories, as it may affect system security and functionality.
✅ Example: Recursively change and display the operation process
sudo chown -Rv user1:developers /home/user1/projects
Set the owner of the
/home/user1/projectsdirectory and all its contents touser1, and the group todevelopers, displaying each step of the changes.