Share your love
Recursively Change File Permissions in Ubuntu Linux
In this guide from the Orcacore website, we want to teach you How To Recursively Change File Permissions in Ubuntu Linux. As every Linux user and Administrator must know, Chmod (Change Mode) is one of the most useful commands in Linux. It can be used for changing file permissions and directories. This will help you to make the file executable, readable, writable, etc. This is so important to set the correct file permissions in your Ubuntu Linux.
This tutorial will cover the subject of changing file permissions recursively. Let’s get familiar with file permissions first, then, proceed to the next steps to complete this guide.
Linux File Permissions
If you are a Linux user, you may face the error “Permissions denied” while you are editing a file or executing a file. This means that you don’t have the right permissions to do your task. File permissions in Linux will ensure who can access the files and directories.
Here one of the most Linux commands comes in. By using the chmod command, sudo users can change the file permissions.
Now you can follow the steps below to see how you can recursively change file permissions.
Steps To Recursively Change File Permissions in Ubuntu Linux
To complete this guide, you must have access to your server as a root or non-root user with sudo privileges. Here we use Ubuntu 22.04 as an example to show you the guide steps. You can visit this guide on Initial Server Setup with Ubuntu 22.04.
Step 1 – Changing File Permissions Recursively in Ubuntu
Let’s understand the recursive in the file permissions. When recursive mode comes in, it means that when you want to set permission for your directory, it will affect all the files in the directory. In this way, every file that exists in the directory will get the permissions recursively.
For example, we have a directory named orca. We set the permission 700 to our directory.
chmod 700 orca
It means that the owner has all the permissions to read, write, and execute. But the Group and Others have no permissions. The character of this will represent drwx——.
Inside the orca directory, we have the following files:
ls -la
Output
-rw-r--r-- 1 root root 0 Sep 4 08:29 file2.txt
-rw-r--r-- 1 root root 0 Sep 4 08:29 testfile.txt
As you can see, the files aren’t affected by changing the permission of the orca directory. Files owners have read and write permissions, and groups, and others have read permissions.
If you want to recursively change the permissions in your Ubuntu, you must use the -R option in your command. For example, to recursively change our orca directory permission, run the command below:
chmod -R 700 orca
At this point, when you check your file permissions inside the directory, you will see that the files have the same permissions:
Output
-rwx------ 1 root root 0 Sep 4 08:29 file2.txt
-rwx------ 1 root root 0 Sep 4 08:29 testfile.txt
Step 2 – Changing Files Types Permissions Recursively with find Command
You must keep in mind that files and directories shouldn’t have the same permissions. So in this case, you can change permissions by searching for specific types and changing their permission recursively. For this purpose, you can use the find command.
For example, the two following commands will search for the d and f types in the directory and set the following permissions:
# find ~/home/tmp/orca -type d -exec chmod -R 755 {} \;
# find ~/home/tmp/orca -type f -exec chmod -R 644 {} \;
In this example the find utility searches for files and directories inside a specific starting directory, and with the -exec option we tell it to run the chmod -R function as normally we would.
That’s it, you are done. With this option, you can easily search for your desired file types in your desired directory and set the permission recursively.
Conclusion
At this point, you have learned to Recursively Change File Permissions in Ubuntu Linux. As you saw, you can use the -R option in your chmod command to do this. But it is recommended not to do this. It is better to use the file types with the find command. Files and Directories shouldn’t have the same permissions.
Hope you enjoy it. We will be glad to share your ideas and suggestions. Please comment for Us.
Also, you may be interested in these articles: