chown: Change file ownership
August 9th, 2024 9:19 AM Mr. Q Categories: Command
Command: chown
Used to change the ownership of files or directories. Ownership can be changed for the user (owner) and group.
Sample Command and Output:
$ chown user1:group1 file1.txt
$ ls -l file1.txt
-rw-r--r-- 1 user1 group1 4096 Aug 7 12:34 file1.txt
Description:
chown user1:group1 file1.txt
: Changes the ownership offile1.txt
to useruser1
and groupgroup1
.ls -l file1.txt
: Displays the detailed listing offile1.txt
to confirm the new ownership.
Sample Command and Output:
$ chown -R user2:group2 /home/user/Documents/
$ ls -l /home/user/Documents/
drwxr-xr-x 2 user2 group2 4096 Aug 7 12:34 folder1
-rw-r--r-- 1 user2 group2 4096 Aug 7 12:34 file1.txt
Description:
chown -R user2:group2 /home/user/Documents/
: Recursively changes the ownership of all files and directories in/home/user/Documents/
to useruser2
and groupgroup2
. The-R
option applies the changes to the directory and its contents.ls -l /home/user/Documents/
: Lists the contents of the directory to confirm the new ownership.
Additional Arguments:
-R
: Recursively changes ownership for directories and their contents.--reference=ref_file
: Changes ownership to match that ofref_file
.
Sample Command and Output:
$ chown --reference=ref_file.txt file1.txt
$ ls -l file1.txt
-rw-r--r-- 1 user1 group1 4096 Aug 7 12:34 file1.txt
Description:
chown --reference=ref_file.txt file1.txt
: Changes the ownership offile1.txt
to match the ownership ofref_file.txt
.ls -l file1.txt
: Displays the detailed listing offile1.txt
to verify the new ownership.