Bash Script to Set Sensible Directory/File Permissions

Every once in a while a directory structure's permissions just get messy (or an archive I download and extract has stupid permissions, like making every single file executable). So I'll occasionally want to chmod 755 all directories and chmod 644 all files. If you're new to file system permissions in Linux, let me translate that goal. For every directory I want the owner to be able to read/write/execute (execute allows to cd into directory) and both group and all others to have read/execute permissions. For every file I want the owner to have read/write permissions and both the group and all others to have read-only permission. The Ubuntu Community Documentation has a good File Permissions page that covers this in more detail.

That same documentation has a Recursive chmod using find, pipemill, and sudo section that details precisely the two commands I need, but I hate to waste time finding this link and carefully remembering what I want to do. So, I've whipped up a simple Bash script that simply accepts a directory path as its only parameter and prompts me to confirm before making the permission changes. Here it is.


# exit this bash script if any statement returns a non-true return value (same as: set -e)
set -o errexit

if [ -z "$1" ]; then
    echo "ERROR: Must pass parameter of path to directory (which will have permissions set accordingly; 755 directories and 644 files)."
    echo "USAGE: $0 /path/to/directory"
    exit 1

echo "Working recursively with directory $1 this script will chmod 755 all directories and chmod 644 all files."

read -p "Okay to continue (y/n)? "
if [ "$REPLY" != "y" ]; then
    exit 1

echo "Starting..."

find "$1" -type d -print0 | xargs -0 chmod 755
find "$1" -type f -print0 | xargs -0 chmod 644

echo "Done."

exit 0

Since I'll often need to run this script as root, I've taken a few extra steps to protect it by making it owned and executable only by root:

sudo chown root:root
sudo chmod 744

So, from the directory containing this script (as you can see I've named mine, here's an example usage:

sudo ./ ~/directory-to-clean-up-permissions

If your directory has strange owners and/or groups set, you can quickly clean that up recursively as well. For example:

sudo chown -R jkrug:jkrug ~/directory-to-clean-up-permissions

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Stan W's Gravatar Thanks for the tips. You helped me tweak my own scripts.
# Posted By Stan W | 10/19/11 6:58 AM
Jamie Krug's Gravatar @Stan: Glad you found this useful. Thanks for the feedback.
# Posted By Jamie Krug | 10/20/11 4:03 PM
Edward Beckett's Gravatar I don't know how many times a day I'm running find . -type (d|f) -print0 | xargs -o sudo chmod foo ....

That's going into my .bashrc ...

# Posted By Edward Beckett | 7/4/12 4:09 PM
f00d's Gravatar Thanks dude, this helps me a loot!
# Posted By f00d | 6/26/13 4:40 AM
BlogCFC was created by Raymond Camden.