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.

1 Answer 1

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" ';'

You must log in to answer this question.

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