10 Practical chmod Command Examples in Linux

This tutorial explains Linux “chmod” command, options and its usage with examples.

chmod command is used to change file/directory mode bits or permissions.

Syntax:

  <strong><em>chmod [OPTION]... MODE[,MODE]... FILE...</em></strong>
  <strong><em>chmod [OPTION]... OCTAL-MODE FILE...</em></strong>
  <strong><em>chmod [OPTION]... --reference=RFILE FILE...</em></strong>

Here, MODE can be of two types:

  • Symbolic Mode
  • Octal Mode

In UNIX/LINUX system each file/directory has three permissions read, write, and execute. These permissions are categorized into three classes: user (owner), group, and others. To view and modify these permissions, you can use the chmod command.

You can check a file or directory’s permissions as follows:

advertisement
advertisement
sanfoundry-> ls -l 1.txt 
-rw-rw-r-- 1 user_name user_name 0 Jun 14 12:42 1.txt

Symbolic Representation:
Symbolic representation defines three different roles and their respective permissions:

  • Roles:
    • “u” – user
    • “g” – group
    • “o” – others
  • Permissions:
    • “r” – read
    • “w” – write
    • “x” – execute

The output format of “ls -l” consists of 10 symbols. The first line begins with “-” or “d,” indicating a file or directory. Following that are the permissions for the user, group, and others, grouped in threes. For example, “-rw-rw-r–” represents a file with user permissions “rw-” (read and write, but no execute), and so on.

Octal Representation:
In octal representation, “r” has a value of “4,” “w” is “2,” and “x” is “1.” Therefore, the octal representation for “rw-” is (4 + 2 + 0 =) 6 for the user. Consequently, for “-rw-rw-r–,” it becomes 664.

chmod Command Examples

Here’s the listing of various examples of using the chmod command to modify file permissions.

Example 1: Making a File Read-Only (chmod 400 file_name):
To make a file read-only means only the owner can read it. You need to grant read permissions to the owner and no permissions to others and the group, so “400” is the octal representation for this.

sanfoundry-> ls -l 1.txt 
-rw-rw-r-- 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod 400 1.txt
sanfoundry-> ls -l 1.txt 
-r-------- 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Example 2: Changing Permissions for All Users (chmod 777 file/directory_name):

sanfoundry-> mkdir sample_folder 
sanfoundry-> ls -l 
drwxrwxr-x 2 himanshu himanshu  4096 Jun 14 12:42 sample_folder
sanfoundry-> chmod 777 sample_folder/
drwxrwxrwx 2 himanshu himanshu  4096 Jun 14 12:42 sample_folder

Example 3: Change permissions only for user, group or others (chmod u/g/o+r/w/x file/directory_name):
In order to change permissions only for a user (u), group (g), or others (o) at a time, symbolic representations is a good choice, Though you can use the octal representation as well.

advertisement
sanfoundry-> ls -l 1.txt 
-r-xr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod u+w 1.txt 
sanfoundry-> ls -l 1.txt 
-rwxr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Similarly , it can be done for the directories as well.

sanfoundry-> ls -l
drwxr-xr-x 2 himanshu himanshu  4096 Jun 14 12:42 sample_folder
sanfoundry-> chmod g+w sample_folder/
sanfoundry-> ls -l 
drwxrwxr-x 2 himanshu himanshu  4096 Jun 14 12:42 sample_folder

Example 4: Removing File Permissions (Symbolic Mode) Using the chmod Command in Unix (chmod u/g/o – r/w/x file/directory):
To remove the premissions You have to use “-” symbol instead od “+” in chmod command.

advertisement
sanfoundry-> ls -l 1.txt 
-rwxr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod u-w 1.txt 
sanfoundry-> ls -l 1.txt 
-r-xr----x 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Example 5: Removing File Permissions (Octal Mode) Using the chmod Command in Unix (chmod “numbers” file/directory):

sanfoundry-> ls -l 1.txt 
-rwxrw-rw- 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod 555 1.txt 
sanfoundry-> ls -l 1.txt 
-r-xr-xr-x 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Here , file “1.txt” has permissions 755. If you want to remove the write permissions of a user, You only have to change 7 of user to 5(since 2 for
write is reduced.) These permissions are overwritten on the previous ones.

Example 6: Removing Read and Write Access for User/Group/Others with Symbolic Mode (chmod u/g/o – rw file/directory_name):

sanfoundry-> ls -l 1.txt 
-rwxrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod u-rw 1.txt 
sanfoundry-> ls -l 1.txt 
---xrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Example 7: Removing Read and Write Access for All Users with Symbolic Mode (chmod a – rw file/directory_name):

sanfoundry-> ls -l 1.txt 
-rwxrwxrwx 1 himanshu himanshu 0 Jun 14 12:42 1.txt
sanfoundry-> chmod a-rw 1.txt 
sanfoundry-> ls -l 1.txt 
---x--x--x 1 himanshu himanshu 0 Jun 14 12:42 1.txt

Example 8: Setting execute permission only on directories without touching files (chmod a+X *):
For this purpose you can use “X” option instead of “x” option since “x” option will give permissions to files/directories both.

sanfoundry-> ls -l 
-rw-rw-r-- 1 himanshu himanshu     0 Jun 14 15:53 1.txt
-rw-rw-r-- 1 himanshu himanshu     0 Jun 14 15:53 2.txt
drwxrwxr-- 2 himanshu himanshu  4096 Jun 14 15:53 sample_folder1
drwxrwxr-- 2 himanshu himanshu  4096 Jun 14 15:53 sample_folder2
sanfoundry-> chmod a+X *
-rw-rw-r-- 1 himanshu himanshu     0 Jun 14 15:53 1.txt
-rw-rw-r-- 1 himanshu himanshu     0 Jun 14 15:53 2.txt
drwxrwxr-x 2 himanshu himanshu  4096 Jun 14 15:53 sample_folder1
drwxrwxr-x 2 himanshu himanshu  4096 Jun 14 15:53 sample_folder2

Example 9: Copying permissions from one file to another in Unix (chmod –reference source_file destination_file):

sanfoundry-> ls -l 1.txt 2.txt 
-rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 1.txt
-rw-rw-r-- 1 himanshu himanshu 0 Jun 14 15:53 2.txt
sanfoundry-> chmod --reference 1.txt 2.txt 
sanfoundry-> ls -l 1.txt 2.txt 
-rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 1.txt
-rwxrwxrwx 1 himanshu himanshu 0 Jun 14 15:53 2.txt

Here as we can see, that here with “–reference option” file permissions of 1.txt are given to 2.txt.

Example 10: Changing multiple permissions in a file/directory (chmod Per1,per2,… file/directory_name):

sanfoundry-> ls -l 1.txt 
-r-xrw-r-x 1 himanshu himanshu 0 Jun 14 15:53 1.txt
sanfoundry-> chmod u+w,g+x 1.txt 
sanfoundry-> ls -l 1.txt 
-rwxrwxr-x 1 himanshu himanshu 0 Jun 14 15:53 1.txt

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.