FreeBSD find the chmod numerical value for a file or directory

I need to find out the numerical value of a file on a FreeBSD system. For example, get 644 instead of -rw-r--r-- for the /etc/resolv.conf file. Is there is a way to get this value in my bash shell script?

You need to use the stat command to see information about the file.


The basic syntax is:
stat file
stat [options] file


To see info about /etc/resolv.conf, enter:
$ stat /etc/resolv.conf
Sample outputs:

1314290432 258051 -rw-r--r-- 1 root wheel 4294967295 57 "Jan 24 01:02:44 2016" "Oct 12 16:30:41 2015" "Oct 12 16:30:41 2015" "Oct 12 16:30:41 2015" 4096 1 0x800 /etc/resolv.conf

To see verbose output (just like Linux) pass the -x option to stat, enter:
$ stat -x /etc/resolv.conf
Sample outputs:

Fig.01: FreeBSD stat command output

Get the chmod numerical value for a file with stat command

Pass the -f format option to display information using the specified format as follows:

stat -f "%OLp" /path/to/file
stat -f "%OLp" /etc/resolv.conf
perm=$(stat -f "%OLp" /etc/resolv.conf)
echo "$perm"

Sample outputs:

Fig.02: Get the value directly using a stat command

  1. -f "Format" – Display info about file using given format.
  2. % – Format strings are similar to printf formats in that they start with %, are then followed by a sequence of formatting characters.
  3. O – Sets the fill character for left padding to the ‘0’ character, instead of a space.
  4. L – Select bits for permissions from the string form of p (last or below option).
  5. p – Display the mode of file (numercial value for file permission as in ls -lTd).

Use raw information to extract numerical value for a file

Run the following command:
$ stat -r /etc/resolv.conf | awk '{ print $3}'
Sample outputs:


You can use as follows to get last four digits:

x=$(stat -r /etc/resolv.conf  | awk '{ print $3}')
cut -c 4-8 <<<"$x"

Sample outputs:


A note about Linux user

Try the following command on Linux for the same purpose:

## Linux stat syntax ##
stat -c '%a' /etc/resolv.conf 
## Or use find command, may work on unix-like system. See find man page. ##
find /etc/ -name resolv.conf -printf "%m\n"

Sample outputs:


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
4 comments… add one
  • max Jan 24, 2016 @ 0:56

    is this also possible for gnu/linux debian?
    thank you. any help and insight is appreciated.

    • 🐧 Vivek Gite Jan 24, 2016 @ 9:44

      Yes. See stat command:
      stat -c '%a' /etc/resolv.conf
      find /etc/ -name resolv.conf -printf "%m\n"

  • Md. Minhazul Haque Jan 24, 2016 @ 5:44

    For Ubuntu,

    $ stat -c '%a' /etc/resolv.conf

    $ stat -c '%A' /etc/resolv.conf

    Thanks. :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum