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.

#!/bin/bash

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

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 Reply

Your email address will not be published. Required fields are marked *