Cyberithub

11 Popular Unix/Linux chmod command examples to Change File Permissions

In this article, I will take you through 11 Popular Unix/Linux chmod command examples to Change File Permissions. chmod command is used to change the permissions of files and directories in Linux. It allows the permissions to be changed in either Symbolic form or in numerical form. Both forms can be interchangeably used. Symbolic forms of permission could be given by:-

  • Read – r
  • Write – w
  • Execute – x

Numerical form of permission could be given by:-

  • 4 – Read
  • 2 – Write
  • 1 – Execute

Every files and directories has three types of ownership which in turn can have a permission of read, write and execute as described below.

  • User : Read, Write and Execute
  • Group : Read, Write and Execute
  • Others : Read, Write and Execute

You will always see permission in the form -rwxrwxrwx for any of the files and drwxrwxrwx for any of the directories in which first bit would be either (-) or d depends on it is a file or directory and then  rwx is for User, next rwx is for Group and last rwx is for Others. You can add or remove these permissions for User, Group and Others using chmod command in Linux. There would be some special bits also like sticky bit which I will explain in later articles. In this session, I will explain more about the usage of chmod command with the help of examples as mentioned below.

SYNOPSIS

chmod [OPTION]… MODE[,MODE]… FILE…
chmod [OPTION]… OCTAL-MODE FILE…
chmod [OPTION]… –reference=RFILE FILE…

11 Popular Unix/Linux chmod command examples to Change File Permissions 1

Unix/Linux chmod command examples to Change File Permissions

Also Read: 40 Best Examples of Find Command in Linux

Example 1: How to check chmod command version

If you want to check chmod command version then you need to use chmod --version command as shown below. As you can see from below output current chmod version is 8.22.

