×

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to modify the read, write, and execute permissions of files and directories.

The basic syntax of the chmod command is:

chmod [options] [mode] [file(s)]

The "mode" is the permissions that you want to set for the file or files, and the "file(s)" is the file or files that you want to change the permissions for.

The most basic use of the chmod command is to change the permissions of a single file. For example, to give read and write permissions to the owner, and read permissions to everyone else for a file called "fruits.txt", you would use the following command:

chmod 644 fruits.txt

The number 644 represents the permissions in octal notation, where the first digit represents the owner's permissions, the second digit represents the group's permissions, and the third digit represents the permissions for others. The number 6 stands for read and write permissions, and 4 stands for read permissions.

Another way of specifying permissions is by using the letters r, w, and x. The letter r stands for read permission, w stands for write permission, and x stands for execute permission. The permissions can be represented as a combination of these letters. For example, the above command can also be represented as:

chmod u+rw,g+r,o+r fruits.txt

This command gives read and write permissions to the owner, read permission to the group, and read permission to others.

The chmod command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -R: recursive, changes the permissions of the directory and all its contents.
  • +x : adds execute permission to the file.
  • -x : removes execute permission from the file.
  • +w : adds write permission to the file.
  • -w : removes write permission from the file.
  • +r : adds read permission to the file.
  • -r : removes read permission from the file.
  • u : stands for user, used to specify the owner's permissions.
  • g : stands for group, used to specify the group's permissions.
  • o : stands for others, used to specify the permissions for others.

The chmod command can also be used in combination with other commands to perform more complex tasks. For example, you can use the chmod command in combination with the find command to change the permissions of all files in a directory and its subdirectories. For example, the following command will change the permissions of all files in the "fruits" directory and its subdirectories to 644:

find fruits -type f -exec chmod 644 {} +

It is important to note that the chmod command can only be used by the owner of the file or by a user with superuser privileges. This is because the chmod command modifies the ownership and permissions of a file, which are considered sensitive operations.

Another important thing to consider when using the chmod command is the use of the chmod mask. The chmod mask is a set of permissions that are applied to all new files and directories that are created within a directory. This can be set using the command "umask". For example, the following command will set the chmod mask to 022, which will give read and write permissions to the owner, and only read permission to others:

umask 022

It's important to be aware of the chmod mask when creating new files and directories, as it can affect the permissions that are set on those files and directories.

Another thing to consider is the use of the chmod command with symbolic links. A symbolic link is a type of file that points to another file or directory. When using the chmod command on a symbolic link, the permissions of the link itself are modified, not the permissions of the file or directory that it points to. To modify the permissions of the file or directory that a symbolic link points to, the option -h should be used.

chmod -h 755 mylink

In addition to the chmod command, there is also the chown command which can be used to change the ownership of files and directories. The chown command can be used to change the owner and group of a file or directory. It is similar to the chmod command in that it can be used to change the permissions of a file, but it is used to change the ownership rather than the permissions.

chown user:group file

In summary, the chmod command in Linux is a powerful command-line utility that is used to change the permissions of files and directories. It is essential for anyone who works with files and directories in Linux, as it allows you to control access to files and directories and ensure that they are used in a secure and appropriate way. The chmod command has several options that can be used to modify its behavior, such as recursive and specifying permissions for the owner, group, or others. The chmod command can also be used in combination with other commands to perform more complex tasks. It is important to be aware of the chmod mask, the use of symbolic links, and the chown command when working with files and directories in Linux.


Related Topics

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.