When you need to manage file permissions on Linux or macOS, the chmod ordered is the way to go. However, the recursive chmod option allows you to apply the chmod command to all files in a directory and its subdirectories.
In this article, we’ll explain how to use recursive chmod to easily change file permissions on macOS and Linux. We’ll also explain how to use the “X” argument to preserve file execution privileges and show you everything you need to know to use the chmod command like a pro.
How to use recursive chmod to change file permissions on macOS and Linux
If you’re just looking for a quick answer, here’s how to use the chmod command to set permissions for folders and files separately. In the example below, files are made executable for owners only if they had an executable bit before the permission change.
find . -type d -exec chmod 755 {} + # gives owners read, write, and execute permissions, everyone else just read and execute ones find . -type f -exec chmod u=rw,og=r {} + #gives owners read and write permissions, read permissions to others
Need a little more detail? Continue reading.
Understand a basic recursive chmod
If you just want to change permissions in a folder and all of its subfolders (and subfolders without them, etc.), you can just use chmod
with the -R
flag:
chmod -R 755 .
When using the ls command to view file permissions, we see that chmod-R gives the owning group and others read and execute permissions to the current folder and its subfolders
If what you are modifying requires admin rights, you will need to preface your command with sudo
as in the example below:
sudo chmod -R 755 .
Also, if you want to change the permissions for a path you’re not currently in, you’ll need to replace .
with your desired relative or absolute path. Here is an example command below:
sudo chmod -R 755 ./subdir.
For the purposes of this article, we’ll assume that you know how to specify permissions with chmod
and we won’t cover that here.
Using the “X” Argument to Preserve File Execution Privileges
A useful permission symbol to use with chmod
is X
instead of standard executable mode. This symbol allows you to add executable permission only to files for which this executable bit was previously set for at least one class of users (regardless of class), as well as to all folders.
The x
The symbol can be used to set the executable bit if enabled for any user class. You can use it like this:
chmod -R u+rwX,go+rX .
Note that this argument is not globally supported. On most Linux systems you will find that chmod
supports operations with =,+and –. On macOS, however, only the + operation is supported.
Using the find command to differentiate between files and folders
If you want to grant different permissions to different sets of files or folders, use the find
ordered. More often than not, you’ll want to grant files different permissions than folders.
Here is an example command below to illustrate this: +
at the end of the command indicates find
to chain all file paths and run chmod
once on each of them. If we had used ;
instead, it would have led chmod
to be executed once per file, which would be much less efficient.
find . -type d -exec chmod 755 {} +find . -type f -exec chmod u=rw,go=r {} +
You can of course use find
to select specific files and folders as needed. This includes folders with a certain prefix to files with a specific extension. This article will not go into detail on how to use the find
command, however.
Use xargs if you don’t want to use the “-exec” option
Maybe the -exec
option of the to find The command may be buggy on your system or even completely absent (it was added to GNU findutils in 2005). Or you might want to speed up the execution a bit by using all your CPU cores with parallel pipeline execution.
For any of these reasons, you can try using the xargs
order instead:
find . -type d -print0 | xargs -0 chmod 755find . -type f -print0 | xargs -0 chmod u=rw,go=r
It is important to use the -print0
parameter for find
and -0
parameter for xargs
, so that filenames with spaces are handled correctly. It’s because -print0
makes find
use a null character instead of a newline, and -0
recount xargs
to consider a null character as a separator.
Note that these flags are part of GNU findutils and may be missing on some non-Linux operating systems.
Conclusion
Now you should have all the tools you need to change permissions inside a folder and all of its subfolders using the find
command to apply changes to specifically chosen files or folders. If you have any questions or maybe suggestions on what to cover next, please drop us a comment below.
When you need to manage file permissions on Linux or macOS, the chmod ordered is the way to go. However, the recursive chmod option allows you to apply the chmod command to all files in a directory and its subdirectories.
In this article, we’ll explain how to use recursive chmod to easily change file permissions on macOS and Linux. We’ll also explain how to use the “X” argument to preserve file execution privileges and show you everything you need to know to use the chmod command like a pro.
How to use recursive chmod to change file permissions on macOS and Linux
If you’re just looking for a quick answer, here’s how to use the chmod command to set permissions for folders and files separately. In the example below, files are made executable for owners only if they had an executable bit before the permission change.
find . -type d -exec chmod 755 {} + # gives owners read, write, and execute permissions, everyone else just read and execute ones find . -type f -exec chmod u=rw,og=r {} + #gives owners read and write permissions, read permissions to others
Need a little more detail? Continue reading.
Understand a basic recursive chmod
If you just want to change permissions in a folder and all of its subfolders (and subfolders without them, etc.), you can just use chmod
with the -R
flag:
chmod -R 755 .
When using the ls command to view file permissions, we see that chmod-R gives the owning group and others read and execute permissions to the current folder and its subfolders
If what you are modifying requires admin rights, you will need to preface your command with sudo
as in the example below:
sudo chmod -R 755 .
Also, if you want to change the permissions for a path you’re not currently in, you’ll need to replace .
with your desired relative or absolute path. Here is an example command below:
sudo chmod -R 755 ./subdir.
For the purposes of this article, we’ll assume that you know how to specify permissions with chmod
and we won’t cover that here.
Using the “X” Argument to Preserve File Execution Privileges
A useful permission symbol to use with chmod
is X
instead of standard executable mode. This symbol allows you to add executable permission only to files for which this executable bit was previously set for at least one class of users (regardless of class), as well as to all folders.
The x
The symbol can be used to set the executable bit if enabled for any user class. You can use it like this:
chmod -R u+rwX,go+rX .
Note that this argument is not globally supported. On most Linux systems you will find that chmod
supports operations with =,+and –. On macOS, however, only the + operation is supported.
Using the find command to differentiate between files and folders
If you want to grant different permissions to different sets of files or folders, use the find
ordered. More often than not, you’ll want to grant files different permissions than folders.
Here is an example command below to illustrate this: +
at the end of the command indicates find
to chain all file paths and run chmod
once on each of them. If we had used ;
instead, it would have led chmod
to be executed once per file, which would be much less efficient.
find . -type d -exec chmod 755 {} +find . -type f -exec chmod u=rw,go=r {} +
You can of course use find
to select specific files and folders as needed. This includes folders with a certain prefix to files with a specific extension. This article will not go into detail on how to use the find
command, however.
Use xargs if you don’t want to use the “-exec” option
Maybe the -exec
option of the to find The command may be buggy on your system or even completely absent (it was added to GNU findutils in 2005). Or you might want to speed up the execution a bit by using all your CPU cores with parallel pipeline execution.
For any of these reasons, you can try using the xargs
order instead:
find . -type d -print0 | xargs -0 chmod 755find . -type f -print0 | xargs -0 chmod u=rw,go=r
It is important to use the -print0
parameter for find
and -0
parameter for xargs
, so that filenames with spaces are handled correctly. It’s because -print0
makes find
use a null character instead of a newline, and -0
recount xargs
to consider a null character as a separator.
Note that these flags are part of GNU findutils and may be missing on some non-Linux operating systems.
Conclusion
Now you should have all the tools you need to change permissions inside a folder and all of its subfolders using the find
command to apply changes to specifically chosen files or folders. If you have any questions or maybe suggestions on what to cover next, please drop us a comment below.