How to use chmod recursively on Linux?

LinuxOperating SystemOpen Source

You might have been in a scenario where you are using a Linux as your main operating system and then you try to create or edit a file and the Linux terminal responds with something like “Permission deny” error. In typical sense, such an error is related to insufficient permissions that your current user is having and can be solved by setting the correct file permissions or changing the owner.

In Linux, the files are controlled through the file permissions, ownership and attributes, which in turn makes sure that only authorized users and processes can access files and directories.

Before understanding how to make the chmod command to run recursively on all the directories and subdirectories, let’s first understand what the chmod command actually means.

Chmod is a linux command that is mainly used to change the access permissions of file system objects. It changes the permissions of each file according to a mode, where mode simply describes the permissions to modify.

Syntax

chmod [Options]... Mode [,Mode]... file...
chmod [Options]... Numeric_Mode file...
chmod [Options]... --reference=RFile file...

In the above syntax, the [OPTIONS] placeholder can be replaced with the different flags that can be used with it, some of the most commonly used flags are

-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
-c, --changes like verbose but report only when a change is made
-c, --reference=RFile use RFile's mode instead of MODE values
-R, --recursive change files and directories recursively
--help display help and exit
--version output version information and exit

Now that we know about what the chmod actually does, let’s consider a few examples where we will provide different permissions to the same file.

Example 1

chmod 777 sample.txt

The above command uses the mode 777 which simply means that anyone can read, write and execute the file.

Example 2

chmod 020 sample.txt

The above command uses the mode 020 which simply means that only a group can read, write and execute the file.

Now, let’s learn how to run the chmod recursively

Syntax

chmod -R MODE directory

Consider we have a directory where I want to run chmod recursively on all the files. The directory looks something like this before the command

immukul@192 linux-questions-code % ls -tlr
total 960
-rw-r--r-- 1 immukul staff 446966 Sep 23 1998 wget-1.5.3.tar.gz
drwxr-xr-x 3 immukul staff     96 Jul  7 17:42 d1
-rwxrwxrwx 1 root    staff    106 Jul  8 13:10 sample2.sh
drwxr-xr-x 4 immukul staff    128 Jul  8 19:05 d2
-rwxrwxrwx 1 root    staff    946 Jul 12 18:45 sample.sh
-rwxrwxrwx 1 root    staff    718 Jul 12 18:48 sample1.sh

Consider the command shown below

chmod -R 777 .

Output

total 960
-rwxrwxrwx 1 immukul staff 446966 Sep 23 1998 wget-1.5.3.tar.gz
drwxrwxrwx 3 immukul staff     96 Jul  7 17:42 d1
-rwxrwxrwx 1 root    staff    106 Jul  8 13:10 sample2.sh
drwxrwxrwx 4 immukul staff    128 Jul  8 19:05 d2
-rwxrwxrwx 1 root    staff    946 Jul 12 18:45 sample.sh
-rwxrwxrwx 1 root    staff    718 Jul 12 18:48 sample1.sh
raja
Published on 31-Jul-2021 11:55:53
Advertisements