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"
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
echo "EXITING -- NO ACTION TAKEN."
find "$1" -type d -print0 | xargs -0 chmod 755
find "$1" -type f -print0 | xargs -0 chmod 644
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 chmod 744 setperms.sh
So, from the directory containing this script (as you can see I've named mine setperms.sh), here's an example usage:
If your directory has strange owners and/or groups set, you can quickly clean that up recursively as well. For example: