How to get octal file permissions on Linux/Unix command line nixCraft Updated Tutorials/Posts

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.
How to get octal file permissions on Linux/Unix command line
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: -

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
%aaccess rights in octal (note ‘#’ and ‘0’ printf flags)
%Aaccess rights in human readable form
%bnumber of blocks allocated (see %B)
%Bthe size in bytes of each block reported by %b
%CSELinux security context string
%ddevice number in decimal
%Ddevice number in hex
%fraw mode in hex
%Ffile type
%ggroup ID of owner
%Ggroup name of owner
%hnumber of hard links
%iinode number
%mmount point
%nfile name
%Nquoted file name with dereference if symbolic link
%ooptimal I/O transfer size hint
%stotal size, in bytes
%tmajor device type in hex, for character/block device special files
%Tminor device type in hex, for character/block device special files
%uuser ID of owner
%Uuser name of owner
%wtime of file birth, human-readable; – if unknown
%Wtime of file birth, seconds since Epoch; 0 if unknown
%xtime of last access, human-readable
%Xtime of last access, seconds since Epoch
%ytime of last data modification, human-readable
%Ytime of last data modification, seconds since Epoch
%ztime of last status change, human-readable
%Ztime 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

Get up to 50% off on Linux Foundation certified SysAdmin, Kubernetes/Containers and Open Stack certification bundle. Offer expires on August 3, 2018
training.linuxfoundation.org

Posted by: Vivek Gite

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.

Facebook Comments
Spread the love

Posted by News Monkey