How to use chmod recursively on Linux?

chmod is a Linux command that changes file and directory permissions. When working with complex directory structures, you often need to modify permissions not just for a single file, but for all files and subdirectories within a directory tree. This is where the recursive option (-R) becomes essential.

In Linux, file permissions control who can read, write, or execute files and directories. The chmod command allows you to modify these permissions using either symbolic notation (like rwx) or numeric notation (like 755).

Basic chmod Syntax

chmod [OPTIONS] MODE file...
chmod [OPTIONS] NUMERIC_MODE file...
chmod [OPTIONS] --reference=RFILE file...

Recursive chmod Syntax

To apply chmod recursively to all files and subdirectories, use the -R (recursive) flag:

chmod -R MODE directory_name

Common Options

-R, --recursive    change files and directories recursively
-v, --verbose      output a diagnostic for every file processed
-c, --changes      like verbose but report only when a change is made
-f, --silent       suppress most error messages
--reference=RFILE  use RFILE's mode instead of MODE values

Examples

Example 1 − Setting Full Permissions Recursively

Before applying chmod recursively:

$ ls -la myproject/
total 24
drwxr-xr-x 4 user staff  128 Jul  8 19:05 .
drwxr-xr-x 8 user staff  256 Jul  8 19:04 ..
-rw-r--r-- 1 user staff  446 Sep 23 1998 config.txt
drwxr-xr-x 3 user staff   96 Jul  7 17:42 scripts
drwxr-xr-x 2 user staff   64 Jul  8 19:05 docs
-rw-r--r-- 1 user staff  718 Jul 12 18:48 readme.md

Apply 755 permissions recursively:

chmod -R 755 myproject/

After applying chmod recursively:

$ ls -la myproject/
total 24
drwxr-xr-x 4 user staff  128 Jul  8 19:05 .
drwxr-xr-x 8 user staff  256 Jul  8 19:04 ..
-rwxr-xr-x 1 user staff  446 Sep 23 1998 config.txt
drwxr-xr-x 3 user staff   96 Jul  7 17:42 scripts
drwxr-xr-x 2 user staff   64 Jul  8 19:05 docs
-rwxr-xr-x 1 user staff  718 Jul 12 18:48 readme.md

Example 2 − Different Permissions for Files vs Directories

Often you want different permissions for files and directories. Use separate commands:

# Set directories to 755 (rwxr-xr-x)
find /path/to/directory -type d -exec chmod 755 {} \;

# Set files to 644 (rw-r--r--)
find /path/to/directory -type f -exec chmod 644 {} \;

Example 3 − Using Symbolic Notation

# Add execute permission for owner recursively
chmod -R u+x myproject/

# Remove write permission for group and others recursively
chmod -R go-w myproject/

# Set read and write for owner, read for group and others
chmod -R u=rw,go=r myproject/

Common Permission Codes

Numeric Symbolic Description Use Case
755 rwxr-xr-x Owner: full, Group/Others: read+execute Directories, executable scripts
644 rw-r--r-- Owner: read+write, Group/Others: read only Regular files
600 rw------- Owner: read+write, Group/Others: no access Private files
777 rwxrwxrwx Full permissions for everyone Temporary directories (use carefully)

Best Practices

  • Test first − Use chmod -R --dry-run or test on a copy before applying to important files

  • Use appropriate permissions − Don't use 777 unless absolutely necessary for security reasons

  • Separate file and directory permissions − Use find commands when you need different permissions for files vs directories

  • Use verbose mode − Add -v flag to see what changes are being made

Conclusion

The chmod -R command is essential for managing permissions across directory trees in Linux. Use numeric codes like 755 for directories and 644 for files, or symbolic notation for more granular control. Always consider security implications and test changes before applying them to critical system files.

Updated on: 2026-03-17T09:01:38+05:30

502 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements