Intro To 'chown' Command In Linux
2023-05-04 - By Robert Elder
I use the 'chown' command to change the user and group ownership of files and directories. Here, I have a file that's currently owned by the user 'robert':
ls -l file1.txt
-rw-rw-r-- 1 robert robert 0 May 4 23:54 file1.txt
This command will change the user ownership of file1.txt from 'robert' to 'mike':
chown mike file1.txt
And now, you can see that 'file1.txt' is owned by 'mike':
ls -l file1.txt
-rw-rw-r-- 1 mike robert 0 May 4 23:54 file1.txt
The 'chown' command can also perform the functions of the 'chgrp' command as well. If we start with 'file1.txt' again with the owner and group set to 'robert':
ls -l file1.txt
-rw-rw-r-- 1 robert robert 0 May 4 23:54 file1.txt
This command will change both the user and group ownership at the same time from 'robert' to 'mike':
chown mike:mike file1.txt
And here, you can verify that both the user and group have been set to 'mike':
ls -l file1.txt
-rw-rw-r-- 1 mike mike 0 May 4 23:54 file1.txt
The file permissions associated with the user and group are located here:
Using 'chown' To Recursively Change Ownership
Here, I have a directory with a bunch of files that are all owned by the user and group 'robert':
ls -l docs
total 4
-rw-rw-r-- 1 robert robert 0 May 5 00:13 cat.jpg
-rw-rw-r-- 1 robert robert 0 May 5 00:13 dog.jpg
drwxrwxr-x 2 robert robert 4096 May 5 00:13 more-docs
-rw-rw-r-- 1 robert robert 0 May 5 00:13 README.txt
You can use the '-R' flag with the 'chown' command to recursively change the ownership of all items in a directory:
chown -R mike:mike docs
And now, you can see that all files in this directory are owned by the user and group 'mike':
ls -l docs
total 4
-rw-rw-r-- 1 mike mike 0 May 5 00:13 cat.jpg
-rw-rw-r-- 1 mike mike 0 May 5 00:13 dog.jpg
drwxrwxr-x 2 mike mike 4096 May 5 00:13 more-docs
-rw-rw-r-- 1 mike mike 0 May 5 00:13 README.txt
And that's why the 'chown' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|