How to Use chown in Linux
Learn how to change file ownership in Linux with the chown command. Simple and effective.
The chown
command stands for ‘Change Owner’; it is used to change the owner and group of files, directories, and symbolic links in the system.
To better understand its function, it’s important to know that in Linux, every file and directory is assigned three types of owner attributes:
- User: The User is the one who owns the file,
- Group: Group includes other users who are in the file’s group, and ,
- Others: Users that are not in the file’s group.
Each of these categories can have different permission levels for reading, writing, and executing the files.
The chown
command allows you to change the User and Group ownership of a file or directory.
chown
typically requires administrative (root) privileges to run. If you’re not logged in as root, you’ll likely need to use sudo
before the command, like sudo chown user:group filename
, to make changes.
Here are some ways to use the chown
command:
1. Change the Owner
The basic syntax for changing the owner is chown owner filename
.
Example :
To change the owner of a file named file.txt
to user1
, you would use:
chown user1 file.txt
2. Change the Owner and Group
You can change the owner and group at the same time by separating the owner and group with a :
(colon).
Example :
To change the owner of file.txt
to user1
and the group
to group1
, you would use:
chown user1:group1 file.txt
3. Change the Owner of a Directory and Its Contents
You can change the owner of a directory and all its contents using the -R
(or --recursive
) option.
Example :
To change the owner of a directory named dir
and all its contents to user1
, you would use:
chown -R user1 dir
4. Change the Owner of All Files in a Directory
To change the owner of all files in a directory without changing the owner of the directory itself or any subdirectories, you can use the *
wildcard.
Example:
chown user1 dir/*
5. Change the Group Only
If you want to change the group without changing the owner, you can do so by prefixing the group with a :
(colon).
Example:
To change the group of file.txt
to group1
, you would use:
chown :group1 file.txt
6. Change the Owner and Group to the Login User
You can change the owner and group of a file or directory to the login user without specifying the user name.
Example:
chown $USER:$USER file.txt
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |