Detailed explanation and application examples of cp, chmod, chown, chgrp, grep commands

cp

Function: Copy files or directories

Common options:

-a archive

-b Create a backup if the target file exists. The backup file is the file name followed by ~

-f forces a file or directory to be copied

-r copies directories recursively

-p retains original file or directory attributes

-i ask user before overwriting file

-u Copy only when the source file has a newer modification time than the destination file

-v displays copy information

Example:

Copy directory:

# cp -rf test /opt

chmod

Permissions to operate three types of users: use octal to
 chmod [-R] OCTAL-MODE FILE...

        Permissions to operate users of specified categories: =
            u: Owner
            g: Belonging group
            o: Others
            a: all

            Specify one or more categories=mode

            chmod u=rw 
            chmod ug=rw
            chmod o= 

        Operate the user-specified permission bits of the specified category: +/-
            chmod u+x 
            chmod +w 

        --reference=FILE: Reference FILE permissions

        -R: Recursive modification

Exercise: Copy /etc/skel and all the contents inside it to /home/mageedu; This directory and all files inside it are required to have no access rights to other users and groups; # cp -r /etc/skep /home/
        mageedu
        # chmod -R go= /home/mageedu

 chown

The chown command changes the owner and group of a file or directory. This command can authorize a user to become the owner of the specified file or change the group to which the file belongs. The user can be user or user D, and the user group can be group name or group id. The filename can be a space-separated list of files, and the filename can contain wildcard characters.

Only file owners and super users can use this command.

grammar

chown(option)(parameter)

Options

-c or --changes: The effect is similar to the "-v" parameter, but only the changed parts are reported;

-f or --quite or --silent: Do not display error messages;

-h or --no-dereference: Only modify the symbolic link file without changing any other related files;

-R or --recursive: recursive processing, processing all files and subdirectories in the specified directory together;

-v or --version: displays the instruction execution process;

--dereference: The effect is the same as the "-h" parameter;

--help: online help;

--reference=<reference file or directory>: Set the owner and group of the specified file or directory to be the same as the owner and group of the reference file or directory;

--version: Display version information.

parameter

User:Group: Specify the owner and working group to which it belongs. When ":group" is omitted, only the file owner is changed;

Files: Specify the list of files to change owner and workgroup. Supports multiple files and targets, supports shell wildcards.

Example

Change the file owner of the directory /usr/meng and all files and subdirectories under it to liu:

chown -R Liu /usr/meng

 chgrp

Function: Change the user group to which the document belongs

Syntax: #chgrp -R groupname path to document

grep

Syntax format:
            grep [option]... 'PATTERN' FILE...

                --color=auto

        Regular expressions:
            are patterns written by a type of characters, many of which do not represent their literal meaning, but express functions such as control or wildcards;
                metacharacters: do not represent their literal meaning, but are used for additional functional descriptions

            Regular Expressions: Regular Expression Engine

            Basic regular expressions: grep
            extended regular expressions: egrep, grep -E
            fgrep: fast, the use of regular expressions is not supported

        Metacharacters of basic regular expressions:
            Character matching:
                .: Matches any single character
                []: Matches any single character in the specified range
                    [0-9], [[:digit:]]
                    [az], [[:lower: ]]
                    [AZ], [[:upper:]]
                    [[:space:]]
                    [[:punct:]]
                    [[:alpha:]]
                    [[:alnum:]]
                [^]:
            Number of times to match metacharacters: Used to specify the number of times that the character before it can appear
                *: any length, the character before it can appear any number of times,
                    for example: x*y
                        xxy, xyy, y, 
                \?: 0 times or 1 time, the character before it It is optional.
                    For example: x\?y
                        xy, y, ay
                \{m\}: m times, the character before it must appear m times.
                    For example: x\{2\}y
                        xy, xxy, y, xxxxy, xyy
                \{m,n\}: at least m times, at most n times
                    For example: x\{2,5\}y
                        xy, y, xxy
                \{m,\}: at least m times
                \{0,n\}: at most n times

                .*: Any character of any length

                    Work in greedy mode: match as many
            positional anchors as possible:
                ^: anchor at the beginning of the line;
                    write on the leftmost side of the pattern
                $: end of line anchor:
                    write on the far right side of the pattern
                ^$: blank line

                A string of consecutive characters that does not contain special characters is called a word:
                \<: beginning of the word, appearing on the left side of the word, \b
                    \<char
                \>: ending of the word, appearing on the right side of the word, \b
                    char\>
            grouping:
                \( \)
                    For example: \(ab\)*
                    The content matched by the pattern in the group can be memorized in memory by the regular expression engine and can be referenced later.

                Quote:
                    For example, \(ab\(x\)y\).*\(mn\)
                        has numbering: left bracket from left to back, and matching right bracket
                        \(a\(b\(c\)\)mn \(x\)\).*\1

                \#: refers to the content matched by the nth bracket, not the pattern itself.
                    For example:
                        \(ab\?c\).*\1

                            abcmnaaa
                            abcmnabc
                            abcmnac
                            acxyac

        Command options:
            -v: reverse selection
            -o: only display matching strings, not the lines where the strings are located
            -i: ignore-case, ignore the case of characters
            -E: support the use of extended regular expressions
            -A #print The last few lines of the match
            -B #Print the first few lines of the match
            -C #Print the first few lines of the match

 practise:

