Daily Linux Command: Chown

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.txt to user1, keeping the group unchanged.

2. Change both owner and group

sudo chown user1:group1 filename.txt

Change the owner of filename.txt to user1 and the group to group1.

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

-R indicates 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 sudo privileges.
  • When changing the owner, the user and group must already exist in the system (in /etc/passwd and /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/projects directory and all its contents to user1, and the group to developers, displaying each step of the changes.

Leave a Comment