Change File Permissions Recursively in Linux

When working with filesystem in the Linux, there might be a situation that unable to create or edit file due insufficient permissions. This problem can be solved by setting the correct file permissions or ownership. This tutorial shows how to change file permissions recursively In Linux.

Create few directories and files for testing:

mkdir -p docs/dir_1 docs/dir_2
echo "Hello world" > docs/dir_1/test.txt
echo "Hello world" > docs/dir_2/test.txt

To change file permissions recursively, use chmod command with -R option. For example, the following command sets the permissions of all files and subdirectories under the docs directory to 755:

sudo chmod -R 755 docs

755 means read, write, and execute access to the owner (7), read and execute access for group members (5) and other users (5).

In many cases, we need to set separate permissions to files and directories. To achieve it, use find command to locate files and directories and chmod command to set the permissions.

For example, the following command recursively sets directory permissions to 755:

sudo find docs -type d -exec chmod 755 {} \;

This command recursively sets file permissions to 644:

sudo find docs -type f -exec chmod 644 {} \;

644 means read and write access to the owner (6), read only access for group members (4) and other users (4).

Leave a Comment

Your email address will not be published.