Blog

03/06/2019

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.

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: -

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:

FormatDescription
%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

(adsbygoogle = window.adsbygoogle || []).push({});

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....