One day a shell command Linux text Manipulation Series-chmod command usage _linux shell

Source: Internet
Author: User
Tags bit set chmod

First to familiarize yourself with the following permissions, an example of the above Comm document as an example
$ls-L

-rw-rw-r--1 yjplxq yjplxq 36 July 22:40 A.txt
-rw-rw-r--1 yjplxq yjplxq 27 July 22:41 B.txt
-rw-rw-r--1 yjplxq yjplxq 1231 July 22:45 comm.txt
-rwxrwxr-x 1 yjplxq yjplxq 0 July 11:22 comm.sh
drw-rw-r--1 yjplxq yjplxq 1231 July 22:45 Subcomm

The first column explains:

"-" refers to the ordinary document
"D" refers to the catalogue
"C" refers to the character device
"B" block equipment
"L" character connection
"S" sockets
"P" Pipeline

The remainder of the first column is divided into three groups, the first group of three characters (RWX) represents user rights, the second group corresponds to the user group permissions, and the third group corresponds to other user rights.

Permissions Brief:

R refers to read reading permission
W refers to write permission
X refers to execute execution

S is only used to replace the "X" permission bit setuid or setgid
T is only used to replace the "X" permission bit the sticky bit directory has a sticky bit set, only the user who created the directory can delete the catalog file

Let's get acquainted with the next chmod.

Chmod is used to change your operating rights, full name: changing Modle. Very good to remember.

Familiar with the next--help:

Usage: chmod [options] ... mode [, Mode] ... File...
Or: chmod [options] ... Octal mode file ...
Or: chmod [options] ...--reference= reference file ...
Change the mode of each file to the specified value.

-C,--changes similar to--verbose, but displays results only if there are changes
--no-preserve-root does not treat the root directory specifically (default)
--preserve-root prevents recursive operation of the root directory
-F,--silent,--quiet Remove most of the error messages
-V,--verbose displays diagnostic information for all files processed
The--reference= reference file uses the schema of the specified reference file, rather than specifying the permission mode yourself
-R,--recursive changes all files and subdirectories recursively
--HELP displays this help information and exits
--version display version information and exit

Each mode should be of this type [ugoa]* [-+=] ([rwxxst]*|[ Ugo])) + ".

Interpretation:

1. Eight in-mode files

Look, the noun is a bit big, but it's very simple.

r=4 w=2 X=1 So if you see this expression:
rwxrw-r--can actually be represented as 764

So you can do this chmod 764 ${filename} at this time, filename user has read and write permission, and the user group has read and write permissions, other users have only the right to reading.

2.--reference= Reference Document

You can set the permissions of a file as a template, and then
chmod--reference=${Reference file} ${modified file}

3. Each mode should belong to "[ugoa]* [-+=] ([rwxxst]*|[ Ugo])) + ".

Example:

3.1.chmod o=u filename #filename其他用户有和用户同样的权限
3.2.A refers to all chmod a+x filename #filaname所有用户增加执行权限

Practical Skills:

1. Recursively modify all permissions under current directory

$chmod 777. –R

2. Develop permission templates such as u7.tl for user read and write execution rights templates

How to $chmod--reference= reference files

3. Set Sticky bit

$chmod a+t Directory_name

Chmod is a must master, often used, the number of times is not a frequent operation (feel the contradiction is it), hehe.

Related commands: Chown, W, who

chmod----Change the access mode of one or more files (mode)

chmod [options] mode files

Only file owners or privileged users can use this feature to change file access mode. Mode can be a digital form or expressed in the form of who opcode permission. Who is optional, by default is a (all users). Only one opcode (opcode) can be selected. Multiple mode can be specified, separated by commas.
Options:
-c,--changes
Output only changed files
-f,--silent,--quiet
users who do not notify files when chmod cannot change file mode
--help
Output help information.
-r,--recursive
Recursively iterates through subdirectories, setting permissions by modifying the permissions of all files and subdirectories
--reference=filename
in the directory to reference filename
-v,--verbose
Output information for each file
--version
Output version information, regardless of whether the modification was successful.
who
U
users
G
Group
O
Other
a
All users (default)
opcode
+
Add permissions
-
Delete Permissions
=
Reassign rights Limited to
Permission
R
Read
W
Write
x
Perform
S
Set User (or group) ID number
T
set sticky bit (sticky bit), prevent file or directory from being non-primary delete
U
User's current permissions
G
Group's current Permissions
O
Other users ' current permissions
as a choice, most of us use the form of three-bit octal digits to represent permissions, the first to specify the owner's permissions, the second to specify group permissions, and the third to specify the permissions of other users. Permissions are defined for each digit by 4 (read), 2 (write), 1 (execute) three numeric values. If 6 (4+2) Representative has the right to read and write, 7 (4+2+1) has read, writes and executes the permission. The
can also set the fourth bit, which precedes the three-bit permission sequence, and the fourth digit value is 4,2,1, which means the following:
4, set the user ID at execution time to authorize the file-based process, rather than to the user who created the process.
2, set the user group ID at execution time to authorize the process based on the group of files, rather than the user who created the process.

1, set the sticky bit.

Instance:

$ chmod u+x file to add execute permissions to owner of file
$ chmod 751 file assigns read, write, execute (7) permissions to the owner of file, assigns read, execute (5) permissions to the group that contains the file, and assigns permissions to other users for execution (1)
$ chmod u=rwx,g=rx,o=x Another form of the example in the file
$ chmod =r File Assign Read permissions to all users
$ chmod 444 File Previous Example
$ chmod a-wx,a+r File Previous example
$ chmod-r U+r directory recursively assigns read permissions to the owner of all files and subdirectories under the directory directory
$ chmod 4755 Sets the read, write, and execute permissions assigned to the owner by ID, assigning read and Execute permissions to groups and other users.

Related Article

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.