Linux commands: chown
A quick guide to the `chown` command, used to change the owner of a file
Every file/directory in an Operating System like Linux or macOS (and every UNIX systems in general) has an owner.
The owner of a file can do everything with it. It can decide the fate of that file.
The owner (and the root
user) can change the owner to another user, too, using the chown
command:
chown <owner> <file>
Like this:
chown flavio test.txt
For example if you have a file that’s owned by root
, you can’t write to it as another user:
You can use chown
to transfer the ownership to you:
It’s rather common to have the need to change the ownership of a directory, and recursively all the files contained, plus all the subdirectories and the files contained in them, too.
You can do so using the -R
flag:
chown -R <owner> <file>
Files/directories don’t just have an owner, they also have a group. Through this command you can change that simultaneously while you change the owner:
chown <owner>:<group> <file>
Example:
chown flavio:users test.txt
You can also just change the group of a file using the chgrp
command:
chgrp <group> <filename>
The
chown
command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025