How to chmod 755 all directories but no file (recursively) ?
Inversely, how to chmod only files (recursively) but no directory ?
|
How to chmod 755 all directories but no file (recursively) ? Inversely, how to chmod only files (recursively) but no directory ? |
|||||
|
|
To recursively give directories read&execute privileges:
To recursively give files read privileges:
Or, if there are many objects to process:
Or, to reduce
|
|||||
|
|
A common reason for this sort of thing is to set directories to 755 but files to 644. In this case there's a slightly quicker way than nik's
Meaning:
The important thing to note here is that uppercase
In other words, chmod u+X on a file won't set the execute bit; and g+X will only set it if it's already set for the user. |
|||||||||||||||||||||
|
|
If you want to make sure the files are set to 644 and there are files in the path which have the execute flag, you will have to remove the execute flag first. +X doesn't remove the execute flag from files who already have it. Example:
Update: this appears to fail because the first change (ugo-x) makes the directory unexecutable, so all the files underneath it are not changed. |
|||||
|
|
I decided to write a little script for this myself. Recursive chmod script for dirs and/or files — Gist It basically does the recursive chmod but also provides a bit of flexibility for command line options (sets directory and/or file permissions, or exclude both it automatically resets everything to 755-644). It also checks for a few error scenarios. I also wrote about it on my blog. |
||||
|
|
|
Try this python script; it requires no spawning of processes and does only two syscalls per file. Apart from an implementation in C, it will probably be the fastest way of doing it (I needed it to fix a filesystem of 15 million files which were all set to 777)
In my case, a try/catch was required around the last chmod, since chmodding some special files failed. |
|||
|
|
|
To recursively give directories "read" and "execute" privileges:
|
||||
|
|
|
You could use the following bash script as an example. Be sure to give it executable permissions (755). Simply use ./autochmod.sh for the current directory, or ./autochmod.sh <dir> to specify a different one.
|
|||||||||
|