Ten examples of chmod commands in unix and linux

Source: Internet
Author: User
In unix and linux, the chmod command is used to change the permissions of files and directories. This is a must for unix and linux users to be familiar with many

In unix and linux, the chmod command is used to change the permissions of files and directories. This is one of the many basic commands that unix and linux users must be familiar. In this tutorial on unix commands, we will learn how to use the chmod command to change the file permissions and what file permissions refer to in unix. We will also learn how to use the chmod command in unix to change the permissions of the file directory and its subdirectories. Finally, we will learn how to use the chmod command in unix to change the permissions of executable files. Before going directly to these examples, let's take a few minutes to understand what permissions refer to in unix and why we need to change the file permissions.

In unix, each file has three permission attributes: read, write, and execute. at the same time, there are three types of attributes: file owner, owner group, and others. Therefore, the permissions provided by each file refer to the combination of file permission attributes and type attributes. You can use the ls command to view the permissions of personal files and directories. For example, the following file:

1example @ localhost ~ /Test ls-lrt stock_trading_systems

2-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 stock_trading_systems *

All users, group members, and others have read, write, and execute permissions on them. You can learn more about file permissions in my guide to learn about unix file permissions. For more information about unix and linux permissions, see unix file and directory permissions tutorial.

Chmod command instances in unix and linux

Next let's take a look at some of the more practical and common chmod command instances in unix

Chmod command instance 1: Read-only file properties in unix

In the example of this chmod command, we will know how to make a file read-only to its owner. In the following example, you can grant the read permission to the group members or other types of members, and the file owner has the write permission.

1example @ localhost ~ /Test ls-lrt stock_trading_systems

2-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 stock_trading_systems *

3 # Here we can see that all user types have read, write, and execute permissions on the stock_trading_systems file.

4example @ localhost ~ /Test chmod 400 stock_trading_systems

5 #400 indicates 100 000, that is, r -- --- that is, only the file owner has the read permission.

6example @ localhost ~ /Test ls-lrt stock_trading_systems

7-r -------- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

8 # currently, only the read attributes of a file can be read by the file owner "-r --------"

Chmod command instance 2: only modify the permissions of the owner, group member, or other classes.

In this chmod command example, we will know how to change file permissions at the level of owner, group, and other types. You can also easily change the permissions of any type of members on files. If you use text format, 'u' indicates the file owner, 'O' indicates other types, and 'G' indicates the group in which the file belongs. 'R' indicates readable, 'W' indicates writable, and 'x' indicates executable. + Adds a permission.-removes the permission.

1example @ localhost ~ /Test ls-lrt chmod_examples

2-r -------- 1 example Domain Users 0 Jul 15 :42 chmod_examples

3example @ localhost ~ /Test chmod u + w chmod_examples

4example @ localhost ~ /Test ls-lrt chmod_examples

5-rw ------- 1 example Domain Users 0 Jul 15 :42 chmod_examples

Let's use the chmod command to modify only the file permissions of the group members.

1example @ localhost ~ /Test ls-lrt chmod_examples

2-rw ------- 1 example Domain Users 0 Jul 15 :42 chmod_examples

3example @ localhost ~ /Test chmod g + w chmod_examples

4example @ localhost ~ /Test ls-lrt chmod_examples

5-rw -- w ---- 1 example Domain Users 0 Jul 15 :42 chmod_examples

In this chmod example, we will only change the permissions of other types on files, but will not affect the permissions of the file owner and the owner's group.

1example @ localhost ~ /Test ls-lrt chmod_examples

2-rw -- w ---- 1 example Domain Users 0 Jul 15 :42 chmod_examples

3example @ localhost ~ /Test chmod o + w chmod_examples

4example @ localhost ~ /Test ls-lrt chmod_examples

5-rw -- w-1 example Domain Users 0 Jul 15 :42 chmod_examples

Chmod command instance 3: modify permissions of all type members (file owner + group + others)

In the previous chmod command example of unix, we learned how to change permissions for file owners, groups, and other types of members one by one, however, sometimes you need to modify the permissions of all types of members, not just one of them. If you use text format, 'A' is used to indicate all, and 'U' is used to indicate the file owner.

1example @ localhost ~ /Test ls-lrt linux_command.txt

2-rw -- w-1 example Domain Users 0 Jul 15 :42 linux_command.txt

3example @ localhost ~ /Test chmod a + x linux_command.txt

4example @ localhost ~ /Test ls-lrt linux_command.txt

5-rwx-wx-wx 1 example Domain Users 0 Jul 15 :42 linux_command.txt *

Chmod command instance 4: Use the numeric format of the chmod command to change the permission

In unix, the chmod command not only allows you to change permissions in readable text format, but also allows you to use the octal format in numeric format to express the permission combination. For example, the first digit in 777 indicates the file owner, the second digit indicates the user group, and the third digit indicates other types of users. Now, if you convert the first digit to a binary format, it is written as 111. The first digit indicates the read attribute, the second digit indicates the write attribute. The third digit indicates the executable permission.

1example @ localhost ~ /Test ls-lrt unix_command.txt

2-rw -- w-1 example Domain Users 0 Jul 15 :42 unix_command.txt

