Skip to content

How to change file permissions recursively using Chmod in Linux?

If you’re a Linux user, you’re bound to run into some permission issues sooner or later. Generally, these errors can be resolved by assigning the correct user permissions or ownership to the file you’re trying to modify. 

In this article, we’re going over how to recursively change permissions of files and folders in Linux using the chmod command.


Chmod in Linux

The chmod command in Linux lets you change the permissions of files. The permissions are specified in a symbolic or numeric mode. Since the command only works on files, you’ll have to run it recursively using the –R or (–recursive) flag to change an entire folder’s permissions. 

The general syntax for the chmod command is as follows. 

chmod -R [permissions] [directory]

Perhaps the most popular example of a file permission changing scenario is when you’re running a local web server on your Linux machine. In this case, you’ll have to change several file permissions for them to be visible over your local network. You’ll be using the chmod command as follows.

chmod -R 755 /var/html/www

In the command above, 755 represents the permissions. You can mention the same permissions in symbolic mode as follows.

chmod -R u=rwx,go=rx /var/www/html

Do keep in mind that only the root owner or users with sudo privileges can change file permissions. 

In the examples above, specifies the current user, specifies the group, and o specifies the others. r stands for read permissions, w for write and x for execute. = assigns new permission to an existing one, + adds permission and – revokes one. 

In numeric mode, permissions and owners are specified by numbers. Below is the numerical representation of each number. 

  • 0: No permission
  • 1: Execute
  • 2: Write
  • 4: Read

The permission number for a specific user class is represented by the sum of values for that group. For example, if you want to give read, write and execute permissions to a user, the permission number will be 4+2+1 = 7.

Therefore the examples give the user permissions to read, write and execute while the group and owner only have read and execute permissions. 

Also read: What is Linux Booting Process?


Changing specific file types using find 

If you’re looking to change a specific file type’s permissions under a folder, you’re going to have to use the find command to find the file and then pass it on to chmod to change permissions. 

Going further with our webserver example, here’s how you would change all files that have permissions as 644 to 755. 

find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;

The same can be done in symbolic mode like this

find /var/www/html -type d -exec chmod u=rwx,go=rx {} \;
find /var/www/html -type f -exec chmod u=rw,go=r {} \;

Also read: How to rename a directory in Linux?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. We're an ad-supported publication. So, if you're running an Adblocker, we humbly request you to whitelist us.

We may earn a commission if you buy something from a link on this page. Thanks for your support.







>