Skip to main content

Using`chmod` Command in Numeric Mode

Understanding Octal Numbers in the Context of chmod

When you're dealing with file permissions in Linux, you often encounter octal numbers. An octal number system is a base-8 number system, which means it includes the numbers 0 to 7. Octal numbers offer a shorthand to represent the standard Linux file permissions.

The octal number is computed from three binary digits, which align with the rwx (read, write, execute) permission system.

  • r (read) is 4 in octal, or 100 in binary.
  • w (write) is 2 in octal, or 010 in binary.
  • x (execute) is 1 in octal, or 001 in binary.

Combining these, we get the octal representation of Linux file permissions.

Octal, Binary, and File Mode Table

Here's a table that correlates octal numbers, their binary representation, and their rwx format:

Octal NumberBinary RepresentationFile Mode (rwx Format)
0000---
1001--x
2010-w-
3011-wx
4100r--
5101r-x
6110rw-
7111rwx

How Octal Numbers Work in chmod

Using the octal system, you can represent complex permissions in a compact form. Here’s how to interpret an octal number like 755, commonly seen in Linux file permissions:

  • The first digit (7) refers to the owner's permissions. 7 in octal corresponds to 111 in binary, which means rwx in file mode.
  • The second digit (5) refers to the group's permissions. 5 in octal corresponds to 101 in binary, which translates to r-x in file mode.
  • The third digit (5) refers to everyone else's permissions, similar to the group's permissions in this case (r-x).

Therefore, chmod 755 some_file_or_directory would result in the following permissions:

  • Owner: rwx
  • Group: r-x
  • Others: r-x

Understanding octal numbers is crucial for efficiently managing Linux file permissions. It not only speeds up the process but also reduces the chances of setting incorrect permissions.

The chmod Command and Octal Numbers: Changing File and Directory Permissions in Linux

Introduction

In the Linux operating system, managing file and directory permissions is a crucial skill. While symbolic modes (rwx) offer an intuitive way to manage these permissions, octal numbers provide a quick and efficient alternative. This article dives into how you can use octal numbers with the chmod command to change permissions.

Basic Syntax

The chmod command with octal numbers follows this basic syntax:

chmod [OPTION] MODE FILE
  • OPTION: Optional flags that modify the command's behavior.
  • MODE: The octal number representing the new permissions.
  • FILE: The file or directory whose permissions you wish to change.

Using chmod with Octal Numbers

When using octal numbers to specify file permissions with chmod, the format generally used is a three-digit number. Each digit in this number represents a group of permissions:

  • The first digit represents the permissions for the owner.
  • The second digit represents the permissions for the group.
  • The third digit represents the permissions for others.

Simple Examples

Here are some straightforward examples that cover essential use-cases:

Give read, write, and execute permissions to the owner:

chmod 700 myfile.txt

In this example, the owner has rwx permissions (7 in octal), and all other permissions are set to zero.

Give read and execute permissions to everyone:

chmod 555 myfile.txt

Here, the owner, group, and others have r-x permissions (5 in octal).

Give read, write, and execute permissions to the owner, and read and execute permissions to the group and others:

chmod 755 myfile.txt

Here, the owner has rwx permissions (7 in octal), and the group and others have r-x permissions (5 in octal).

Advanced Examples

You can also set permissions for directories and utilize some advanced features of chmod:

Recursively Change Permissions for Directories:

To change the permissions of a directory and all its contents recursively, you can use the -R option:

chmod -R 700 myfolder/

Apply Different Permissions for Directories and Files:

You can combine find with chmod to apply different permissions to directories and files:

For Directories:

find /path/to/folder -type d -exec chmod 755 {} \;
  • find: The find command used for searching files and directories.

  • /path/to/folder: Replace this with the path to the folder where you want to change permissions. find will search here and in all its subdirectories.

  • -type d: This option tells find to look only for directories.

  • -exec: This option allows you to pass another command (chmod 755 in this case) that will be executed on each file or folder that find locates.

  • chmod 755: The chmod command sets the permissions. 755 gives read, write, and execute permissions to the owner, and read and execute permissions to the group and others.

  • {}: This is a placeholder for each item found. chmod 755 will be run on each directory found.

  • \;: This indicates the end of the -exec command. The backslash is necessary to escape the semicolon, which is a special shell character.

For Files:

find /path/to/folder -type f -exec chmod 644 {} \;

This command is almost identical to the previous one, but there are some key differences:

  • -type f: This option tells find to look only for files (not directories).

  • chmod 644: Sets read and write permissions for the owner, and read-only permissions for the group and others.

By using these commands, you can apply different sets of permissions to directories and files within the same parent directory, providing a granular level of control.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle