Best Linux chmod Command With Examples

In this article, we are going to learn what is Linux chmod command and how to use it. chmod command is used in Linux related operating system to Change file mode bits. In simple language, you can change the permissions of files and directories in Linux using chmod command. There are two methods by which we can change the permissions:

1. Alphabetical
2. Numerical

To change permission using the Linux chmod command we have to follow some syntax and rules. As we discussed above we can change permission using Numerical and Alphabetical way, Here I have explained both methods with all tricks.

Alphabetical Way :

To use this method you have to remember below rules and alphabets for proper use.

To change the permission of user you have to use the alphabet “u“. Like that, for Group “g” and for others it’s “o“.

Like User, Group and Others we have to remember some alphabets to give Read, Write and Execute permission Like for Read permission you have to use the alphabet “r“, for Write “w” and for Execute it’s “x“.

So the formula for assigning permissions using chmod command is shown below :

u – User
g – Group
o – Others

r – Read
w – Write
x – Execute

For Example, if you want to give Read & Write permission to User/Owner and Read permission to Group & Others using Alphabetical way then the command would be: chmod u+rw,g+r,o+r Filename

Numerical Way :

To use this method you have to remember below Rules and Numbers for proper use.

4 – To give Read Permission
2 – To give Write Permission
1 – To give Execute Permission

For Example, if you want to give Read & Write permission to User/Owner and Read permission to Group & Others using Numeric way then the command calculation would be :

4+2 = 6 : This will add Read & Write permission to User/Owner.

4: This will add Read permission to Group.

4: This will add Read permission to Other.

So the command would be like: chmod 644 filename

You can check the permission of any file using below command :

itsmarttricks@ubuntu:~$ ls -l file.txt 
-rw-rw-r-- 1 itsmarttricks itsmarttricks 0 Feb 20 06:16 file.txt

And use the below command to check the permission of a directory :

itsmarttricks@ubuntu:~$ ls -ld data/
drwxrwxr-x 2 itsmarttricks itsmarttricks 4096 Feb 20 06:18 data/

Now here the question is How to Identify what permissions been assigned to a file or a directory. It’s quite simple.

Above I have given two examples. Now let’s identify the permission of file.txt. If you notice the properties of the file starts with “-rw-rw-r–“. Here we can identify what permission assigned to file.txt.

The permission section is divided into 10 bits out of which

”: First bit is responsible to identify whether it’s a file, directory, or a Symbolic Link. Refer the symbols below :

Important symbols :

 : Permission assigned to a File
d: Permission assigned to a Directory
l: Permission assigned to a Symbolic Link

2nd3rd4th bit’s are for permission of Owner.
5th6th7th bit’s are for permission of Group.
8th9th10th bit’s are for permission of Others.

  • To Give permission to any file/directory you have to use “+” Symbol.
  • To remove permission from any file/directory you have to use “” Symbol.
Best Linux CHMOD Command With Examples
Best Linux CHMOD Command With Examples

Now let’s have a look at some most important Linux chmod command with examples :

Assign permission to a File

Here I have a file named file.txt. Let’s give Read and Write permission to User/Owner using chmod command.

itsmarttricks@ubuntu:~$ chmod u+rw file.txt

itsmarttricks@ubuntu:~$ ls -l file.txt 
-rw------- 1 itsmarttricks itsmarttricks 0 Feb 20 06:16 file.txt

By Numerical Way use the below command :

itsmarttricks@ubuntu:~$ chmod 600 file.txt 

Also Read – Linux Permissions SUID, SGID and Sticky Bit Concept Explained with Examples

Assign Permission to a Directory

The below command can be used to give permission to a directory. Here I have a directory named data. So let’s give Full Permission (Read, Write, Execute) to User/Owner. Refer to the command below.

itsmarttricks@ubuntu:~$ chmod u+rwx data/

itsmarttricks@ubuntu:~$ ls -ld data/
drwx------ 2 itsmarttricks itsmarttricks 4096 Feb 20 06:18 data/

Numerical Way :

itsmarttricks@ubuntu:~$ chmod 700 data/

Remove permission from a file/directory

