Unix permissions in symbolic notation (including sticky bit)

I need to get a Access rights in larger format human readablefor a file or folder with a symbolic type designation u=rwx,g=srwx,o-rwx(possibly sticky bits)

  • Using stat --format '%a', I get the result with format 2770, octal format
  • Using stat --format '%a', I get the result with a drwxrws---human readable format

I need a command to get the type format u=rwx,g=srwx,o-rwx(compatible with chmod symbolic modes)

  • [u|g|o]: for user/ group/ otheror afor all
  • [=]: for granted rights
  • [rwxst]: list of rights not relevant to the order
  • [-rwx]: to revoke the right (if the right is not granted)

I tried this (but it does not handle all cases, special sticky bits):

stat --format '%A' temp |
    sed -E 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' | # split by triplet
    sed 's/=---/-rwx/g' | # revoker grants
    sed 's/rws/srwx/g'  | # setgid with x ...
    sed 's/--S/s/g'     | # setgid without x ...
    sed ... nead more transormation... # manage  sticky bit setuid setgid

.

== >

  • drwxrws--- == > u=rwx,g=srwx,o-rwx ( d == > )
  • drwxrwxrwx == > u=rwx,g=rwx,o=rwx ugo=rwx a=rwx
  • -r-xrw-r-- == > u=rx,g=rw,o=r ( - == > )
  • -rwx--S--- == > u=rwx,g=s,o-rwx (S )
  • ------s--t == > u=-srwx,g=sx,o=xt (Stickybit)

== > stat ls -al
== > chmod

, , (.. sed)

stat --format '%A' a |
sed -E 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' | # split by triplet    
sed -E 's/s/xs/g'          | # setgid ou setuid with x ...
sed -E 's/t/xt/g'          | # sticky bit with x ...    
sed -E 's/S/s/g'           | # setgid ou setuid without x ...
sed -E 's/T/t/g'           | # sticky bit alone
sed -E 's/-//g'            | # remove -
sed -E 's/=(,|$)/-rwx\1/g'   # revoker grants
+4
2

sed -e 'sed-command', , sed:

stat --format '%A' a |
sed -E -e 's/^.(...)(...)(...)/u=\1,g=\2,o=\3/g' \
       -e 's/s/xs/g' \
       -e 's/t/xt/g' \    
       -e 's/S/s/g' \
       -e 's/T/t/g' \
       -e 's/-//g' \
       -e 's/=(,|$)/-rwx\1/g'

-, , . s/s/xs/g , , ( sx, xs SGID ). , -e , . -e . , -f script.sed script . , script , , .

. , , , , , Unix.

+2

, , , :

stat --format '%A' myFile | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g'

, bash, (8 * 8 * 8 * 8 = 4096 !)

test_file=/tmp/a

return_code=0

echo -e "from itertools import product;\nfor mod in  product('01234567', repeat=4) : print ''.join(mod)" | python | shuf |
while read octalMod
do
  chmod $octalMod $test_file  # apply octal mod

  # get symbolic mod
  symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g')

  chmod 0000 $test_file          # reset mod
  chmod $symbolicMod $test_file  # apply symbolic mod

  modActual=$(stat --format '%a' $test_file) # get actual mod in octal format to compare it with the original
  if [ $octalMod -ne $modActual ]
  then
    echo "$octalMod ($symbolicMod) !!! octalMod=$octalMod <> modActual=$modActual" >&2
    return_code=255
  else
    echo "$octalMod ($symbolicMod)     octalMod=$octalMod == modActual=$modActual" >&1
  fi

done
exit $return_code

:

test_file=/tmp/a

echo -e "octalMod\thumanMod\tsymbolicMod"

echo -e "from itertools import product;\nfor mod in  product('01234567', repeat=4) : print ''.join(mod)" | python |
while read octalMod
do
  chmod $octalMod $test_file  # apply octal mod

  # get symbolic mod
  symbolicMod=$(stat --format '%A' $test_file | sed -E -e 's/(...)(...)(...)$/u=\1,g=\2,o=\3/;s/(s|t)/x\1/g;s/(S|T)/\l\1/g;s/-//g;s/(u|g)=,/\1-rwxs,/g;s/o=$/o-rwxt/g')
  humanMod=$(stat --format '%A' $test_file)

  echo -e "$octalMod\t$humanMod\t$symbolicMod"

done

echo -e "octalMod\thumanMod\tsymbolicMod"

:

octalMod        humanMod        symbolicMod
0001    ---------x      u-rwxs,g-rwxs,o=x
0354    --wxr-xr--      u=wx,g=rx,o=r
1210    --w---x--T      u=w,g=x,o=t
3337    --wx-wsrwt      u=wx,g=wxs,o=rwxt
3566    -r-xrwSrwT      u=rx,g=rws,o=rwt
3734    -rwx-wsr-T      u=rwx,g=wxs,o=rt
6734    -rws-wsr--      u=rwxs,g=wxs,o=r
7121    ---s-wS--t      u=xs,g=ws,o=xt
7430    -r-S-ws--T      u=rs,g=wxs,o=t
7611    -rwS--s--t      u=rws,g=xs,o=xt
+2

Source: https://habr.com/ru/post/1687871/


All Articles