Sunday, July 25, 2021

10 Examples of chmod command in UNIX Linux

chmod command in UNIX or Linux is used to change file or directory permissions. This is one of many UNIX basic commands which a UNIX or Linux user must be familiar with. In this UNIX command tutorial we will see how to change file permissions using the chmod command, what are file permissions in UNIX, how to change permissions of directory and sub-directory using UNIX chmod command and finally how to create executable files in UNIX using the chmod command. Before going directly into examples of chmod command let's spend a few minutes on understanding file permissions in UNIX and why do we need to change file permissions etc.


In UNIX each file has three permission read, write and execute, and three classes user(owner), group, and others. So each file is provided permissions in a combination of class and permissions. You can see the permission of an individual file or directory by using the ls command. For example in the below file

example@localhost~/test ls -lrt stock_trading_systems
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

It has read, write, and execute permission for all users, groups, and others. You can read more about file permissions in UNIX in my post beginner’s guide to UNIX file permissions. To read more about UNIX and Linux file permissions see this UNIX tutorial on file and directory permissions.

By the way, if you are new to Linux then I also suggest you go through a comprehensive Linux course to learn some basics commands and fundamentals like Linux file system, permissions, and other basic things. 




Chmod command Examples in UNIX and Linux

Now let's see some practical and frequently used example of chmod command in UNIX



1. chmod command Example 1: making read-only file in Unix

In this example of chmod command in UNIX we will see how to make a file read only by only providing read access to owner. You can also give read access to group and others and keep write access for owner which we will see in subsequent examples.

example@localhost~/test ls -lrt stock_trading_systems
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

Here file stock_trading_systems has read, write and execute permission “-rwxrwxrwx" for all

example@localhost~/test chmod 400 stock_trading_systems

400 means 100 000 000 means r-- --- --- i.e. read only for owner

example@localhost~/test ls -lrt stock_trading_systems
-r-------- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

Now file is read only and only owner can read it “-r--------"



chmod command examples in unix linux



2. chmod command Example 2:  change permissions only for user, group, or others.

In this example of the chmod command, we will see how to change file permissions on user, group, and others level. You can easily modify file permission for any of these classes. If you are using text format than “u” is user , “o” is other and “g” is group also “r” is read , “w” is write and “x” is execute. + means adding permission and “-“is removing permission.

example@localhost~/test ls -lrt chmod_examples
-r-------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

example@localhost~/test chmod u+w chmod_examples

example@localhost~/test ls -lrt chmod_examples
-rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

Now let’s change file permissions only for group by using chmod command

example@localhost~/test ls -lrt chmod_examples
-rw------- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

example@localhost~/test chmod g+w chmod_examples

example@localhost~/test ls -lrt chmod_examples
-rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

In this chmod command example we will change permission only for others class without affecting user and group class.

example@localhost~/test ls -lrt chmod_examples
-rw--w---- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

example@localhost~/test chmod o+w chmod_examples

example@localhost~/test ls -lrt chmod_examples
-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 chmod_examples


3. Chmod command Example 3:  change file permissions for all (user + group + others)

In the last UNIX chmod example we learn how to change permission for user, group and others individually but some time its convenient to change permissions for all instead of modifying individual permission for user, group and other.
If you are providing permission in text format than “a” is used for “all” while “u” is used for user.

example@localhost~/test ls -lrt linux_command.txt
-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt

example@localhost~/test chmod a+x linux_command.txt

example@localhost~/test ls -lrt linux_command.txt
-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*


4. Chmod command Example 4: Changing permissions in numeric format of chmod command

Chmod command in UNIX and Linux allows modifying permissions not just on text format which is more readable but also on numeric format where combination of permissions are represented in octal format e.g. 777 where first digit is for user, second is for group and 3rd is for others. Now if you write down 1st digit in binary format it will be written as 111 on which 1st digit is for read permission, 2nd is for write and 3rd is for execute permission.

example@localhost~/test ls -lrt unix_command.txt
-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 unix_command.txt

example@localhost~/test chmod 777 unix_command.txt

example@localhost~/test ls -lrt unix_command.txt
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*

5. Chmod command Example 5: How to remove file permission using chmod command Unix

In this example of chmod command in UNIX we will see how to remove various permissions from files. You can easily remove read, write, or execute permission from a file using chmod command in both numeric and text format. Below examples shows the removal of execute permission represented by –x in text format.

example@localhost~/test ls -lrt linux_command.txt
-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

example@localhost~/test chmod a-x linux_command.txt

example@localhost~/test ls -lrt linux_command.txt
-rw--w--w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt

6. Chmod command Example 6: changing permission for directory and subdirectory recursively in Unix

