All files & directories in Linux have user permissions represented by rwx (r-read, w-write, x-execute). By default, when you use commands like ls, it displays file permissions in rwx format. But sometimes you may want to display file permissions in octal format. For this purpose, you can use stat command. stat displays quite a lot of useful information about each file & directory, when used without any options. However, when used with -c option, it allows you to specify the output format. In this article, we will learn how to convert permissions to octal in Linux.
How to Convert Permissions to Octal in Linux
Here is an example of how rwx file permissions translate to octal permissions.
Here is a command to view permissions of all files & directories using stat command, in octal format.
# stat -c '%n %a' *
Here is a sample output.
add_emails.sh 755 delete_emails.sh 765 employee-dump.sql 744 index.html 644
In the above command, %n means file name and %a means octal format. On the other hand, you can use %A instead of %a if you want to view results in rwx format.
# stat -c '%n %A' *
Here is the sample output.
add_emails.sh -rwxr-xr-x delete_emails.sh -rwxrrx--x employee-dump.sql -rw-r--r-- index.html -rw-r--r--
You can also view file type using %F format specifier as shown below.
# stat -c '%c %F %a'
In the above command’s output, you will see file type after filename and before file permissions.
In this article, we have learnt how to display file permissions in octal format instead of rwx format.
Also read:
How to Compare Local & Remote Files in Linux
How to Generate Pre Shared Key in Linux
How to Run Command After Certain Time in Linux
Timeout Command in Linux
How to Set SSH Warning Message in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.