Enhancing Skills

chown: Change file ownership

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 of file1.txt to user user1 and group group1.
  • ls -l file1.txt: Displays the detailed listing of file1.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 user user2 and group group2. 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 of ref_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 of file1.txt to match the ownership of ref_file.txt.
  • ls -l file1.txt: Displays the detailed listing of file1.txt to verify the new ownership.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.