1. Copy the /etc/skel directory to /home/tuser1, requiring that other groups and other users of /home/tuser1 and its internal files do not have any access rights.

[root@dxlcentOS ~]# cp -a /etc/skel/ /home/tuser1
[root@dxlcentOS ~]# chmod -R go= /home/tuser1 递归修改权限,g:组的权限,o其他人权限,后面没有加权限,表示取消他们的权限

2. Edit the /etc/group file and add the group hadood.

[root@dxlcentOS ~]# vim /etc/group
hadoop:x:2050:
加入一行hadoop:x:2050:,指定组名和组ID,组ID不能和存在的ID号相同,要是没有指定组ID,则是无效组,不能往里面添加用户。

3. Manually edit the /etc/passwd file and add a new line to add user hadoop. Its basic group ID is the ID number of the hadoop group, and its home directory is /home/hadoop.

[root@dxlcentOS ~]# vim /etc/passwd
hadoop:x:2049:2050::/home/hadoop:/bin/bash
Hadoop用户增加进用户信息库后,还没有家目录,还要复制/etc/skel过来

4. Copy the /etc/skel directory to /home/haddoop, requiring that the owner group and other users of the /home/haddoop directory do not have any access rights.

[root@dxlcentOS ~]# cp -a /etc/skel /home/hadoop
[root@dxlcentOS ~]# chmod go= /home/hadoop

5. Modify the /home/haddoop directory and all the files inside it to have hadoop as owner and group as hadoop.

[root@dxlcentOS ~]# chown -R hadoop /home/hadoop | chgrp -R hadoop /home/hadoop

6. Display all lines starting with uppercase or lowercase S in the /proc/meminfo file, two ways.

[hadoop@dxlcentOS ~]$ grep -i “^s” /proc/meminfo
[hadoop@dxlcentOS ~]$ grep “^[Ss]” /proc/meminfo

7. Display the default shell in the /etc/passwd file for users other than /sbin/nologin.

[hadoop@dxlcentOS ~]$ grep -v “/sbin/nologin” /etc/passwd

8. Display users whose default shell is /bin/bash in the /etc/passwd file.

[root@dxlcentOS ~]# grep “/bin/bash” /etc/passwd
root:x:0:0:root:/root:/bin/bash
centos:x:1000:1000::/home/centos:/bin/bash

9. Find the one-digit or two-digit number in the /etc/passwd file

[root@dxlcentOS ~]# grep “\<[0-9]\{1,2\}\>” /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
…….

10. Display lines starting with at least one blank character in the /boot/grub/grup.conf file.

[root@dxl ~ 12:33:45]# grep “^[[:space:]]\+” /boot/grub/grub.conf

11. Display the lines in the /etc/rc.d/rc.sysinit file that begin with #, followed by at least one blank character, and followed by at least one non-blank character.

[root@dxl ~ 12:46:27]# grep "^[#][[:space:]]\+[^[:space:]]\+" /boot/grub/grub.conf
# grub.conf generated by anaconda
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda3
#          initrd /initrd-[generic-]version.img

12. Find the lines ending with 'LISTEN', followed by a blank character, in the results of the "netstat -tan" command.

[root@dxl ~ 12:59:51]# netstat -tan | grep "LISTEN[[:space:]]\+$"
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN  

13. Add users bash, testbash, basher and nologin (their shell is /sbin/nologin); then find the user information whose user name is the same as the default shell on the current system.

The first step is to add users bash, testbash, basher and nologin.

Step 2

[root@dxl ~ 13:24:53]# grep  -E "^([^:]+\>).*\1$" /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:504:504::/home/bash:/bin/bash
nologin:x:507:507::/home/nologin:/sbin/nologin

Summary: The ID of a group added by editing the group information base file /etc/grouptonggu cannot be the same as the existing group ID.

After you add a user by manually editing the /etc/passwd file of the user information base and specify the home directory path, it does not take effect immediately. You also need to copy the /etc/skel directory to /home/hadoop, and modify its owner and group to hadoop. .

Guess you like

Origin blog.csdn.net/weixin_44657888/article/details/129299840