Signup/Sign In

Recursive Modification of Directory Permissions

Files in Linux have set read, write, and execute permissions, separated into three categories, user, groups, and others.

This gives a great level of freedom on which user can run what kind of application, or whether the text content of a file can be read, or if a file can be written to. To change these permissions Linux provides us with a utility known as chmod, which is used to modify these said file permissions, and they can be set in various combinations, such as read-write, read-execute, and so on.

To continue, it is suggested to get a grasp of file permissions in Linux, and how to use chmod in the most basic scenarios.

chmod command syntax

chmod has a basic syntax as follows

chmod [OPTIONS] [MODE] [FILE]

Only one option above will be discussed in this post, the rest can be referred to over here.

The option we will discuss has the following ways of specification; -R/--recursive.

To continue first let us discuss chmod modes, and their notations.

chmod command modes

chmod modes can be specified in two ways:

  • Numeric notation
  • Symbolic notation

Numeric Notation

Numeric notation takes the form of an octal number of 3 digits. The first digit specifies user permissions, the next group permissions, and the final permissions for other users/groups.

The octal digit can be broken into the sum of 3 digits: 4, 2, 1. The 4 specifies read, 2 write, and 1 is execute.

To summarize

Number Permission Summary
7 Read, write, execute
6 Read, write
5 Read, execute
4 Read
3 Write, execute
2 Write
1 Execute
0 No actions permitted

Put simply, looking at the above table, we can see that for example a file with the permissions 775, specifies that both the owner user, and the group have all permissions, but everyone else is only allowed to only read, or execute the file.

Symbolic Notation

Symbolic notation takes the numeric notation and expands it a bit more. r for read, w for write, x for execute. And some keywords are used to specify who are we modifying the file for. u for a user, g for a group, o for others, and a for all (which is the default for no specifier mentioned). We can set exact user permissions using the = symbol, remove permissions with -, and add permissions with +.

For example, we want to set all with only an execute permission, we can run

chmod =x fileName

chmod =x file

And say we want to specify file permission, which gives all permissions to users, and groups, but no write permission to others, we would run:

chmod =rx,ug+w fileName

chmod =rx,ug+w file

The above options can be broken down to:

  • =rx: Everyone (implicitly) is given a read and execute permission.
  • ug+w: Users and groups are adding permission. The permission granted is the write (w) permission.

Now that we understand how to specify permissions, we can see how to recursively run the chmod program for all files within a directory.

Recursive chmod using -R/--recursive

The -R flag specifies that when the argument for the file is a directory, we are to recursively descend that directory, and modify the permission of every file, and directory that we can.

For example, say we have a directory that has all permissions set for every category of permissions in Linux, but we want to set the permission to be read, execute for the user, read for the group, and no permissions for others, we could manually modify every file in the directory, or we could use the -R flag.

Taking the above example, to make the changes specified above, we would run the following code snippet from our terminal:

chmod -R 540 test

We can see the permissions of every file in test before, and after the modifications in the following screenshot.

chmod -R 540 test

Conclusion

The tutorial here discusses how to modify the file permissions on Linux recursively using chmod command, and the -R/--recursive flag.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.