Linux chmod Command
Linux chmod Command
In this tutorial, we will learn about the chmod Linux command with Examples. chmod is a short form of ‘change mode’ .
The <em>chmod</em>
command in Linux is used to change the modes and permissions of files and directories. Permissions determine who can read, write, or execute a file.
Linux follows a permission model that includes three types of users: the owner, the group, and others. The <em>chmod</em>
command allows users to modify these permissions using symbolic or numeric modes.
chmod Flags
Flag | Description |
---|---|
-R |
Recursively change permissions for all files and directories inside a specified directory. |
--reference=FILE |
Change permissions to match those of a specified file. |
--verbose |
Display detailed information about the permission changes. |
--silent or --quiet |
Suppress error messages. |
Examples
Give Executable Permission to a Script File
To make a script file executable, use the following command:
$ chmod +x script.sh
Notice how the script.sh changed to green color after giving the executable permission. Now, you can run the script using:
$ ./script.sh
Set Read, Write, and Execute Permissions for the Owner
To allow the owner to read, write, and execute a file while giving others only read permission:
$ chmod 744 file.txt
You can also use symbolic notation:
$ chmod u=rwx,g=rx,o= file.txt
The above chmod
command grants the owner read, write, execute, the group read and execute, and others no permissions on the file.
Remove Write Permission from a File
To remove write permission for all users:
$ chmod a-w file.txt
Recursively Change Permissions of a Directory
To make all files inside a directory readable and executable for everyone:
$ chmod -R 755 /path/to/directory
The number 755
in chmod represents the permission settings for a file or directory. It consists of three digits, each corresponding to a different user category: owner, group, and others.
Digit | Binary | Permission | Description |
---|---|---|---|
7 | 111 | rwx | Owner has read (r), write (w), and execute (x) permissions. |
5 | 101 | r-x | Group has read (r) and execute (x) permissions, but no write permission. |
5 | 101 | r-x | Others have read (r) and execute (x) permissions, but no write permission. |
Set Permissions to Match Another File
To copy the permissions of one file to another:
$ chmod --reference=example.txt myfile.txt