Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Check Linux File Octal Permissions Using stat Command

· 5 min read · Home Tech

In this article
  1. What Are Octal Permissions?
  2. Using stat to Display Octal Permissions
  3. Why Use stat Instead of ls?
  4. Understanding the Output Format
  5. Special Permissions: setuid, setgid, and Sticky Bit
  6. Common Mistakes and Troubleshooting
  7. Alternatives to stat
  8. Frequently Asked Questions

To check Linux file octal permissions using the stat command, run stat -c "%a %n" filename to display the permission bits as a three-digit octal number (e.g., 644) alongside the file name. The %a format specifier outputs the permissions in octal, which is the numeric representation used with chmod.

What Are Octal Permissions?

Linux file permissions control who can read, write, and execute a file. They are divided into three groups: the file owner (user), the group that owns the file (group), and everyone else (others). Each group has three permission bits:

  • Read (r) – view file contents or list directory entries.
  • Write (w) – modify file contents or create/delete files in a directory.
  • Execute (x) – run a file as a program or traverse a directory.

Each bit is represented by a binary digit: 4 for read, 2 for write, 1 for execute. The octal number is the sum of these values for each group. For example, 7 = 4+2+1 (read, write, execute), 6 = 4+2 (read, write), 5 = 4+1 (read, execute), and 4 = read only. A typical file has 644 (owner can read/write, group and others can read), while a typical directory has 755 (owner can do everything, others can read and execute).

Using stat to Display Octal Permissions

The stat command shows detailed file metadata. To get the octal permissions, use the -c (or --format) option with the %a specifier:

stat -c "%a %n" file.txt

This prints the octal permissions followed by the file name. If you want the symbolic representation (like -rw-r--r--), use %A instead. You can combine both:

stat -c "%a %A %n" file.txt

Output example: 644 -rw-r--r-- file.txt. The %n is the file name. For a directory, the output might look like 755 drwxr-xr-x dir.

Why Use stat Instead of ls?

The ls -l command shows permissions in symbolic form (e.g., -rw-r--r--), which is human-readable but not directly usable with chmod. The octal form is what chmod accepts, so converting from symbolic to octal manually can be error-prone. stat gives you the exact octal value instantly, which is especially useful in scripts or when you need to copy permissions from one file to another. For example, you might capture the octal permissions of a reference file and apply them to a new file:

perms=$(stat -c "%a" reference.txt)
chmod "$perms" newfile.txt

This is much more reliable than trying to parse ls output.

Understanding the Output Format

The stat command has many format specifiers. The most relevant for permissions are:

  • %a – octal permissions (e.g., 644).
  • %A – symbolic permissions (e.g., -rw-r--r--).
  • %u – user ID (UID) of the owner.
  • %g – group ID (GID) of the group.
  • %U – owner username.
  • %G – group name.

You can combine these in any order. For instance, stat -c "%U:%G %a %n" file shows the owner and group names, octal permissions, and file name. The %n is the file name; use %N for quoted file name (useful for filenames with spaces).

Special Permissions: setuid, setgid, and Sticky Bit

Octal permissions can also include a fourth leading digit for special permission bits:

  • setuid (4) – when set on an executable, the process runs with the file owner's privileges.
  • setgid (2) – on directories, new files inherit the directory's group; on executables, the process runs with the file group's privileges.
  • Sticky bit (1) – on directories, only the file owner (or root) can delete or rename files within it (e.g., /tmp).

For example, a file with permissions 4755 has setuid set. The stat command shows this four-digit number when any special bit is set. If no special bit is set, stat outputs only three digits (e.g., 644). To always see four digits, you can use %a with a leading zero? Actually, stat does not pad to four digits by default. If you need a consistent four-digit output, you can use printf:

printf "%04d\n" $(stat -c "%a" file)

This pads the octal number with leading zeros, so 644 becomes 0644.

Common Mistakes and Troubleshooting

One common mistake is confusing the order of format specifiers. The -c option expects a single argument; if you forget to quote it, the shell may expand wildcards. Always quote the format string: stat -c "%a %n" file.

Another issue: on some systems (e.g., macOS), the -c option is not available; you must use -f with different specifiers. However, Linux distributions (including Ubuntu, Debian, RHEL, Arch) support -c. If you get an error like stat: illegal option -- c, you are likely on BSD/macOS. In that case, use stat -f "%Lp %N" file to get octal permissions.

If you need only the octal value without the file name (for scripting), use stat -c "%a" file. If the file name contains spaces, the output will still be correct because %n prints the name as-is; but if you use %N, it will be quoted.

Alternatives to stat

While stat is the most direct way to get octal permissions, there are alternatives:

  • ls -l – shows symbolic permissions; you can convert manually.
  • find with -printf: find . -maxdepth 1 -printf "%m %f\n" prints octal permissions and file name.
  • namei -l – shows permissions for each path component.
  • getfacl – shows ACL entries, which may include more granular permissions than standard octal.

For most tasks, stat is the simplest and most portable across Linux distributions.

Frequently Asked Questions

How do I check octal permissions for multiple files at once?

Use stat -c "%a %n" * in the current directory, or specify a list of files: stat -c "%a %n" file1 file2. You can also use find with -exec stat for recursive checks.

What does 644 mean in octal permissions?

644 means the owner has read and write (6 = 4+2), while group and others have read only (4). This is the default for most regular files on Linux systems.

Why does stat show 755 for a directory but 644 for a file?

Directories need execute permission for users to access their contents, so the execute bit is typically set for all groups. A common default for directories is 755, while files are 644 because they don't need execute permission.

Can I use stat to change permissions?

No, stat only displays information. To change permissions, use chmod. You can combine them in a script: chmod $(stat -c "%a" file1) file2.

How do I see the octal permissions including special bits?

Use stat -c "%a" file. If the output has three digits, no special bits are set. If it has four digits, the first digit indicates setuid, setgid, or sticky bit (e.g., 4755).

What is the difference between %a and %A in stat?

%a gives the octal number (e.g., 644), while %A gives the symbolic string (e.g., -rw-r--r--). Both are useful; use %a for scripts and %A for human-readable output.