Linux commands (6/11)--Modify the file's user group CHGRP and the file owner Chown

Source: Internet
Author: User
Tags chmod

In the LUnix system, the control of the permissions of a file or directory is managed by the owner and the group being sued. You can use the CHGRP command to change the file and directory to belong to the group, this way with the group name or group identification code can be. The CHGRP command is the abbreviation for change Group! The group name to be changed must exist within the/etc/group file.

1. Command format:

CHGRP [Options] [group] [file]

2. Command function:

The CHGRP command can change the group that the file or directory belongs to by using a group name or a group identifier. The usage permission is superuser.

3. Command parameters:

Necessary parameters:

-C Output Debug information when a change occurs

-F does not display error messages

-R handles all files in the specified directory and its subdirectories

-V Run-time display verbose processing information

--dereference acts on the point of a symbolic link, not the symbolic link itself

--no-dereference acting on the symbolic link itself

Select parameters:

--reference=< files or Directories >

--HELP Display Help information

--version displaying version information

4. Usage examples:

Example 1: Changing the group properties of a file

Command:

Chgrp-v bin Log2012.log

Output:

[email protected] test]# LL

---xrw-r--1 root root 302108 11-13 06:03 log2012.log

[Email protected] test]# Chgrp-v bin Log2012.log

The owning group for "Log2012.log" has changed to Bin

[email protected] test]# LL

---xrw-r--1 root bin 302108 11-13 06:03 log2012.log

Description

Change the Log2012.log file from the root group to the bin group

Example 2: Changing the group properties of a file according to a specified file

Command:

Chgrp--reference=log2012.log Log2013.log

Output:

[email protected] test]# LL

---xrw-r--1 root bin 302108 11-13 06:03 log2012.log

-rw-r--r--1 root root 11-13 06:03 Log2013.log

[Email protected] test]# chgrp--reference=log2012.log log2013.log

[email protected] test]# LL

---xrw-r--1 root bin 302108 11-13 06:03 log2012.log

-rw-r--r--1 Root bin 11-13 06:03 Log2013.log

Description

Change the group properties of the file Log2013.log to make the group properties of the file Log2013.log the same as the group properties of the reference file Log2012.log

Example 3: Changing the group properties of the specified directory and all files under its subdirectories

Command:

Output:

[email protected] test]# LL

Drwxr-xr-x 2 root root 4096 11-30 08:39 Test6

[Email protected] test]# CD TEST6

[email protected] test6]# LL

---xr--r--1 root root 302108 11-30 08:39 linklog.log

-rw-r--r--1 root root 0 11-30 08:39 log2017.log

[Email protected] test6]# CD.

[Email protected] test]# Chgrp-r bin Test6

[Email protected] test]# CD TEST6

[email protected] test6]# LL

-rw-r--r--1 Root bin 11-30 08:39 Log2013.log

-rw-r--r--1 root bin 0 11-30 08:39 Log2014.log

-rw-r--r--1 root bin 0 11-30 08:39 Log2015.log

-rw-r--r--1 root bin 0 11-30 08:39 Log2016.log

-rw-r--r--1 root bin 0 11-30 08:39 Log2017.log

[Email protected] test6]# CD.

[email protected] test]# LL

Drwxr-xr-x 2 root bin 4096 11-30 08:39 Test6

[Email protected] test]#

Description

Changes the group properties of the specified directory and all files under its subdirectories

Example 4: Changing file group properties by group ID

Command:

Chgrp-r Test6

Output:

[Email protected] test]# chgrp-r TEST6

[email protected] test]# LL

Drwxr-xr-x 2 root users 4096 11-30 08:39 Test6

Directive Name: chown permission to use: root
How to use: chown [-cfhvR] [--help] [--version] user[:group] file…
The chown can be used to change the owner of the file. This instruction is only used by the system administrator (root), the general user does not have the authority to change the other person's file owner, and does not have the authority to change the owner of their own files to other persons. Only the system administrator (root) has this permission.
Parameters:

-C or-change: The function is similar to-V but only returns the modified part
-F or –quiet or –silent: Do not display error messages
-H or –no-dereference: Only modify the symbolic linked files without changing any other related files
-R or-recursive: recursively handles all files and subdirectories under the specified directory
-V or –verbose: show instruction execution process
–dereference: The effect is just the opposite of-H
Help: Show online instructions
–reference=< reference file or directory;: Set the owner of the specified file or directory and the owning group to the same owner of the referenced file or directory as the owning group
–version: Display version information
Chown Command Use Example: (like I use the VPS, you must first SSH login)

