How to Get Octal File Permissions from Command Line in Mac OS

Sep 6, 2018 - 4 Comments

Getting file permissions in octal format on Mac

Command line users are likely familiar with using chmod to set file permissions in numerical or octal format, for example running a command like ‘chmod 755 filename’, but have you ever wondered how you can get file permissions in octal format?

If you want to see or view the octal numerical value of permissions of any file or folder via the command line, you can turn to the stat command in Mac OS to do so.

We’re assuming you have a reasonable level of experience and comfort at the command line, if you don’t then this article likely isn’t relevant to you. Most Mac users will only ever view or change file permissions through the Mac Finder as described elsewhere (if even that), whereas this particular article is aimed at more advanced users.

How to Get Numerical chmod Permissions Values on the Mac

To get started, launch the Terminal app from /Applications/ on the Mac and use the following commands:

stat -f %A file.txt

For example, that command may output something like the following:

$ stat -f %A wget-1.18.tar.gz
644

Where, in this example, ‘644’ is the octal value of that files permissions.

Alternatively, you can use -f and %OLp (yes that’s an upper case ‘o’ and not a zero), the output will be the same assuming the file is too:

stat -f %OLp /Applications/System\ Preferences.app

Example output for that command may look like the following, showing the numerical octal value permissions for the target item:

$ stat -f "%OLp" '/Applications/System Preferences.app'
775

In this example, the “System Preferences” application has a octal permissions value of 775.

How to get file permissions as octal value on Mac from command line

You should not need to use quotations, though if you need for some reason to escape a file name or path, or for scripting purposes, they’re easy to place like so:

stat -f "%OLp" '/Applications/System Preferences.app'

The -f flag is for format, you can read more about specific formatting options for the stat output from the manual page on stat with ‘man stat’.

In the latter command case, the “O” (upper case o) is specifically for achieving octal output.

Knowing the exact numerical permissions of a file or folder is wildly useful for so many reasons, and it can be helpful to know this if you’re adjusting the permissions of various items, or even if you’re moving files on the Mac and want to maintain the exact permissions and to verify it after the fact. There are countless other uses as well, particularly if you’re running a server of any sort from the Mac.

These commands should work the same for retrieving octal permissions in just about any version of macOS, MacOS, or Mac OS X, regardless of how the naming convention is capitalized. Notably however, is that the approach to getting octal permissions on the Mac is different from the rest of the Linux world, thus if you’re coming to the Mac from the Linux world you’ll need to adjust the stat command flags to accurately get the permissions in octal format, we’ll cover that quickly next.

Getting Octal File Permissions from Command Line in Linux

For the sake of being thorough, we’ll briefly discuss getting octal permissions values in the Linux world as well, where you can use the following to get the octal file permissions:

stat -c "%a %n" /Path/To/File

You can also more simply use the stat -c command:

stat -c %a /Path/To/File.txt

The numerical value output will be the same regardless, as long as the inputted target file is the same of course.

Again, these latter two approaches are linux specific, and you’ll need to use the methods outlined further above to get octal values of permissions of a file in Mac OS.

Do you know of any other methods or approaches to retrieving the numerical value of file permissions on a Mac? Share them in the comments below!

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

4 Comments

» Comments RSS Feed

  1. Awkward says:

    Ive enjoyed dabbling w some awk tutorials and found very handy for parsing csv files and ls output and such, that one can easily derive a line of awk syntax to extract whatever column(s) of data youd like and in whatever order needed.

    Never heard of stat command so a nice gordion chop for getting permissions. Nice article thanks! What else can one use stat for?

  2. tired says:

    I’ve been a command line user for decades, was a Mac user then ran Linux as a desktop for a long time, then back to the Mac to enjoy Terminal and less of the configuration hassles of Linux.

    Even with that background, I have never quite figured out the octal permissions. I have memorized a few of the critical ones (644, 755, 775, 740, etc) but if you told me to count the permissions manually in numeric notation I wouldn’t be able to tell you how to do it. I wonder how many linux and Mac power users could? I think most of us just search the web for permissions rather than add them all up. I mean just look at this, and put in the context of all else we do and memorize try and think of an intuitive way to memorize octal notation.

    https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation

    Anyway, I find it very useful to be able to view and retrieve octal notation and I was not aware of this, so it’s very helpful. I don’t quite understand why the command itself is the same but the flags are different on Mac and Linux but I assume the Mac version of stat is older or from a different branch.

    Very useful so thank you.

    Also as Sam said, the simple “ls -l file” command will reveal permissions in the read/write/execute format

    • Doc Oct says:

      Its simple really. (Self)User, Group, world. For most group is irrelevant so often mimic setting for world. Read, Write, Execute, in a bit mask form, ie

      Read 100 binary is 4 octal
      Write 010 binary is 2 octal
      Execute is 001 binary or 1 octal

      So read write is 110 or 6 (typically documents)
      Read execute is 101 or 5 (typically read only folders)
      All rwx is 111 or 7, and obviously 0 means no permissions

      Just use that for each, user, group, world and thats the 3 octal digits you see.

      Octal is just 3 digit shorthand for binary. Hex is 4 digit shorthand.

  3. Sam says:

    I simply use `ls -l file.txt`. this give you file permissions, though not in ocatl format, ownership info and other data as well.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site