Search code examples
javalinuxunixstat

How to know the Octal Permission of a file


In my Java application , I am running following command to get the Octal permission and file name.

stat -c "%a %n" <file name>

This command works on Linux but the Solaris version I have doesnt have stat command supported.

How can I achieve this with a command which works on both Linux and Solaris There are some options with perl stat but unfortunately I cant use perl.


Solution

  • You can achieve same thing using find command

    find <file name> -printf "%m %f"
    
    find <directory name> -maxdepth 0 -printf "%m %f"
    

    Example:

    find /etc/passwd -printf "%m %f"
    
    find /etc -maxdepth 0 -printf "%m %f"