This is the most frequently used example of a chmod command where we want to provide permission to any directory and all contents inside that directory including files and subdirectories. By using –R command option of chmod in Unix you can provide  permissions recursively to any directory as shown in the below example of chmod command.

example@localhost~/test ls -lrt
total 8.0K
-rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*
drwxr-xr-x+ 1 example Domain Users    0 Jul 15 14:33 stocks/

example@localhost~/test chmod -R 777 stocks/

example@localhost~/test ls -lrt
total 8.0K
-rwxrwxrwx  1 example Domain Users    0 Jul 15 11:42 unix_command.txt*
drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

example@localhost~/test ls -lrt stocks
total 0
-rwxrwxrwx 1 example Domain Users 0 Jul 15 14:33 online_stock_exchanges.txt*

7. Chmod command Example 7:  How to remove read and write access from file for all

So far we have been seeing how to provide read, write and execute permission to file and directory in UNIX and now we will see the opposite of that i.e. how to remove read, write and execute access. It's simple in text format because instead of + we are going to use -. Just like + used to add permission – it will be used to remove permissions.

example@localhost~/test ls -lrt stock_trading_systems
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

example@localhost~/test chmod a-wx stock_trading_systems

example@localhost~/test ls -lrt stock_trading_systems
-r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

8. Chmod command Example 8: setting execute permission only on directories without touching files


Many times we just want to provide directory or subdirectory execute permission without modifying permissions on file just to make those directories searchable. Until I know this command I used to do this by finding all directory and then changing there execute permission but we have a better way to do it by using chmod command in UNIX. You can use "X" (capital X) options to provide execute permission to only directories without touching files. Let’s see an example of chmod command for that:

example@localhost~/test ls -lrt
total 8.0K
-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
drw-rw-rw-+ 1 example Domain Users    0 Jul 15 14:33 stocks/

example@localhost~/test chmod a+X *

example@localhost~/test ls -lrt
total 8.0K
-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

Remember to use X (capital case) if you use x (small case) it will affect all files and directories.

9. Chmod command Example 9:  changing multiple permission for a file or directory in Unix or Linux

You can change the combination of user + groups or groups+ other in one command to modify permissions of files and directory. Below an example of chmod command just doing same it’s providing execute permission for user and read, execute permission

example@localhost~/test ls -lrt
total 8.0K
-r--r--r--  1 example Domain Users    0 Jul 15 11:42 stock_trading_systems
drwxrwxrwx+ 1 example Domain Users    0 Jul 15 14:33 stocks/

example@localhost~/test chmod u+x,g+x stock_trading_systems

example@localhost~/test ls -lrt stock_trading_systems
-r-xr-xr-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

10. Chmod command Example 10: How to copy permission from one file to another in Unix

This is a very interesting example of chmod command in UNIX which copies permission from one file to another. You can easily reference source file and copy all permissions on that file to the destination file as shown in the following chmod example:

example@localhost~/test ls -lrt future_trading
-rwxrwxrwx 1 example Domain Users 0 Jul 15 15:30 future_trading*

example@localhost~/test ls -lrt stock_trading_systems
-r--r--r-- 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

example@localhost~/test chmod --reference=stock_trading_systems future_trading

example@localhost~/test ls -lrt future_trading
-r--r--r-- 1 example Domain Users 0 Jul 15 15:30 future_trading


These were some of the frequently used examples of chmod command in UNIX or Linux. Chmod command is as useful as UNIX find command or grep command and knowing how to change file permissions is an essential skill while working in UNIX. Please share if you have any other example of chmod command which we should be aware of.


Other UNIX command tutorial you may like

6 comments :

Kedar said...

what is difference between chmod 777 and chmod 755 command ? I have used chmod 644 command on files but I am just puzzled about chmod 755 and chmod 777 and where can I use it or more precisely where does those chmod command get used ?

sky said...

Hi, What is difference between chown and chmod command in Unix? Can you also share some difficult example of chmod command in Unix like updating permissions of all files matching with regular expression

Pawan said...

Hi Kedar, main difference between chmod 777 and chmod 755 is there options, 777 is read + write + execute for every one ( owner, group and others) while 755 is read + write + execute for owner and read + execute for others and group. many unix programmer just use chmod 777 to provide every permission to any directory or file. chmod -R 777 is great if you want to provide similar permission on sub directories as well.

Anonymous said...

Noone sane at mind uses 777 much.
always keep security inn mind.

Anonymous said...

Kedar - Take a look at this site: http://file-permissions.ninja
Type numbers like 777, 755 and watch the checkbox change - its help me a lot to understand what each number say.

Serious learners said...

Chown is to assign user:group like Chown oracle:dba file name

Post a Comment