Language
Category
Search

Change permissions only for directories or files recursively

Useful commands in the Linux terminal to fix permissions on large file and directory structures

Change permissions only for directories or files recursively
At Terminal By Rudi Drusian Lange
Published on
Last updated

Before you start

Make sure you know what you are doing, changing permissions, especially recursively, can cause serious problems, making files, programs and folders inaccessible. Undoing changes can be a lot of work, each structure has particularities in its permissions.

Changing for everything

Using the -R parameter of chmod command it is possible to recursively apply the same permission to all files and folders.

$ Shell

chmod -R 755 /path/to/base/dir

For files or folders separately, see below.

Directories

To change permission of all subdirectories recursively from a base directory.

$ Shell

# Permission 755 (drwxr-xr-x)
find /path/to/base/dir -type d -exec chmod 755 {} +

# Other way
chmod 755 $(find /path/to/base/dir -type d)

# For directories with too many files
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755

# Uses the current folder as base directory
find . -type d -exec chmod 755 {} +

Permissions 755 (drwxr-xr-x) were used, which is more common for directories and is the default option when creating new folders.

The dot (.) represents the current directory and can be used in place of the path to the base directory in any of the above commands.

Files

To change the permissions of all files in the base directory defined in the command and its subdirectories recursively:

$ Shell

# Permission 644 (-rw-r--r--)
find /path/to/base/dir -type f -exec chmod 644 {} +

# Other way 
chmod 644 $(find /path/to/base/dir -type f)

# For directories with many files
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

# Uses the current folder as base directory
find . -type f -exec chmod 644 {} + 

Permissions 644 (-rw-r--r--) were applied, which is the default used when creating text files. Using this permission on a script will remove its execution permission which will make it lose its functionality.

Sources

This is not my original language and I don't speak it very well. I used my little knowledge and translators tools to compose the text of this article. Sorry for possible spelling or grammatical errors, suggestions for corrections are appreciated and can be sent to the contact email in the footer of the site. My intention is to share some knowledge and I hope this translation is good enough.