Skip to content

Check a Named Path Has Given Mode Bits

Return success if the named path exists and its mode bits match the expected octal permissions, using GNU or BSD stat.

Language: Shell

Added on: 19th August 2026

check_permissions() {
  local file_name=$1
  local expected_perms=$2
  local actual_perms

  [[ -e "${!file_name-}" ]] || return 1

  actual_perms=$(stat -c "%a" "${!file_name}" 2>/dev/null || stat -f "%OLp" "${!file_name}" 2>/dev/null)
  [[ "$actual_perms" == "$expected_perms" ]]
}

Caveats

Pass the variable name that stores the path, then the expected mode as octal (for example 644).