Translate rwx Permissions into Octal Format in Linux
Sometimes you may find it useful to display the access rights of files or directories in octal form instead of
rwx or perhaps you want to display both.
Instead of using good old
ls -l command, in most modern Linux distributions (if not all) you will find
stat, an utility that displays file or filesystem status.
When run without arguments but followed by a given filename,
stat will display a good deal of information about the file or directory. If used with the
-c option, stat allows you to specify an output format. It is precisely this option that’s of particular interest to us.
To display all files in the current working directory followed by the access rights in octal form, type:
# stat -c '%n %a' *
add_emails.sh 755 anaconda-ks.cfg 600 delete_emails.sh 755 employee-dump.sql 644 index.html 644 latest.tar.gz 644 nrpe-2.15.tar.gz 644 php7 644 playbook.retry 644
In the command above, the format sequence:
%n– means file name
%a– means access rights in octal form
Alternatively, you can append
%A, the argument passed to stat if you want to display the permissions in
rwx format as well.
In that case, you can type:
# stat -c '%n %A' *
add_emails.sh -rwxr-xr-x anaconda-ks.cfg -rw------- delete_emails.sh -rwxr-xr-x employee-dump.sql -rw-r--r-- index.html -rw-r--r-- latest.tar.gz -rw-r--r-- nrpe-2.15.tar.gz -rw-r--r-- php7 -rw-r--r-- playbook.retry -rw-r--r--
To view the file type in the output, you can add
%F format sequence.
# stat -c '%c %F %a'
There are several other format sequences you can specify, refer to the
stat man page to find out more.
# man stat
In this tip, we have covered an important Linux utility called stat, that helps you to display a file or file system status. Our main focus here was to translate the
rwx access rights from the traditional
ls -l output to octal form.
As I had mentioned earlier on, many modern Linux distributions now come with stat utility. But you must also remember that your shell may come with its own version of stat, therefore refer to your shell’s documentation for more information concerning options and how to use them.