In the process of installing Jenkins recently, I used the operation command to modify file permissions on the Mac system again. I checked the use of chmod command before, but I forgot it again. In a fit of pique, record here, facilitate follow-up memo inquiry. Have need, might as well take reference.

Modifying file access permissions on a PC is a common operation, which is often used when installing some software. The chmod command in Linux is used to change file permissions on a Mac terminal. This section describes how to use the command in detail.

You can also view the help manual of the command on the terminal tool by using man chmod.

View file Permissions


You can run the ls -l command to view the access permissions of all files in the current directory or a specified file. For example, check the access permission of the startup.sh file in the Tomcat bin directory:

yifeng:bin yifeng$ ls -l startup.sh
-rwxrwxrwx@ 1 yifeng  staff  1904  9 27 18:32 startup.shCopy the code

The meanings of each part in the preceding information are explained as follows:

File Type and Access Permission File Quantity Owning user group File size Modification Date (month/day/hour) File nameCopy the code

-rwxrwxrwx is used as an example. The first symbol indicates the file type. The hyphen (-) indicates that the file is not a directory type. (The @ symbol at the end indicates the file extension property, which is a feature of the file system.)

The last nine letters are divided into three groups. Each group corresponds to the permission of the owning user, the owning user’s group, and other users to access the file.

The three characters “RWX” in each group indicate the read, write, and execute permissions of the user. If the user has no permissions, use the hyphen (-) instead.

Modifying Access Rights


As described in the view permissions section above, modifying permissions should also include user access, add or cancel operations, specific permissions, and file access, namely:

Chmod User + operation + permission fileCopy the code

User: u indicates the file owner (user), G indicates the group of the file owner, O indicates other users, and A indicates all users (including the previous three types of users).

Operation part: “+” symbol means to increase permission, “-” symbol means to cancel permission, “=” symbol means to assign permission;

Permission: R indicates read, W indicates write, and X indicates execute.

File part: If the file name is not specified, the operation object is all files in the current directory.

The startup.sh file is used as an example to change the read and write permissions of the owning group and other users and cancel the execute permissions as follows:

chmod go-x startup.shCopy the code

Then use the ls command to view permissions,

yifeng:bin yifeng$ ls -l startup.sh
-rwxrw-rw-@ 1 yifeng  staff  1904  9 27 18:32 startup.shCopy the code

You can see that the file access permission has been changed as required.

For more complex operations, multiple operators can be used to add and cancel permissions at the same time, and the ‘, ‘symbol can be used to change permissions for different user ranges at the same time. For example:

chmod g+x,o+x-w startup.shCopy the code

Another simple way to write this is to use numbers to indicate the read/write/executable permission type of the permission section. The corresponding relationship between numbers and permission types can be seen intuitively in this figure:

That is, 1 indicates executable, 2 indicates writable, and 4 indicates readable. The value obtained by adding the numbers of each type represents the common type of the intersecting part.

In this case, three numbers can be used to represent permission changes for three different user types. For example, to change the access permissions of all users to read, write, and execute (RWX), use:

chmod 777 startup.shCopy the code

The three digits indicate the access rights of u, G, and O users from front to back, and can be changed as required.

As a bonus, sometimes you need to recursively change the file types in directory files and their subdirectories, using the -r option.

About me: Yifeng, blog address: Yifeng. Studio /, Sina Weibo: IT Yifeng

Scan the QR code on wechat, welcome to follow my personal public account: Android Notexia

Not only share my original technical articles, but also the programmer’s workplace reverie