[root@localhost ~]# chmod --version
chmod (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie and Jim Meyering.

NOTE:

Please note that here I am using root user to run all the below commands.You can use any user with sudo access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo access to the User.

Example 2: How to use chmod command to change file permissions

If you want to change the permission of a file then you need to first find the current permission of the file and then change it to the required permission. Here we are first checking the permission of hello.txt file using ls -lrt hello.txt command.

[root@localhost ~]# ls -lrt hello.txt
-rw-r--r-- 1 root root 29 Jun 18 03:23 hello.txt

As you can see from above output, current file permission is 0640. If you want to change it to 0770 then you need to use chmod 770 hello.txt command as shown below.

[root@localhost ~]# chmod 770 hello.txt

Now if you again check the file hello.txt permission using ls -lrt hello.txt command then you can see it is now changed to 0740 as shown below.

[root@localhost ~]# ls -lrt hello.txt
-rwxrwx--- 1 root root 29 Jun 18 03:23 hello.txt

Example 3: How to Use Verbose Mode with chmod command in Linux

If you want to use see the permission changing then you need to use -v option with chmod to enable the verbose mode. Here we are changing the permission of hello.txt file from 0644 to 0770 using chmod -v 770 hello.txt command as shown below.

[root@localhost ~]# chmod -v 770 hello.txt
mode of ‘hello.txt’ changed from 0644 (rw-r--r--) to 0770 (rwxrwx---)

-v : output a diagnostic for every file processed. More can be checked on chmod Man Page.

Example 4: How to Track a change in File Permission using chmod command

If you want to track the file permission if it has really changed then you need to use -c option with chmod command as shown below. This option is just like verbose option but it will only report if there is a change in file permission. If you assign the same permission what file already had then it won’t show anything on the output. In this example hello.txt file permission was already 770 so after changing the permission again to 770 using chmod -c 770 hello.txt won’t show any output as there is no real change in file permission.

[root@localhost ~]# chmod -c 770 hello.txt

-c : like verbose but report only when a change is made. More can be checked on chmod Man Page.

Example 5: How to Assign File Permission to be the same as Reference File 

If you want to change some file permission to be the same as reference file then you need to provide the file name with --reference option. Here we have two files file.txt and hello.txt in which we want to change the permission of hello.txt file to be the same as file.txt permission. As you can see from below output, file.txt has 0644 permission and hello.txt has 0770 permission.

[root@localhost ~]# ls -lrt file.txt
-rw-r--r-- 1 root root 1177864 Jun 6 16:46 file.txt
[root@localhost ~]# ls -lrt hello.txt
-rwxrwx--- 1 root root 29 Jun 18 03:23 hello.txt

Now to change the permission of hello.txt file to be the same as file.txt permission you need to use chmod -v --reference=file.txt hello.txt command as shown below.

[root@localhost ~]# chmod -v --reference=file.txt hello.txt
mode of ‘hello.txt’ changed from 0770 (rwxrwx---) to 0644 (rw-r--r--)

–reference=RFILE : use RFILE’s mode instead of MODE values. More can be checked on chmod Man Page.

Example 6: How to Change the Permission of a Directory in Linux

If you want to change the permission of a directory in Linux then you need to use the same chmod command what you have used in above examples for files. In this example, we are having a directory example whose permission can be checked by using ls -lrtd example command as shown below. As you can see current permission on this directory is 0755.

[root@localhost ~]# ls -lrtd example
drwxr-xr-x 2 root root 35 Jun 28 14:02 example

So if you want to change the permission of directory example from 0755 to 0777, you need to use chmod -v 777 example command as shown below.

[root@localhost ~]# chmod -v 777 example
mode of ‘example’ changed from 0755 (rwxr-xr-x) to 0777 (rwxrwxrwx)

If you again check the permission of example directory using ls -ltrd example command then you will see it is now changed to 777 permission.

[root@localhost ~]# ls -lrtd example
drwxrwxrwx 2 root root 35 Jun 28 14:02 example

Example 7: How to Recursively change the permission of all the files in a directory

If you want to recursively the change the permission of all the files in a directory then you need to use -R option with chmod command. In this example we have a directory called example which contains two files hello.rs and hello. Current permission on these files can be checked by using ls -lrt example/ command as shown below.

[root@localhost ~]# ls -lrt example/
total 2848
-rw-r--r-- 1 root root 58 Jun 28 14:02 hello.rs
-rwxr-xr-x 1 root root 2912208 Jun 28 14:02 hello

So if you want to recursively change the permission of all the files of example directory to 777 then you need to use chmod -R 777  example command as shown below.

[root@localhost ~]# chmod -v -R 777 example
mode of ‘example’ retained as 0777 (rwxrwxrwx)
mode of ‘example/hello.rs’ changed from 0644 (rw-r--r--) to 0777 (rwxrwxrwx)
mode of ‘example/hello’ changed from 0755 (rwxr-xr-x) to 0777 (rwxrwxrwx)

Now if you again check the permission of all the files of example directory using ls -lrt example command then you will see it changed as 0777 as shown below.

[root@localhost ~]# ls -lrt example
total 2848
-rwxrwxrwx 1 root root 58 Jun 28 14:02 hello.rs
-rwxrwxrwx 1 root root 2912208 Jun 28 14:02 hello

Example 8: How to Completely Remove all the Permissions from a File

If you want to completely remove the permissions from a file then you need to set 000 permission to that file as shown below. In this example we are completely removing all the permissions from hello.txt file using chmod -v 000 hello.txt command as shown below.

[root@localhost ~]# chmod -v 000 hello.txt
mode of ‘hello.txt’ changed from 0644 (rw-r--r--) to 0000 (---------)

Example 9: How to Use Textual Permission instead of Numerical Permission with chmod command

If you want to use textual permission instead of numerical permission then you can do that by assigning read, write and execute permission to User, Group and Others in below format. Here we are editing permission of hello.txt file and assigning read, write and execute permission to User, read and write to Group, read and write to Others using chmod -v u=rwx,g=rw,o=rw hello.txt command as shown below.

[root@localhost ~]# chmod -v u=rwx,g=rw,o=rw hello.txt
mode of ‘hello.txt’ changed from 0000 (---------) to 0766 (rwxrw-rw-)

If you want to remove write and execute permission from User only and retain the Group and Others permission of hello.txt file then you need to use chmod -v u=r-- hello.txt command as shown below.

[root@localhost ~]# chmod -v u=r-- hello.txt
mode of ‘hello.txt’ changed from 0777 (rwxrwxrwx) to 0477 (r--rwxrwx)

Another Scenario could be when you want to remove execute permission from both Group and Others only and retain the User permission of hello.txt file then you need to use chmod -v g=rw-,o=rw- hello.txt command as shown below.

[root@localhost ~]# chmod -v g=rw-,o=rw- hello.txt
mode of ‘hello.txt’ changed from 0477 (r--rwxrwx) to 0466 (r--rw-rw-)

Similarly you can use textual permission assignment using chmod command for other scenarios also. In my experience, numerical permission will be far too easy to assigned but it is really a matter of choice. You can use the way you are more comfortable with.

Example 10:  How to Check all the Other Options of chmod command in Linux

If you want to check all the Other Options available with chmod command in Linux then you need to use chmod --help command as shown below.

[root@localhost ~]# chmod --help
Usage: chmod [OPTION]... MODE[,MODE]... FILE...
or: chmod [OPTION]... OCTAL-MODE FILE...
or: chmod [OPTION]... --reference=RFILE FILE...
Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display this help and exit
--version output version information and exit

Each MODE is of the form '[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'chmod invocation'

Example 11: How to check Man Page of chmod command in Linux

If you want to check the man page of chmod command then you need to use man chmod command as shown below.

[root@localhost ~]# man chmod
CHMOD(1) User Commands CHMOD(1)

NAME
chmod - change file mode bits

SYNOPSIS
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

DESCRIPTION
This manual page documents the GNU version of chmod command. chmod command changes the file mode bits of each given file according to mode, which can be either a symbolic
representation of changes to make, or an octal number representing the bit pattern for the new mode bits.

The format of a symbolic mode is [ugoa...][[+-=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the
set ugo. Multiple symbolic modes can be given, separated by commas.

A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g),
other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if a were given, but bits that are set in the umask
are not affected.

The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to
be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.

 

 

 

 

 

Popular Recommendations:-

C# data types with Best Examples (.NET v4.7)

How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps

Learn HTML Image Maps(v5) with Best Examples

Learn HTML Tables(v5) with Best Examples

How to Install PHP on RedHat/CentOS 7 with Easy Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7

Leave a Comment