3

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.

4
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" ';'
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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