Remove permission from a file/directory using below Linux chmod command. Here I am removing Read and Write permission from User/Owner.

itsmarttricks@ubuntu:~$ chmod u-rw file.txt 

itsmarttricks@ubuntu:~$ ls -l file.txt 
----rw-r-- 1 itsmarttricks itsmarttricks 0 Feb 20 07:18 file.txt

Give permission to Everyone

You can use the below command to give permission to everyone. Here I am assigning Full Permission to everyone to access file.txt. There are two methods to do the same.

Method: 1

itsmarttricks@ubuntu:~$ chmod a+rwx file.txt 

itsmarttricks@ubuntu:~$ ls -l file.txt 
-rwxrwxrwx 1 itsmarttricks itsmarttricks0 Feb 20 06:44 file.txt

Method: 2

You can also use the below Linux chmod command to do the same.

itsmarttricks@ubuntu:~$ chmod ugo+rwx file.txt 

itsmarttricks@ubuntu:~$ ls -l file.txt 
-rwxrwxrwx 1 itsmarttricks itsmarttricks 0 Feb 20 06:44 file.txt

Numerical Way :

itsmarttricks@ubuntu:~$ chmod 777 file.txt 

Assign different permissions to User, Group & Others by using Single Linux chmod Command

Here I am Removing Execute permission from User/Owner and Adding Execute permission to Group & Others by using a single command.

itsmarttricks@ubuntu:~$ chmod u-x,g+x,o+x file.txt

itsmarttricks@ubuntu:~$ ls -l file.txt 
-rw-rwxrwx 1 itsmarttricks itsmarttricks 0 Feb 20 09:40 file.txt

Assign permission to a file by taking reference of any other file

You can copy permission from one file to another. Here I have a file named a1.txt whose permission is something like “-rw-r–r–“.

itsmarttricks@ubuntu:~$ ls -l a1.txt 
-rw-r--r-- 1 itsmarttricks itsmarttricks 0 Feb 20 07:21 a1.txt

Now I want to assign the same permission to my other file named a2.txt. You can do so using the Linux chmod command with argument –reference. Refer to the command below.

itsmarttricks@ubuntu:~$ chmod --reference=a1.txt a2.txt 

itsmarttricks@ubuntu:~$ ls -l a2.txt 
-rw-r--r-- 1 itsmarttricks itsmarttricks 0 Feb 20 07:22 a2.txt

Assign Permission Recursively

I have a directory named data, in which I have so many files and I want to give permission to all of them at once instead of manually one by one. To so you can use the Linux chmod command with argument -R. This will help you to give permission Recursively.

itsmarttricks@ubuntu:~$ chmod -R ugo+rwx data/

Numerical Way :

itsmarttricks@ubuntu:~$ chmod -R 777 data/

Assign permission with Verbose output

You can get output after assigning permission to any files/directories by using the Linux chmod command with argument -v. Refer to the command below.

itsmarttricks@ubuntu:~$ chmod -v 777 file.txt 
mode of 'file.txt' changed from 0664 (rw-rw-r--) to 0777 (rwxrwxrwx)

Assign permission with output (This command will give output only if there is any changes)

chmod command with argument -c also do’s the same thing as Verbose output (i.e. -v). But it will show the output only if there is any changes in permission. If you are assigning the same permission then it won’t show any output.

itsmarttricks@ubuntu:~$ chmod -c 755 file.txt 
mode of 'file.txt' changed from 0777 (rwxrwxrwx) to 0755 (rwxr-xr-x)

Assigning Permission by ignoring/solving errors

To avoid any erroes or to rectify any errors during assigning permission you can use Linux chmod command with argument -f. Refer to the command below.

itsmarttricks@ubuntu:~$ chmod -f 755 file.txt 

For chmod command Help

For chmod command more options and arguments you can use the below command.

itsmarttricks@ubuntu:~$ 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/>
Full documentation at: <http://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'

Check chmod command version

You can use the Linux chmod command with argument –version to check the version of the installed chmod command version.

itsmarttricks@ubuntu:~$ chmod --version
chmod (GNU coreutils) 8.25
Copyright (C) 2016 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.

That’s all, In this article, we have explained Best Linux CHMOD Command With Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

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