# chown [-R] [用户名称] [文件或目录]
# chown [-R] [用户名称:组名称] [文件或目录]

Example 1: Change the owner of the Test3.txt file to test user.
# ls -l test3.txt
-rw-r–r–1 Test root 0 2009-10-23 9:59 test3.txt
# chown test:root test3.txt
# ls -l test3.txt
-rw-r–r–1 Test root 0 2009-10-23 9:59

Example 2:chown can be used between the new owner and the new genus Group: join, one of the owners and the other can be empty. If the owner is empty, it should be ": belong to the group", if the group is empty, ":" Can not be taken.

# ls -l test3.txt
-rw-r–r–1 Test root 0 2009-10-23 9:59 test3.txt

# chown :test test3.txt <==把文件test3.txt的属组改为test
# ls -l test3.txt
-rw-r–r–1 test test 0 2009-10-23 9:59 test3.txt

Example 3:chown also provides the-R parameter, which is extremely useful for directory change owners and groups, and can be changed to a new owner or group by adding the-R parameter to all files in a directory.
# ls -l testdir <== 查看testdir目录属性
Drwxr-xr-x 2 usr root 0 2009-10-56 10:38 testdir/<== file owner is usr user, group is root user
# ls -lr testdir <==查看testdir目录下所有文件及其属性
Total 0
-rw-r–r–1 usr root 0 2009-10-23 10:38 test1.txt
-rw-r–r–1 usr root 0 2009-10-23 10:38 test2.txt
-rw-r–r–1 usr root 0 2009-10-23 10:38 test3.txt
# chown -R test:test testdir/ <==修改testdir及它的下级目录和所有文件到新的用户和用户组
# ls -l testdir
Drwxr-xr-x 2 test test 0 2009-10-23 10:38 testdir/
# ls -lr testdir
Total 0
-rw-r–r–1 test test 0 2009-10-23 10:38 test1.txt
-rw-r–r–1 test test 0 2009-10-23 10:38 test2.txt
-rw-r–r–1 test test 0 2009-10-23 10:38 test3.txt

1 CHGRP Change the group to which the file or directory belongs

① Use the man chgrp command to view CHGRP documentation in Linux.

② command format

CHGRP [-Parameter options] group Dir/file

If you want to change all files or subdirectories under the directory, add the parameter-R.

③ Example

CHGRP Users Install.log

Change the group that belongs to the file Install.log to users

2 Chown Change the user of the file or directory, or modify the group you belong to

① Use the man chown command to view chown documentation in Linux.

② command format

chown [-Parameter options] User name directory or file name

chown [-Parameter options] User name: Group name directory or file name

If you need to change subdirectories or included files, add parameter-R.

③ Example

Chown bin Install.log

Change the owning user of the Install.log file to Bin

Chown Bin:root Install.log

Change the owning user of the Install.log file to bin and the owning group to root.

3 chmod permissions to change directories or files

① Use the man chmod command to view chmod documentation in Linux.

② Permission Description

Linux files have a basic line of nine, for-rwxrwxrwx, three for a group, respectively, represents the Owner/group/others three identity read/write/execute permissions, if the corresponding location is-, the representative does not have the appropriate permissions.

There are two ways to change a permission command: digital and symbolic

③ Digital Way to change file permission type

R:4,w:2,x:1

chmod xyz directory or file name

X is the sum of the three permissions corresponding to the owner identity, y, Z, and so on.

④ Changing the file permission type by symbolic mode

U on behalf of user identity, G for group identity, O for Others,a on behalf of all identities;

+ delegate Add permission,-delegate remove permission, = Delegate Set permission

Example:

chmod u=rwx,go=x Install.log

Set the Install.log file to the user to read and write and execute permissions, group and others set the executable permissions

chmod a+w Install.log

Set all users to have write access to the Install.log file

chmod u-x Install.log

Remove user identity from Execute permissions on Install.log file

⑤ description

+ and-Do not change the original permissions of the document, = ALL permissions are reset for the original document.

Linux commands (6/11)--Modify the file's user group CHGRP and the file owner Chown

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.