Recent Posts
- jQuery attrAugust 12, 2021
- jQuery mouseenterAugust 9, 2021
- jQuery ToggleclassAugust 6, 2021
- jQuery attr
Introduction to chmod Command in Linux
In Linux/Unix like operating system, the chmod command is used to change the access mode of a file. It is used to change the permission for files and folders. These permissions are given to file/folder to provide a secure environment to the OS, efficient management of a file and high-level access to the users accessing the files/ folders.
The permissions to access the files or folders are mainly:
Permissions are given to each file or folder to three sets of people. They are:
Below, we are talking about the chmod commands in Linux:
Syntax of chmod:
chmod [options].. [permission] file..
chmod [options].. numeric mode file..
chmod [options].. reference=file files..
Operators used in chmod Command in Linux
Operators are used to specifying which mode of access is to be given to a file/folder. These operators include:
Operations | Descriptions |
+ | Adds the specified permission/mode to access |
– | Removes the specified permission/mode to access |
= | To replicate the specified permission to the other class of the owner/group/others |
To check the options that are available in chmod, we can do by using Linux command:
man chmod
Examples of chmod Command in Linux
Following are the examples of chmod commands in Linux explained in detail.
1. chmod Command using Operator Method
Let us take an example where a file test_file.txt has full permission to the owner, group and other.
-rwxrwxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Example #1
Let us try now giving only read and write permission to the owner(Nikhil) and other users to have only read permission. We can do this by :
Syntax:
chmod u=rw,og=r test_file.txt
Output: -rw-r–r– 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Example #2
We can give execute permission to all (owner, group, others) at one shot by:
Syntax:
chmod a+x test_file.txt
Output: -rwxr-xr-x 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Example #3
We can remove read permission for the group by:
Syntax:
chmod g-r test_file.txt
Output: -rwx-wxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
2. Change Permission in Linux Through Numeric Code
Below are the Numeric Codes to be Known Before we Start Changing the Permissions:
Permission Numbers are to be Known by Following the below Numeric Codes:
Example #1
To give read, write, execute permissions to all owner, group and others, we can use the below syntax:
Syntax:
chmod 777 test_file.txt
Output: -rwxrwxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Example #2
To give read permission to others and removing all the permissions from the owner and group.
Syntax:
chmod 007 test_file.txt
Output: ——-rwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Example #3
To give write and execute permission to all, we can use the below syntax:
Syntax:
chmod 333 test_file.txt
Output: –wx-wx-wx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
3. chmod Permission for the Directory
We can use the chmod option for changing the permissions to the directory as well. We can use a similar syntax as we have done it earlier for files to change the permissions for the directory. A few examples are listed to explain the use of chmod.
Let us take a directory having all read-write execute permission on a directory.
drwxrwxrwx 2 nikhil group users 4096 Nov 29 05:48 test_directory
Example #1
To remove all execute permission for the test_directory using operator code. We can use the below syntax:
Syntax:
chmod a-x test_directory/
Output: drw-rw-rw- 2 nikhil group users 4096 Nov 29 05:48 test_directory
Example #2
To give read and execute permission to the owner, group, and others by numeric code. We can do by using the below syntax:
Syntax:
chmod 755 test_directory/
Output: drwxr-xr-x 2 nikhil group users 4096 Nov 29 05:48 test_directory
Example #3
To remove write permission from others using operator function, we can use the below syntax:
Syntax:
chmod o-w test_directory/
Output: drwxrwxr-x 2 nikhil group users 4096 Nov 29 05:48 test_directory
chmod permission using –reference can copy the permission from one file to another file in Linux using –reference command.
Basic Syntax:
chmod –reference ref_file target_file
Example: Let us take two files, file 1 having all read-write-execute permission and another file 2 having only read permission for owner, group and other.
-rwxrwxrwx 1 nikhil group users 0 Nov 29 06:02 file1.txt
-rw-r–r– 1 nikhil group users 0 Nov 29 06:02 file2.txt
Now if we have to copy the permission from file1 to file2, we can use the below syntax:
Syntax:
chmod –reference file1.txt file2.txt
Example: chmod command to give multiple permission to files at the same time. We can give multiple permissions to a file at the same time or in one command by using the below syntax:
Syntax:
chmod u=x,g-w file1.txt
Output: —xr-xrwx 1 nikhil group users 0 Nov 29 06:02 file1.txt
Conclusion
In this article, we have learned how to change permissions for a file or directory with the help of the chmod command with different methods like numeric, operators and reference. By granting permission, we can now secure the usability of files by other users.
MCP is the right place for best computer Courses institute and advance Courses in Mohali and Chandigarh.The Complete Programming Academy can change your life – providing you with the knowledge, skills, and performance in a second language which helps you to excel in your job.You can also Contact us for 6 month industrial training institute in Mohali.