Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 down vote accepted
MYFILE=./myfile

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

 
discard

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.