Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux File Permissions as Numbers: A Practical Guide

To show Linux file permissions as a number, use stat -c '%a %n' filename; for example, it may print 644 report.txt. The number is an octal representation of the read, write, and execute permissions for the file’s owner, group, and others.

Quick command to display numeric permissions

On Linux, the GNU stat command is a direct way to print a file’s permission mode in octal alongside its name:

  • stat -c '%a %n' report.txt shows the mode and filename.
  • stat -c '%a' report.txt prints only the numeric mode.
  • stat -c '%a %n' . checks the current directory itself.

The format code %a requests the permission mode in octal, and %n requests the filename. The output for a typical private file might be 600 secrets.txt; a commonly readable file might show 644 report.txt. The command reports the permissions currently recorded for the path, rather than recommending what permissions it ought to have.

To display modes for many files, pass multiple paths, or use find when you need to search a directory tree. For example, find . -type f -exec stat -c '%a %n' {} + prints the numeric modes of regular files under the current directory. Add -type d instead to select directories. If a filename begins with a hyphen, use -- before the filename in a direct stat command so it is not mistaken for an option.

What the permission number means

In a three-digit mode such as 754, each digit represents one class of users, in this order: owner, group, and others. Each digit is the sum of the permissions enabled for that class. The values are 4 for read, 2 for write, and 1 for execute. Add the values together to get the digit:

  • 7 means read, write, and execute: 4 + 2 + 1.
  • 6 means read and write: 4 + 2.
  • 5 means read and execute: 4 + 1.
  • 4 means read only.
  • 0 means none of these permissions.

So 754 gives the owner all three permissions, the group read and execute, and other users read only. The digits are octal because each is a value from zero through seven, representing three permission bits. A leading zero is sometimes used to make the octal notation explicit, so 0754 and 754 describe the same ordinary permission mode in commands such as chmod.

How to read permissions shown by ls -l

The familiar ls -l output displays permissions as letters, not as an octal number. A line can begin with -rw-r--r--: the first character identifies the file type, and the next nine characters describe three sets of permissions. Here, - means a regular file; a d in that position means a directory. The remaining characters divide into owner (rw-), group (r--), and others (r--), which corresponds to 644.

For a quick conversion, read each group of three characters and add the matching values. rwx is 7, r-x is 5, rw- is 6, and r-- is 4. Thus -rwxr-x--- corresponds to 750. You can also ask stat for both forms with stat -c '%a %A %n' file: %a is numeric, while %A is the symbolic form.

On Linux, ls -l may show a plus sign after the permission letters when a file has an access control list, or a dot for certain security-context information, depending on the system. Those marks are not extra octal digits. They indicate that additional metadata may affect access and may need to be inspected separately.

Using numeric modes with chmod

To set permissions numerically, use chmod followed by the mode and path. For example, chmod 640 report.txt gives the owner read and write access, gives the group read access, and gives others no permissions. Check the result with stat -c '%a %n' report.txt or ls -l report.txt.

Numeric modes are useful when you want an exact, repeatable result. Unlike a symbolic change such as chmod g+w file, which adds group write permission while retaining other existing bits, chmod 640 file sets the ordinary permission bits to the specified pattern. This makes numeric modes convenient in setup instructions and scripts, but it also means they can remove permissions that were already present.

Be cautious with recursive changes. chmod -R 755 directory applies the same mode to files and directories below that path, potentially making every regular file executable and exposing files to other users. A common pattern is to give directories mode 755 and regular files mode 644, but the right modes depend on what the content is and who should access it. Use a targeted command or a carefully tested find expression when files and directories need different modes.

Why directory permissions behave differently

The same permission bits have related but distinct effects on files and directories. Read permission on a regular file allows its contents to be read; write permission allows changes to its contents; execute permission allows it to be run as a program or script, subject to other requirements such as a valid interpreter. A directory’s read permission allows its entries to be listed, while execute permission allows a user to search or traverse it—for example, to access a known file inside it.

This distinction explains why a directory often needs execute permission even when its contents should not be listed. A user with execute permission but no read permission may be able to access a file if its name is already known and other path permissions allow it. Conversely, read permission without execute permission usually does not provide useful access to entries inside the directory. To reach a file, a user generally needs search permission on each directory in the path, as well as suitable permissions on the file itself.

