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?

 

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, which 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 from Linux command line

The syntax is:

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 see octal file permissions

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

See stat command man page for more info:

$ man stat

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *