Setting different permission bits for files and directories

If you want to recursively set the permission bits for a whole directory structure, you need to differentiate between files and directories. Directories need to be executable so you can enter them. Files should generally be non-executable.

The Swiss Army Knife command line tool find can help you with that. I wrote a small bash script that simplifies this task.


if [ "$#" != "3" ]; then
echo "Sets different permission bits for directories and files"
echo "Example: setrights /path 775 664"
exit 1

find $1 -type d -exec chmod $2 '{}' \;
find $1 -type f -exec chmod $3 '{}' \;
exit 0

If you save this script as /usr/local/bin/setrights and mark it as executable (chmod +x setrights), it will save you some typing.

Of course you can also use the symbolic permission notation: setrights /path +x -x will make all directories executable and all files non-executable.

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.