Retrieving File Permissions in Octal Mode Using the Command Line

File permissions on a Linux system can be represented in either symbolic mode or octal mode. Using octal mode to represent file permissions is a little more succinct, since we can usually list all relevant file permissions with just three numbers. These numbers represent the owner, group, and other user permissions for any file or directory on Linux. In this tutorial, you will see how to get a listing of file permissions in octal mode representation on the Linux command line.

In this tutorial you will learn:

  • How to retrieve octal representation for file permissions using stat command
  • How are octal permissions translated into symbolic mode and vice versa
  • Advantages and differences of using octal mode vs. symbolic mode for file permissions
Retrieving File Permissions in Octal Mode Using the Command Line
Retrieving File Permissions in Octal Mode Using the Command Line
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ls, stat, chmod
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to List File Permissions in Octal Mode via Command Line




The stat command is the best tool for checking file permissions in octal mode from the Linux command line. The stat command is installed on all Linux systems by default, so you will undoubtedly already have access to it. You may already be familiar with the stat command as a very simple way to check access and modification time for file on Linux. Check the example commands below to see how to use stat to check octal permissions for files and directories in Linux:

  1. Using the stat command, followed by the path to a file you wish to check the permissions for, will reveal its permissions in octal mode.
    $ stat my_file.txt
    ...
    Access: (0750/-rwxr-x---)
    

    Here, we can see that our octal permissions for my_file.txt are 0750. We can also see the symbolic permissions, which are -rwxr-x---.

  2. The same command syntax can also be used to check the octal permissions for a directory:
    $ stat my_directory
    ...
    Access: (0775/drwxrwxr-x)
    

    Here, we can see that our octal permissions for my_directory are 0775. We can also see the symbolic permissions, which are drwxrwxr-x.

  3. In case you want more succinct output from the stat command, to isolate just the relevant octal and symbolic permissions for the file, we can utilize the -c option in our command along with operators '%A %a %n'. Here is how the command would look (once again, specify your file name):
    $ stat -c '%A %a %n' my_file.txt
    -rwxr-x--- 750 my_file.txt
    
    Isolating the symbolic and octal permissions in the stat command output on Linux
    Isolating the symbolic and octal permissions in the stat command output on Linux
  4. If you only want to see the octal permissions for the file, and nothing else at all in your stat output, then we can use the -c option along with the '%A' operator by itself. This will avoid listing the symbolic permissions and file name in the output, like we had in the previous example:
    $ stat -c '%A' my_file.txt 
    -rwxr-x---
    
  5. To get a list of octal mode permissions for all files in a directory, you can supply an asterisk in the command. It is recommended to also supply the %n operator in your stat command, so you will know which file each permissions refer to. The %a operator is for symbolic mode and could be omitted in this case:
    $ stat -c '%A %a %n' ~/*
    drwxr-xr-x 755 Desktop
    drwxr-xr-x 755 Documents
    drwxr-xr-x 755 Downloads
    -rw-rw-r-- 664 linuxconfig-background.png
    drwxr-xr-x 755 Music
    drwxrwxr-x 775 my_directory
    -rwxr-x--- 750 my_file.txt
    
DID YOU KNOW?
There are two ways to represent file permissions on Linux. One is symbolic mode, which represents file permissions with r (read), w (write), and x (execute). The other is octal mode (sometimes referred to as numeric mode or absolute mode), which represents each set of permissions with some number 0-7.

Using Octal Mode vs Symbolic Mode for File Permissions




Most Linux users default to the ls command for listing file attributes, including permissions. Ordinarily, all of our file listing needs is contained in this command, and users do not give much more thought to other commands.

However, the problem with using the ls command is that these permissions are always listed in symbolic mode, as shown in the example output below:

$ ls -l my_file.txt 
-rw-rw-r-- 1 linuxconfig linuxconfig 0 Oct 1 22:40 my_file.txt

As shown above, the permissions are listed as rw-rw-r–. While this format may be suitable in many scenarios, octal mode is more succinct in its representation. These same permissions would translate to 664 in octal mode. Here is a breakdown of how the symbolic permissions translate into octal mode:

  • owner permissions rw- is equivalent to 6
  • group permissions rw- is equivalent to 6
  • other user permissions r-- is equivalent to 4
  • the full permission read out of rw-rw-r-- is equivalent to 664

With octal mode, every file has permissions represented with numbers 0-7. Three numbers grouped together can fully represent the permissions for any file or directory in Linux. Furthermore, Linux commands such as chmod can use octal mode to assign permissions to files. For example, to assign 750 permissions (or rwxr-x--- in symbolic mode), we could use the following command:

$ chmod 750 my_file.txt

Then, let’s verify the newly assigned file permissions with the stat command:

$ stat my_file.txt
The stat command shows us the newly assigned permissions for our file
The stat command shows us the newly assigned permissions for our file

Part of the output returned when running this command reveals the file permissions in both symbolic and octal mode:

Access: (0750/-rwxr-x---)

Of course, the octal permissions are 750 and the symbolic permissions are represented as rwxr-x---. Here is a breakdown of how the two modes translate to each other for these particular permissions:

  • rwx is equivalent to 7
  • r-x is equivalent to 5
  • --- is equivalent to 0

Closing Thoughts




In this tutorial, we saw how to retrieve file permissions represented in octal mode via command line on a Linux system. We also learned about how octal permissions are translated into symbolic mode and vice versa. The main advantage of using octal mode (also known as absolute mode) is that permissions can be listed very succinctly. Both octal and symbolic have their pros and cons, and Linux administrators should know how to wield both of them.