How do I change permissions for a folder and its subfolders/files?
Categories:
Mastering Linux File Permissions: Changing Folder and Subfolder Access

Learn how to effectively manage file and directory permissions in Linux using chmod
and chown
, including recursive changes for subfolders and files.
Understanding and managing file permissions is a fundamental skill for any Linux user or administrator. Incorrect permissions can lead to security vulnerabilities or prevent legitimate users and applications from accessing necessary resources. This article will guide you through the process of changing permissions for a folder and all its contents (subfolders and files) using the chmod
and chown
commands, explaining the concepts behind them.
Understanding Linux File Permissions
Linux permissions are divided into three main categories: read (r), write (w), and execute (x). These permissions can be assigned to three types of users: the owner of the file, the group associated with the file, and others (everyone else). Each permission type has a numerical value: read is 4, write is 2, and execute is 1. These values are summed to represent the permission set for each user type.
flowchart TD A[File/Directory] --> B{Owner Permissions} A --> C{Group Permissions} A --> D{Others Permissions} B --> B1["Read (4)"] B --> B2["Write (2)"] B --> B3["Execute (1)"] C --> C1["Read (4)"] C --> C2["Write (2)"] C --> C3["Execute (1)"] D --> D1["Read (4)"] D --> D2["Write (2)"] D --> D3["Execute (1)"] style B fill:#f9f,stroke:#333,stroke-width:2px style C fill:#bbf,stroke:#333,stroke-width:2px style D fill:#bfb,stroke:#333,stroke-width:2px
Breakdown of Linux File Permissions
Changing Permissions Recursively with chmod
The chmod
command is used to change file and directory permissions. To apply changes to a directory and all its contents, you need to use the -R
(recursive) option. It's important to differentiate between permissions for files and directories. Typically, directories need execute permission to be entered, while files need execute permission to be run as programs.
# Set read, write, and execute for owner; read and execute for group and others for a directory and its contents
chmod -R 755 /path/to/your/folder
# Set read and write for owner; read-only for group and others for files
# and read, write, execute for owner; read and execute for group and others for directories
find /path/to/your/folder -type d -exec chmod 755 {} +
find /path/to/your/folder -type f -exec chmod 644 {} +
Examples of chmod -R
and find
with chmod
find
with chmod
allows for more granular control, applying different permissions to files and directories within the same recursive operation. This is often preferred over a blanket chmod -R
.Changing Ownership Recursively with chown
While chmod
handles permissions, chown
is used to change the owner and/or group of files and directories. Like chmod
, it also supports the -R
(recursive) option to apply changes to all contents within a specified directory. This is crucial when moving files between users or after restoring backups.
# Change owner to 'newuser' and group to 'newgroup' for a folder and its contents
chown -R newuser:newgroup /path/to/your/folder
# Change only the owner to 'newuser'
chown -R newuser /path/to/your/folder
# Change only the group to 'newgroup'
chown -R :newgroup /path/to/your/folder
Examples of chown -R
for changing ownership
chmod -R
or chown -R
on critical system directories (e.g., /
, /etc
, /usr
). Incorrect permissions or ownership can render your system unbootable or insecure.1. Identify the Target Directory
Determine the full path to the folder for which you want to change permissions or ownership. For example, /var/www/html/mysite
.
2. Choose the Right Permissions/Ownership
Decide on the appropriate numeric permissions (e.g., 755 for directories, 644 for files) and the new owner/group (e.g., www-data:www-data
for web servers).
3. Apply Ownership Changes (if needed)
If you need to change the owner or group, use sudo chown -R newuser:newgroup /path/to/your/folder
first. This ensures the new owner has control before permissions are set.
4. Apply Permissions for Directories
Use sudo find /path/to/your/folder -type d -exec chmod 755 {} +
to set appropriate permissions for all subdirectories.
5. Apply Permissions for Files
Use sudo find /path/to/your/folder -type f -exec chmod 644 {} +
to set appropriate permissions for all files.
6. Verify Changes
After applying changes, use ls -lR /path/to/your/folder
to recursively list the contents and verify that the permissions and ownership are set correctly.