1

I'm trying to script a check to make sure two files have the same permissions. There are several ways to think about permissions, the most basic being a hex value (e.g. 777). How can I compare the permissions on two different regular files in bash?

touch a b
chmod 777 a
# What can I use to get the `777` value property from `a`?
if a.perms == b.perms; then
  echo "File permissions match!"
fi

1 Answer 1

1

stat, with the right options, seems to provide a way to compare this property, but not to an arbitrary permissions argument (e.g. 777 or +x). For that you'll want to choose the appropriate representation (e.g. %A or %a). See more in the man page.

$ echo "$(stat -c '%a' a)"
777
perms_a="$(stat -c '%a' a)"
perms_b="$(stat -c '%a' b)"
if [ ${perms_a} = ${perms_b} ]; then
  echo "Permissions match!"
else
  echo "NO match."
fi
1
  • 1
    Replace == with = if you want to be POSIX compliant.
    – fpmurphy
    May 16, 2020 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .