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.