Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script where I'd like to test a file for specific permissions, specifically 0644, if it is not that then chmod it.

I know I could just chmod it and let it fall through in case it already was that way but I'd like to print a message of it's previous value.

share|improve this question
up vote 4 down vote accepted

For readability I'm using u=rw,go=r instead of octal 0644. I'm assuming that you wish to set the missing permissions , but if there are extra permissions present, you want to let them be as they are. Step by step:

PREV_PERM="`find \"\$MYFILE\" \! -perm -u=rw,go=r -ls`"
echo "$PREV_PERM"
chmod u+rw,go+r "$MYFILE"

or combine it into neat one-liner instead:

find "$MYFILE" '!' -perm -u=rw,go=r -ls -exec chmod u+rw,go+r "$MYFILE" ';'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.