Regex for Linux file permissions (numeric notation)
bash, grep, linux, regex
Solution
The simplest and most obvious regexp which is going to work for you is:
grep -q '(0|1|2|3|4|5|7)(0|1|2|3|4|5|7)(0|1|2|3|4|5|7)'
Here is an optimized version:
grep -Eq '(0|1|2|3|4|5|7){3}'
since 6 can represent permissions as well, we could optimize it further:
grep -Eq '[0-7]{3}'
Problem
I can't for the life of me figure out the proper regex for this. What I'm looking for is a regex to match a valid numeric representation of Linux file permissions (e.g. 740 for all-read-none, 777 for all-all-all). So far I've tried the following: ``` strtotest=740 echo "$strtotest" | grep -q "[(0|1|2|3|4|5|7){3}]" if [ $? -eq 0 ]; then echo "found it" fi ``` The problem with the above is that the regex matches anything with `1-5` or `7` in it, regardless of any other characters. For example, if `strtotest` were to be changed to `709`, the conditional would be true. I've also tried `[0|1|2|3|4|5|7{3}]` and `[(0|1|2|3|4|5|7{3})]` but those don't work as well. Is the regex I'm using wrong, or am I missing something that has to deal with `grep`?