UNIX / Linux: File Permissions in Octal

To set file permissions using the chmod command, we can specify the permissions in either numeric mode or symbolic mode. But when we want to get the file permissions, most often with the ls -l command, we are normally only given a printout using the symbolic mode.

There are a couple of ways to print permissions in octal mode. The first method is by using the stat command.

The above will printout the octal permissions of a file. Noting more, nothing less. But if we try to use the command and pass a wildcard argument, the information would be meaningless.

You will not know which permissions are for which file. To include the filenames alongside with the permissions, make a slight modification to the stat command.

Now we know that file foo has a permission of 755 and file foo.c has a permission of 644.

The octal permissions of the stat command also takes care of setuid, setgid and sticky bits. We can test this by printing the permissions of su – a standard setuid command.

I have written a small shell script that will make use of the ls and awk commands to print the octal permissions.

Copy the script to a file called lso (ls octal), make it executable and run it like this:

Alternatively, if you are not interested in the shell script, you can opt to copy the below function to your shell initialisation file ( eg. .bashrc or .bash_profile) instead.

Run the function by calling the function name from the command line.

Update (11-Jan-2017): Script updated to handle setuid, setgidand sticky bits.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, php, python, java, javascript, nodejs, react); plays_on(xbox, ps4); linux_desktop_user(true); }