03/24/2022 23:57 | Category: linux

Tags: bashubuntufilesystempermission

changing ownership and permissions of files or directories recursively

Changing file permissions is very tedious. Often times I run into issues when using code generators to create an application or set of files, wherein the app has only root permissions and my code formatters can't run against them.

For example, when using Pre-Commit I ran into a Permission Denied error for files that were owned by the user:group root:root. These were project files that were generated by Django when running a startapp <name> command.

To resolve the issue, I usually want to set everything to 664 for files and 775 for directories.

Doing this individually with a chwon <user name:group name> <file/name.txt> or chmod 664 <file/name.txt> is annoying.

Link to a good resource on file permissions

Recursively changing file/directory permissions

To do this action recursively, we can execute the below:

Example with file permissions:

# files
find <some/file/path> -type f -exec chmod 664 {}\;

# directories
find <some/directory> -type d -exec chmod 775 {}\;

Example with ownership:

# files
find subscriptions/ -type f -exec chown <user name:group name> {}\;

# directories
find subscriptions/ -type d -exec chown <user name:group name> {}\;