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
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'
In this example, the “System Preferences” application has a octal permissions value of 775.
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!