How to get octal file permissions on Linux/Unix command line

I am a new Linux command line user. How do I find file permissions in octal format such as 0644 from the bash command line running on a Linux or Unix operating systems? How can I get the octal permissions for a file? How do I get octal file permissions on Linux/Unix?

You need to use the stat command to view or get octal file permissions for given filename. By default the ls command will not display the permissions on a file in octal form. The permission in octal form is useful for many commands such as chmod command and other sysadmin tasks.

This quick tutorial shows how to use the stat command to view octal file permissions.

How to get octal file permissions on Linux/Unix command line

To get file or file system status try the stat command. However, the syntax for stat is different on GNU/Linux and BSD/macOS Unix like systems.

How to get octal file permissions from Linux command line

The syntax is as follows to get octal file permissions on Linux:
stat fileName
stat -c 'Format' file

The default output is as follows on a GNU/Linux:
$ stat /etc/passwd
Sample outputs:

  File: /etc/passwd
  Size: 2605      	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 15469225    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-11-05 19:06:44.879876898 +0530
Modify: 2017-10-16 00:15:53.236876717 +0530
Change: 2017-10-16 00:15:53.236876717 +0530
 Birth: -

To just see octal file permissions on a GNU/Linux:
$ stat -c '%a' /etc/passwd
Sample outputs:

644

Another useful command that displays file permissions in both format:
$ stat -c '%A %a %n' /etc/passwd
Sample outputs:

-rw-r--r-- 644 /etc/passwd

Where format is as follows:

Format Description
%a access rights in octal (note ‘#’ and ‘0’ printf flags)
%A access rights in human readable form
%b number of blocks allocated (see %B)
%B the size in bytes of each block reported by %b
%C SELinux security context string
%d device number in decimal
%D device number in hex
%f raw mode in hex
%F file type
%g group ID of owner
%G group name of owner
%h number of hard links
%i inode number
%m mount point
%n file name
%N quoted file name with dereference if symbolic link
%o optimal I/O transfer size hint
%s total size, in bytes
%t major device type in hex, for character/block device special files
%T minor device type in hex, for character/block device special files
%u user ID of owner
%U user name of owner
%w time of file birth, human-readable; – if unknown
%W time of file birth, seconds since Epoch; 0 if unknown
%x time of last access, human-readable
%X time of last access, seconds since Epoch
%y time of last data modification, human-readable
%Y time of last data modification, seconds since Epoch
%z time of last status change, human-readable
%Z time of last status change, seconds since Epoch

A note about macOS/BSD stat command

The syntax is as follows for BSD stat command to get octal file permissions on BSD or macOS:
stat fileName
stat -f "Format" fileName

The default output is as follows:
$ stat /etc/passwd
Sample outputs:

1518287693 66065 -rw-r--r-- 1 root wheel 4294967295 1724 "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" 4096 9 0x800 /etc/passwd

To just see octal file permissions
$ stat -f "%OLp" /etc/passwd
Sample outputs:

644

To displays file permissions in both format:
$ stat -f "%Sp %OLp %N" /etc/passwd
Sample outputs:

-rw-r--r-- 644 /etc/passwd

Conclusion

You just learned how to use the stat command to get octal file permissions from command line. See stat command man page by typing the man command :
$ man stat


🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz