Programming Tips - Bash: check file permissions

Date: 2023apr6 Language: bash Q. Bash: check file permissions A. If you want to check permissikons as the current user:
if [[ -r $FILE ]]; then echo "File $FILE is readable by me" fi
To check if its world readable, use `stat` and some builtin bitwise ops.
#!/bin/sh FILE=/tmp/myfile.txt PERMS=$(stat -c '0%#a' $FILE) # $PERMS will be a 4 digit octal number WORLD_READABLE=$(($PERMS & 0007)) if [[ $WORLD_READABLE != 0 ]]; then echo "$FILE is world readable" else echo "$FILE is not world readable" fi