For example, 700 on a directory gives its owner full access and denies access to the group and others. 755 allows other users to traverse and list it, while only the owner can create, remove, or rename entries there. These are reasons not to copy a mode from a file to a directory without considering how it will be used.

Special permission bits and four-digit modes

Some modes have a leading digit for special permission bits in addition to the owner, group, and other digits. The special digit combines set-user-ID (4), set-group-ID (2), and the sticky bit (1). For example, 4755 includes set-user-ID, 2750 includes set-group-ID, and 1777 includes the sticky bit. If no special bit is set, the leading digit may be omitted.

These bits have particular purposes and should not be added casually. Set-user-ID can make an executable run with the file owner’s effective user ID; set-group-ID can affect executable group identity and, on directories, group inheritance for new entries. The sticky bit is commonly used on shared directories such as /tmp, where it restricts who can remove or rename files within the directory. Exact behavior can depend on the file type and system configuration.

In symbolic output, special bits appear in place of some execute letters. You may see s or S in the owner or group positions, and t or T in the others position. A lowercase letter indicates the corresponding execute bit is also set; an uppercase letter indicates the special bit is set without that execute bit. If a numeric result seems to have four digits, inspect this leading special digit rather than assuming the output is wrong.

Default permissions and umask

New files and directories do not simply inherit one universal permission number. Programs request an initial mode, and the process’s umask removes permission bits from that request. A common starting mode is 666 for regular files and 777 for directories; with a umask of 022, the resulting modes are typically 644 and 755, respectively. Programs can request different modes, so these examples are common outcomes, not a guarantee for every application.

Check the current shell’s mask with umask. The mask is not itself a permission mode to apply with chmod: it specifies bits to withhold when new objects are created. For instance, a umask of 077 commonly results in files accessible only to their owner and directories with owner-only access. Changing the umask affects newly created objects for processes using that setting; it does not change existing file permissions.

Common surprises: ACLs, links, and access errors

An octal mode is useful, but it does not always tell the whole access story. Linux access can also be affected by access control lists (ACLs), ownership, directory traversal permissions, security modules, and filesystem mount options. If ls -l shows a plus sign, inspect an ACL with getfacl filename when that utility is installed. The permission bits shown in the mode can reflect an ACL mask, so a named user’s effective access may not be obvious from the three ordinary digits alone.

Also confirm which object you are inspecting. A symbolic link points to another path; on Linux, permissions on the link itself generally do not control access to its target. GNU stat normally reports information about the link itself, while stat -L follows the link and reports the target. If a command says permission is denied, check the target, its owner and group, and every directory in the path—not just the link’s displayed mode.

Finally, permission bits are not the same thing as ownership. A file marked 640 grants different access depending on which user owns it and which group owns it. Use ls -l or stat -c '%U %G %a %n' file to inspect owner, group, and numeric mode together. If access still does not match expectations, check ACLs and the relevant system or mount policies before broadening permissions.

Related

Frequently Asked Questions

How do I show permissions as numbers for every file in a folder?

For entries in the current directory, use stat -c '%a %n' ./*. To include regular files throughout its subdirectories, use find . -type f -exec stat -c '%a %n' {} +. The second command searches recursively.

Does ls have an option to show octal permissions?

Standard Linux ls -l shows symbolic permissions such as rw-r--r--, not an octal mode. Use stat -c '%a %n' filename for the numeric form, or stat -c '%a %A %n' filename to show both.

What does permission 755 mean?

755 means the owner has read, write, and execute permissions, while the group and others have read and execute permissions. It is common for directories and some executable files, but it is not appropriate for every file.

Why does stat show four digits instead of three?

The first digit represents special bits: set-user-ID, set-group-ID, and the sticky bit. For example, 1777 includes the sticky bit in addition to ordinary permissions of 777. The extra digit is meaningful, not a formatting error.

Are 0644 and 644 different?

No. For ordinary permission modes, the leading zero is a notation indicating octal; it does not add another permission digit. Both forms represent owner read/write, group read, and others read.

Linux File Permissions as Numbers: A Practical Guide
Scroll to top