Sometimes you may need to recursively change folder permission in Linux. In Linux, if you want to read, edit or execute a file, you need to have the sufficient permissions for them. Otherwise, Linux will return ‘permission denied’ message. chmod is the most common in-built utility used to modify user permissions. It allows system administrators to manage access permissions to multiple users by simply modifying the file/folder permission for them. The same holds true for processes also, not just users.
By default, chmod allows you to modify permissions of one or more files & folders that you provide as input. It will only modify the permissions of listed files & folders, nothing more. But sometimes you way want to recursively modify file/folder permissions. In this article, we will learn how to recursively change folder permission in Linux.
How to Recursively Change Folder Permission in Linux
Only the root user, file owner and user with sudo privileges can edit file permissions. Here is the basic syntax of chmod command.
chmod [options] <permission_bits> <file_or_folder_path1> <file_or_folder_path2> ...
In the above command, you need to list one or more file/folder paths after chmod command and permission bits. If you provide relative paths, chmod will look for the specified files & folders relative to your present working directory.
Here is an example to change permission of file /home/ubuntu/data.txt
$ chmod 755 /home/ubuntu/data.txt
Here is the command to change permission of files data1.txt, data2.txt, /home/data.
$ chmod 755 data1.txt data2.txt /home/data
If you want to recursively change file & folder permissions of folder, you need to use -R option as shown below. Here is an example to recursively change folder permission for /home/ubuntu
$ chmod -R 755 /home/ubuntu
If you want to recursively change permissions for multiple folders, just mention their paths one after the other in a space separated format.
$ chmod -R 755 /home/ubuntu /home/projects /home/data
You can also change file/folder permissions using symbolic notation.
$ chmod -R u=rwx,go=rx /home/ubuntu
But please be careful while recursively changing file permissions, as it can impact a lot of files, which have different permissions for a reason.
When you use chmod recursively, it will assign the same permission for both files & folders. If you don’t want this, then you can use chmod with find command. You can use find command to find all the files/folders you need specifically, and then call chmod command on them to set their permissions, using -exec option. Here are two commands to set different permissions for files and folders in /home/ubuntu.
$ find /home/ubuntu -type d -exec chmod 755 {} \; $ find /home/ubuntu -type f -exec chmod 644 {} \;
The first command will assign permission 755 to all directories in /home/ubuntu. The second find command assigns permission 644 to all files.
Similarly, here is an example to set permission 755 to all .deb files in /home/ubuntu.
$ find /home/ubuntu/*.deb -type f -exec chmod 644 {} \;
In this case, you don’t have the danger of unknowingly changing permissions of files & folders.
You can also use symbolic notation, with find command, to change the file permissions.
$ find /home/ubuntu -type d -exec chmod u=rwx,go=rx {} \; $ find /home/ubuntu -type f -exec chmod u=rw,go=r {} \;
You can also use xargs command with find command, to construct separate chmod statements for each file & folder present in result of find command. Here are the above commands using xargs and find.
$ find /home/ubuntu -type d -print0 | xargs -0 chmod 755 $ find /home/ubuntu -type f -print0 | xargs -0 chmod 644
Basically, using find with chmod gives you better control over changing permissions of multiple files, unlike using chmod recursively.
In this article, we have learnt how to recursively change file & folder permissions in Linux. We have also learnt how to use chmod with find command to change permissions of multiple files & folders, without using recursion. You can use either of these approaches, as per your requirement.
Also read:
How to Get Size of Directory in Linux
How to Calculate Time Difference in Python
How to Measure Time Taken By Python Program
How to Encrypt & Decrypt Files in Python
How to Recursively Download Files & Folders in Wget
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.