It is not uncommon that one copies files from a non-Linux filesystem to one’s Linux computer. For example, many digital cameras use FAT or NTFS, and it makes sense to format a USB thumb drive as FAT, so it can be read by various other devices (like game consoles, cameras, etc…).
The issue with copying files from a non-Linux filesystem is that permissions will typically be 777 on both files and directories, and this is certainly not what a Linux user wants. Typically, a Linux user will want (non-executable) files to have permissions set to 644 (-rw-r--r--
) and directories to 755 (drwxr-xr-x
).
It gets tedious to reset permissions manually when copying a bunch of files (e.g. a few hundred images from a digital camera) using
chmod 644 file
especially if the files are in multiple directories. The two proper commands that reset permissions of all files and directories in a given directory are:
find directory -type f -exec chmod 644 {} \;
find directory -type d -exec chmod 755 {} \;
The problem with them is obvious: they are very verbose, which makes them difficult to remember, annoying to type, and prone to making an error (which could be disastrous).
My solution is this simple utility script, which I call reset-permissions
:
#!/bin/bash
# A simple bash script that sets permissions of directories to 755
# and files to 644.
# Version 1.1
# by Krešimir, 2020
###############################################################################
# Detect witdth of the terminal, so that folding works nicely
if ! tcols=$(tput cols 2> /dev/null); then
tcols=60 # assume width is 60 if tput does not work
fi
print_help() {
fold -sw $tcols << EOF
A simple bash script that sets permissions of directories to 755 and files to 644.
If used improperly, this script can break stuff. There is no warranty.
Usage:
reset-permissions file
Sets permissions of the file to 644.
reset-permissions directory
Sets permissions of the directory to 755
without touching its contents.
reset-permissions -R directory
Recursively descend into directory setting
permissions of all files within to 644
and all directories to 755.
Running as root is disabled by default. Use 'chmod' instead, or modify the script file.
EOF
}
# Check if run as root. Comment out at your own risk.
if [ "$EUID" -eq 0 ]; then
>&2 fold -sw $tcols <<< "Error: It is dangerous to run this script as root. Use 'chmod'\
instead, or edit the script file to remove this check."
exit 9
fi
# End of check.
# Recursive mode
if [ "$1" = "-R" ]; then
if [ "$2" != "" ]; then
if [ -d "$2" ]; then
DIRECTORY=$(readlink -f -n "$2")
echo "Permissions will be set to 755 for"
echo -e "\033[1m$DIRECTORY\033[0m"
echo "and for all of its subdirectories, recursively."
fold -sw $tcols <<< "Persmissions will be set to 644 for all files contained in\
it and in its subdirectories, recursively."
read -p "Are you sure [y/N]? " response
if [[ $response =~ ^(yes|y|Y|Yes)$ ]]; then
find "$DIRECTORY" -type f -exec chmod 644 {} \;
find "$DIRECTORY" -type d -exec chmod 755 {} \;
exit 0
fi
echo "Cancelled."
exit 0
fi
>&2 fold -sw $tcols <<< "Error: $2 is not a directory."
exit 1
fi
>&2 echo "Error: Missing path to directory after -R."
exit 2
fi
# Unknown flag, print help
if [[ $# -eq 0 || "$1" =~ ^- ]]; then
print_help;
exit 0
fi
# Single file or directory mode
for FILE in "$@"; do
if [ -f "$FILE" ]; then
chmod 644 "$FILE"
elif [ -d "$FILE" ]; then
chmod 755 "$FILE"
else
>&2 fold -sw $tcols <<< "Error: $FILE is neither a file nor a directory."
exit 3
fi
done
Installation: just copy and paste the code into a file named reset-permissions
, save it in a directory listed in your PATH
variable, and make it executable with: chmod u+x reset-permissions
.
Features:
- does what it is supposed to do: it sets permissions of files to 644 and directories to 755
- it can operate on a single file/directory or descend into a directory recursively
- it has a built-in help (just run the script without an argument)
- it has a protection against being used as root, because, in normal, everyday usage, massively changing permissions as root is just asking for trouble
I hope at least someone finds it useful.