DEV Community

VAtSea
VAtSea

Posted on

What `-rwxr-xr-x` Really Means

File mode

Ever wondered what this -rw-r--r--@ means in this list?

➜  demo git:(main)ls -l
drwxr-xr-x@ 2 johndoe  staff  64  3 Apr 11:12 mydir
-rwxr-xr-x@ 1 johndoe  staff   0  3 Apr 11:12 myfile
Enter fullscreen mode Exit fullscreen mode

This is called the file mode. It shows the file type and the permissions set on that file or directory.

Let's break down what each bit represents.

-rwxr-xr-x
Enter fullscreen mode Exit fullscreen mode

The first bit represents the file type. It can indicate a regular file, a directory, or a symlink.

  • d represents a directory
  • - represents a regular file
  • l represents a symlink

Note: A symlink is like a shortcut. It points to another file or directory.

The next 9 bits represent the permissions for the file/directory.

r represents read permission
w represents write permission
x represents execute permission

They come in sets of 3:

  • The first 3 bits represent the owner permissions
  • The second 3 bits represent the group permissions
  • The third 3 bits represent the permissions for others

In this example rwx r-x r-x:

  • rwx represents the owner permissions. The owner can read, write to, and execute the file.
  • r-x represents the group permissions. Users in the group can read and execute the file, but they cannot write to it.
  • r-x represents the permissions for others. Everyone else can read and execute the file, but they cannot write to it.

You may also notice an extra @ at the end of the output from ls -l on macOS. That is not part of the file mode itself. It usually indicates that the file or directory has extended attributes.

How to update the permissions

We can update permissions with the chmod command.
It has two modes that we can use:

  • Symbolic mode
  • Numeric mode

Numeric mode

Let's go through numeric mode first.

Each of these permissions in rwx has a number:

Permission Number
r 4
w 2
x 1

These numbers are used to represent permissions in numeric form. For example, rwxr-xr-x means the owner has full permissions, while group and others have read and execute permissions. This is written as 755.

rwxr-xr-x = (4 + 2 + 1) (4 + 1) (4 + 1) = 7 5 5

Here is a handy table for numeric permissions:

Octal Permission
7 rwx
6 rw-
5 r-x
4 r--

Examples:

chmod 444 file      # Only read permissions for everyone
chmod 544           # Read and execute permission for owner, only read permission for others
Enter fullscreen mode Exit fullscreen mode

Wondering about other combinations, such as -w- ? Yes, they are valid. Some combinations are uncommon or less useful in practice, but experimenting with them in a test directory is a good way to understand how permissions behave.

Symbolic mode

Now let's take a look at symbolic mode, which you can use with chmod.

chmod [who][operator][permissions] file

This is the basic structure of symbolic mode.

  • who can be u, g, o, or a: user/owner, group, others, and all
  • operator can be -, +, or =: remove permission, add permission, or set exact permission
  • permissions can be r, w, or x

Examples:

chmod u-rwx file    # remove read write execute permissions from user (owner)
chmod u+x file      # Add execute for owner
chmod g-w file      # Remove write permission from group
chmod a=rw file     # Set read and write access for everyone
Enter fullscreen mode Exit fullscreen mode

Top comments (0)