chown Linux Command Guide
The chown
command in Linux is used to change the ownership of files and directories. It allows users to transfer ownership of a file or directory to a specific user or group. This command is essential for system administrators to manage file permissions and access control. By understanding the syntax and options of the chown
command, users can effectively control who can access and modify files on a Linux system.
chown Syntax:
chown [options] [user][:group] file
Options:
Option | Description |
---|---|
-c | Report only when a change is made |
-f | Suppress most error messages |
-h | Change the group of a symlink |
-R | Recursively change files and directories |
-v | Output a diagnostic for every file processed |
Parameters:
Parameter | Description |
---|---|
user | The user ownership to be set |
group | The group ownership to be set |
file | The file or directory to change ownership of |
chown Command Samples:
Change Ownership of a File
chown user1 file.txt
Changes the ownership of the file.txt to user1.
Change Ownership of a Directory Recursively
chown -R user2:group2 directory/
Changes the ownership and group ownership of the directory and all its contents recursively to user2 and group2.
Change Ownership of Multiple Files
chown user3:group3 file1.txt file2.txt
Changes the ownership and group ownership of file1.txt and file2.txt to user3 and group3.
Change Only the Group Ownership of a File
chown :group4 file3.txt
Changes only the group ownership of file3.txt to group4.
Change User and Group Ownership of a Symbolic Link
chown user5:group5 symbolic_link
Changes the user and group ownership of the symbolic_link.
Preserve the Original Owner on Copy
cp -p origin.txt destination.txtchown --reference=origin.txt destination.txt
Preserves the ownership of origin.txt and applies it to destination.txt.
Change Ownership for All Files in a Directory
chown -c newuser:newgroup *
Changes the ownership of all files in the current directory to newuser and newgroup, displaying a message for each file changed.
chown FAQ:
How do I change the owner of a file in Linux?
To change the owner of a file in Linux, use the following command:
chown newowner myfile.txt
How do I change the group of a file in Linux?
To change the group of a file in Linux, use the following command:
chown :newgroup myfile.txt
How do I change both the owner and group of a file in Linux?
To change both the owner and group of a file in Linux, use the following command:
chown newowner:newgroup myfile.txt
How do I change the owner of a directory and its contents in Linux recursively?
To change the owner of a directory and its contents in Linux recursively, use the following command:
chown -R newowner mydirectory
How do I change the owner of a file using the numeric UID in Linux?
To change the owner of a file using the numeric UID in Linux, use the following command:
chown 1000 myfile.txt
How do I change the group of a file using the numeric GID in Linux?
To change the group of a file using the numeric GID in Linux, use the following command:
chown :1000 myfile.txt
Applications of the chown command
- Change the owner of a file
- Change the group owner of a file
- Recursively change ownership of files and directories
- Change ownership of a symbolic link
- Change ownership of multiple files at once