Some time ago I copied a bash shell script from a site which I cannot trace. It was written in bash and awk and was not good but superb ! It was about 25 lines long. I had made it into a function and was using it regularly but in a change of distro I lost it . Can anyone help?
Output of ls command in octal
Collapse
X
-
I believe you need output permissions in octal format ?
for example if original output is:
you need it like this:Code:-rw-r--r-- 1 root root 24405 Sep 20 16:56 a.txt
If this is what you wanted then you can use;Code:644 -rw-r--r-- 1 root root 24405 Sep 20 16:56 a.txt
Code:ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'Comment
-
Output of ls command in octal
Similar but different.Code:function lso { ls -l | awk '{ k = 0 s = 0 for( i = 0; i <= 8; i++ ) { k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) ) } j = 4 for( i = 4; i <= 10; i += 3 ) { s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j ) j/=2 } if ( k ) { printf( "%0o%0o ", s, k ) } print }' } function lsa { ls -al | awk '{ k = 0 s = 0 for( i = 0; i <= 8; i++ ) { k += ( ( substr( $1, i+2, 1 ) ~ /[rwxst]/ ) * 2 ^( 8 - i ) ) } j = 4 for( i = 4; i <= 10; i += 3 ) { s += ( ( substr( $1, i, 1 ) ~ /[stST]/ ) * j ) j/=2 } if ( k ) { printf( "%0o%0o ", s, k ) } print }' }
ashitpro,IRYHS !Comment
Comment