Output of ls command in octal

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pwilliams
    New Member
    • Jul 2010
    • 21

    Output of ls command in octal

    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?
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    @pwilliams,

    Can you give us an example of what the script did?

    Your description isn't very clear. We know something about processing ls output into octal. However a concrete request might help.

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      I believe you need output permissions in octal format ?

      for example if original output is:
      Code:
      -rw-r--r-- 1 root root    24405 Sep 20 16:56 a.txt
      you need it like this:
      Code:
      644 -rw-r--r-- 1 root root    24405 Sep 20 16:56 a.txt
      If this is what you wanted then you can use;
      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

      • pwilliams
        New Member
        • Jul 2010
        • 21

        #4
        Output of ls command in octal

        Oralloy ,ashitpro many thanks for your replies.
        With a red face I must confess,the file came to light in a totally wrong directory , on a usb stick I had forgotten about.Would anybody be interested in seeing it?

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          send the function you are talking about.

          Comment

          • pwilliams
            New Member
            • Jul 2010
            • 21

            #6
            Output of ls command in octal

            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
            }'
            }
            Similar but different.
            ashitpro,IRYHS !

            Comment

            Working...