3example @ localhost ~ /Test chmod 777 unix_command.txt

4example @ localhost ~ /Test ls-lrt unix_command.txt

5-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 unix_command.txt *

Chmod command instance 5: Use the chmod command to remove file permissions

In this example of using chmod in unix, we will know how to remove various permissions from the file. You can easily remove read, write, or execute permissions from a file using the numeric or text format of the chmod command. The following example shows how to use the text format of-x to remove executable permissions.

1example @ localhost ~ /Test ls-lrt linux_command.txt

2-rwx-wx-wx 1 example Domain Users 0 Jul 15 :42 linux_command.txt *

3example @ localhost ~ /Test chmod a-x linux_command.txt

4example @ localhost ~ /Test ls-lrt linux_command.txt

5-rw -- w-1 example Domain Users 0 Jul 15 :42 linux_command.txt

Chmod command instance 6: Use the chmod command to change directory permissions and recursively change sub-directory permissions

This is the most common example of the chmod command. It provides us with the permission to change any directory and contain all contents including subdirectories and files. The chmod command in unix can recursively change the permissions of any directory by using the-R command parameter. the following example shows the chmod example.

01example @ localhost ~ /Test ls-lrt

02

03 total 8.0 K

04

05-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 unix_command.txt *

06drwxr-xr-x + 1 example Domain Users 0 Jul 15 stocks/

07example @ localhost ~ /Test chmod-R 777 stocks/

08example @ localhost ~ /Test ls-lrt

09 total 8.0 K

10-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 unix_command.txt *

11 drwxrwxrwx + 1 example Domain Users 0 Jul 15 14:33 stocks/

12example @ localhost ~ /Test ls-lrt stocks

13 total 0

14-rwxrwxrwx 1 example Domain Users 0 Jul 15 14: 33 online_stock_exchanges.txt *

Chmod command instance 7: how to remove read/write permissions from files for users of the type used

So far, we have learned how to add read, write, and execute permissions to files and directories. then we will learn the opposite, that is, remove read, write, and execute permissions. The text format is very simple, because we only need to use-to replace +. Just as if + is used to add permissions,-it is used to remove permissions.

1example @ localhost ~ /Test ls-lrt stock_trading_systems

2-rwxrwxrwx 1 example Domain Users 0 Jul 15 :42 stock_trading_systems *

3example @ localhost ~ /Test chmod a-wx stock_trading_systems

4example @ localhost ~ /Test ls-lrt stock_trading_systems

5-r -- r -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

Chmod command instance 8: only sets the executable permission for the directory, but does not change its permission to include files

In many cases, we only want to provide executable permissions for directories or subdirectories so that they can be searched, but do not want to change the permissions of files in these directories. Before guiding this command, I usually find all directories and change their permissions. But in unix, we can use chmod to have a better method. You can use the 'X' (uppercase X) parameter to change the permissions of the file directory without affecting the file. Let's take a look at this usage from the example below.

1example @ localhost ~ /Test ls-lrt

2 total 8.0 K

3-r -- r -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

4drw-rw-rw-+ 1 example Domain Users 0 Jul 15 14:33 stocks/

5example @ localhost ~ /Test chmod a + X *

6example @ localhost ~ /Test ls-lrt

7 total 8.0 K

8-r -- r -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

9 drwxrwxrwx + 1 example Domain Users 0 Jul 15 14:33 stocks/

Remember to use uppercase X. If you use lowercase, all files and directories will be affected.

Chmod command instance 9: Use the chmod command in unix and linux to change the multiple permissions of a file or directory

You can use the user + groups or groups + other combination in a command to change the permissions of files and directories. In the following example, the chmod Command provides executable permissions for the owner and members of the group.

1example @ localhost ~ /Test ls-lrt

2 total 8.0 K

3-r -- r -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

4 drwxrwxrwx + 1 example Domain Users 0 Jul 15 14:33 stocks/

5example @ localhost ~ /Test chmod u + x, g + x stock_trading_systems

6example @ localhost ~ /Test ls-lrt stock_trading_systems

7-r-xr-xr -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems *

Chmod command instance 10: How to copy the permissions of one file to another in unix

This is a very interesting example of the chmod command in unix. it can grant the permissions of one file to another. In the following example, you can easily refer to the source file permission and copy all the permissions of this file to the target file:

1example @ localhost ~ /Test ls-lrt future_trading

2-rwxrwxrwx 1 example Domain Users 0 Jul 15 :30 future_trading *

3example @ localhost ~ /Test ls-lrt stock_trading_systems

4-r -- r -- 1 example Domain Users 0 Jul 15 :42 stock_trading_systems

5example @ localhost ~ /Test chmod -- reference = stock_trading_systems future_trading

6example @ localhost ~ /Test ls-lrt future_trading

7-r -- r -- 1 example Domain Users 0 Jul 15 :30 future_trading

These are common examples of chmod commands in unix and linux. The chmod command is as useful as the find Command and the grep command. it is essential to know how to change the file permissions in unix. If you have other examples of chmod usage that we should know, please share with us.

Original link http://javarevisited.blogspot.com/2012/03/10-example-of-chmod-command-in-unix.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.