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 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 chmod command. Before going directly into examples of chmod command let's spend 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 combination of class and permissions. You can see the permission of individual file or directory by using ls command. For example in below file
It has read, write and execute permission for all users, group 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.
Chmod command Examples in UNIX and Linux
Now let's see some practical and frequently used example of chmod command in UNIX
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.
chmod command Example 2: change permissions only for user, group or others.
In this example of 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.
Now let’s change file permissions only for group by using chmod command
In this chmod command example we will change permission only for others class without affecting user and group class.
Chmod command Example 3: change file permissions for all (user + group + others)
In 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.
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.
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 file using chmod command in both numeric and text format. Below examples shows removal of execute permission represented by –x in text format.
Chmod command Example 6: changing permission for directory and subdirectory recursively in Unix
This is the most frequently used example of chmod command where we want to provide permission to any directory and all contents inside that directory including files and sub directories. By using –R command option of chmod in Unix you can provide permissions recursively to any directory as shown in below example of chmod command.
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 opposite of that i.e. how to remove read, write and execute access. Its simple in text format because instead of + we are going to use -. Just like + used to add permission – will be used to remove permissions.
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:
Remember to use X (capital case) if you use x (small case) it will affect all files and directories.
Chmod command Example 9: changing mutiple permission for a file or directory in Unix or Linux
You can change combination of user + groups or groups+ other in one command to modify permissions of files and directory. Below example of chmod command just doing same it’s providing execute permission for user and read, execute permission
Chmod command Example 10: How to copy permission from one file to another in Unix
This is 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 destination file as shown in following chmod example:
These were some of frequently used example 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 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