Recursively Change File Permissions in Ubuntu Linux – 2 Easy Steps
In this guide from Orcacore, we will teach you How To Recursively Change File Permissions in Ubuntu Linux. As every Linux user and Administrator knows, chmod
(Change Mode) is one of the most useful commands in Linux. It can be used for changing file permissions of both files and directories. This allows you to control whether a file is executable, readable, writable, and so on. Setting the correct file permissions in your Ubuntu Linux system is essential for security and proper functionality.
This tutorial will focus on changing file permissions recursively. Let’s first understand Linux file permissions, then proceed to the steps for Recursively Change File Permissions in Ubuntu Linux.
What Are Linux File Permissions?
If you are a Linux user, you have likely encountered the "Permissions denied" error while trying to edit or execute a file. This indicates that you lack the necessary permissions to perform the intended action. File permissions in Linux control who can access specific files and directories, and what actions they can perform.
This is where the powerful chmod
command comes into play. With chmod
, users with sudo
privileges can modify file permissions to grant or restrict access.
Now, let’s dive into the steps required to Recursively Change File Permissions in Ubuntu Linux.
To Recursively Change File Permissions in Ubuntu Linux, you must have root access or be a non-root user with sudo
privileges on your server. We will use Ubuntu 22.04 as an example. You can consult this guide on Initial Server Setup with Ubuntu 22.04.

Step 1 – Changing File Permissions Recursively in Ubuntu
Let’s delve into what it means to Recursively Change File Permissions in Ubuntu Linux. When using the recursive mode, applying a permission change to a directory will affect all files and subdirectories within that directory. In essence, every file residing within the directory will inherit the specified permissions recursively.
For example, consider a directory named orca
. We assign the permission 700
to this directory:
chmod 700 orca
This means that the owner has read, write, and execute permissions, while the group and others have no permissions. This permission setting will be represented as drwx------
.
Now, let’s say the orca
directory contains the following files:
ls -la
**<mark>Output</mark>**
-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 observe, the files within the orca
directory are not affected by changing the permission of the directory itself. The file owners have read and write permissions, while groups and others have only read permissions.
To recursively change the permissions in your Ubuntu directory, you need to use the -R
option in your chmod
command. To recursively change the permissions of the orca
directory, execute the following command:
chmod -R 700 orca
Now, if you check the file permissions inside the directory, you’ll notice that the files have inherited the new permissions:
**<mark>Output</mark>**
-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
It’s important to note that files and directories should ideally not have identical permissions. Therefore, you can change permissions based on specific file types within a directory recursively. The find
command can be used for this purpose.
For instance, the following two commands will search for directory (d
) and file (f
) types within the specified directory and set their respective 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 within the starting directory. The -exec
option tells find
to execute the chmod -R
function on each found item, just as you would normally use it.
With this option, you can easily search for specific file types within a directory and set their permissions recursively, providing granular control over your file system.
Conclusion
At this point, you’ve learned how to Recursively Change File Permissions in Ubuntu Linux. You’ve seen that using the -R
option with the chmod
command achieves this. However, using this option can be dangerous and should be used with caution. It’s generally better practice to use the find
command in conjunction with chmod
to set permissions based on file types, as files and directories shouldn’t typically have the same permissions.
Hope you enjoyed this guide. We appreciate your ideas and suggestions. Please leave a comment.
Also, you might find these articles interesting:
Fix APT Error Download is performed unsandboxed as root
Adding a Directory to Debian 12 System Path
Alternative Solutions for Recursively Changing File Permissions
While the original article presents two effective methods, let’s explore two alternative approaches to recursively changing file permissions in Ubuntu Linux, offering different levels of control and flexibility.
Alternative 1: Using chown
to Change Ownership Recursively
While chmod
is used to change permissions, chown
is used to change the ownership of files and directories. Sometimes, incorrect ownership can lead to permission issues. Changing ownership recursively can resolve access problems in some scenarios.
Explanation:
The chown
command changes the user and/or group ownership of a file or directory. The -R
option makes the change recursive, affecting all files and subdirectories within a given directory.
Code Example:
sudo chown -R newuser:newgroup /path/to/directory
In this example:
sudo
is used because changing ownership usually requires elevated privileges.chown
is the command to change ownership.-R
makes the change recursive.newuser:newgroup
specifies the new owner and group. Replacenewuser
andnewgroup
with the desired user and group names. If you only want to change the user, you can omit the:newgroup
part. If you only want to change the group, use:newgroup
./path/to/directory
is the directory you want to affect recursively. Replace this with the actual path.
Example Scenario:
Imagine you have a web server directory where all the files are owned by the root user. Your web server process runs as the www-data
user. As a result, the web server cannot write to these files. Recursively changing the ownership to www-data:www-data
using the above command would resolve this issue.
Important Considerations:
- Always be cautious when changing ownership, especially recursively. Incorrect ownership can lead to security vulnerabilities or system instability.
- Make sure you understand the user and group structure of your system before using
chown
.
Alternative 2: Using a Script for Complex Permission Logic
For more complex scenarios where simple chmod
or chown
commands are insufficient, you can create a script to handle the permission changes. This offers the greatest flexibility.
Explanation:
A script allows you to implement custom logic for determining which files and directories should have their permissions changed and how. You can incorporate conditional statements, loops, and other scripting constructs to tailor the permission changes to your specific needs.
Code Example:
#!/bin/bash
# Define the directory to process
DIRECTORY="/path/to/directory"
# Function to set permissions based on file type
set_permissions() {
local file="$1"
if [ -d "$file" ]; then
# Directory: set permissions to 755
chmod 755 "$file"
echo "Setting directory permissions to 755: $file"
elif [ -x "$file" ]; then
# Executable file: set permissions to 755
chmod 755 "$file"
echo "Setting executable file permissions to 755: $file"
else
# Regular file: set permissions to 644
chmod 644 "$file"
echo "Setting regular file permissions to 644: $file"
fi
}
# Recursively process each file and directory
find "$DIRECTORY" -print0 | while IFS= read -r -d $' ' file; do
set_permissions "$file"
done
echo "Permission changes complete."
How This Script Works:
- Shebang:
#!/bin/bash
specifies the interpreter for the script (Bash). DIRECTORY
Variable: Defines the target directory.set_permissions()
Function: This function takes a file path as input and determines whether it’s a directory, an executable file, or a regular file. It then sets the appropriate permissions usingchmod
and prints a message.find
Command:find "$DIRECTORY" -print0
finds all files and directories within the specified directory and prints their names separated by null characters (