Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Any know a standard way, other than 'stat' to produce octal output for file permission from 'ls'? Standard way of using stat (on OSX) is:

stat -f '%A %a %N' *

where * can be any file pattern that you'd normally use with ls.

I'm thinking of creating an ls based alias to use for shorthand.

Is there another/better way to do this?

share|improve this question

Do not create a "ls-based" alias. ls just does not do what you are asking for. This is the job of stat.

If you don't like the stat command, you can call the stat() function directly:

#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
    int i;
    struct stat st;
    for (i = 1; i < argc; i++) {
        if (stat(argv[i], &st) < 0)
            perror("stat");
        else
            printf("%06o %s\n", st.st_mode, argv[i]);
    }
    return 0;
}
#!/usr/bin/env perl
printf("%06o %s\n", (stat($_))[2], $_) for @ARGV
#!/usr/bin/env ruby
ARGV.each do |f|
    printf "%06o %s\n", File::stat(f).mode, f
end
#!/usr/bin/env python
import os, sys
for f in sys.argv[1:]:
    sys.stdout.write("%06o %s\n" % (os.stat(f).st_mode, f))
#!/usr/bin/env php
<?php
for ($i = 1; $i < $argc; $i++)
    printf("%06o %s\n", fileperms($argv[$i]), $argv[$i]);
#!/usr/bin/env tclsh
foreach file $argv {
    file stat $file st
    puts [format "%06o %s" $st(mode) $file]
}
share|improve this answer
    
Well, that's pretty thorough. I guess I was hoping to find an easy way to reproduce the output of ls -la with only 1 difference: print permissions in octal instead of the rwxr--r-- format. – ashr Nov 22 '11 at 5:13
    
@ashr: Then that's what you should have written in your post. – grawity Nov 22 '11 at 7:51

Use stat instead or together with ls. You can change stat output format if you want to look like ls output. For example:

find directory -maxdepth 1 -print0 | xargs -0 stat --format='%a %n'

If you would like to see it exactly as ls output then it should be some script already (awk, perl, etc). It can be defined as a function in your shell or saved as a script in some directory in your PATH and you could use this as a ls command replacement. It depends on your needs how similar to the ls it will be (command line option processing etc)

share|improve this answer

You must log in to answer this question.

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