Regular expressions based on Linux introduction to user and group management commands

  • 2020-05-12 06:36:08
  • OfStack

Wildcard (Globbing)

Wildcards are similar to metacharacters in that they are mainly used to match file names and metacharacters are mainly used to match strings.
Here are some common wildcards:

* represents any character that matches any number of digits
The & # 63; Means match 1 arbitrary character
^ is for invert, does not contain
[] represents any 1 character in this interval
{} represents one set
\ escape character to make a character with special meaning lose its original meaning
| means' or 'and matches an optional set of characters

metacharacters

Metacharacters are special characters used to describe characters.
Common metacharacters and their meanings are as follows:


*       Repeat the previous character 0 Times or times 
.       Match any character 1 time 
\+      Matches the preceding character 1 Times or times 
\?      Matches the preceding character 0 Time or 1 time 
\{m\}     Matches the character in front of it m time 
\{m . n\}    Matches the preceding character at least m At most time, n time 
^       Matches the character at the beginning of the line 
$       Matches the character at the end of the line 
^$      Matches a blank line. Spaces, 0 not 
\<      Match the character at the beginning of the word 
\>      The matching character is at the end of the word 
\<string\>  Precise matching string
\(xy\)   xy said 1 A grouping 
\1      The pattern starts on the left, and the 1 The character matched by the pattern between the left parenthesis and the matching right parenthesis 

In addition to the common metacharacters above, there is a special metacharacter:


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 

grep


grep, egrep, fgrep - print lines matching a pattern
 【 SYNOPSIS 】 
  grep [OPTIONS] PATTERN [FILE...]
  grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
 【 OPTIONS 】 
  --color=auto   The matched content is highlighted 
  -i . --ignore-case
     Ignore case distinctions in both the PATTERN and the input
     files. (-i is specified by POSIX.) Ignore character case matching 
  -v . --invert-match
     Invert the sense of matching, to select non-matching lines.
     (-v is specified by POSIX.) Displays rows that did not match 
  -o . --only-matching
     Print only the matched (non-empty) parts of a matching line,
     with each such part on a separate output line. Show only the parts that match 
  -q . --quiet,--silent Silent mode, not enumerating anything 
  -w . --word-regexp   The word matches the line exactly 
  -d .  --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip' , directory representation: read-only, recursive, skip 
  -r . -r, --recursive      like --directories=recurse
  -c . --count print only a count of matching lines per FILE How many lines does the file match 
  -B . --before-context=NUM print NUM lines of leading context Lists the matches before NUM line 
  -A . --after-context=NUM  print NUM lines of trailing context List the matches NUM line 
  -C . --context=NUM   print NUM lines of output context List the preceding and following lines that match 

cut


Print selected parts of lines from each FILE to standard output Enumerate the selected portion of each row to standard output, that is, extract a field from the row 
 【 SYNOPSIS 】 
cut OPTION... [FILE]...
 【 OPTION 】 
  -b . --bytes=LIST   select only these bytes Byte separated 
  -c . --characters=LIST  select only these characters Character separation 
  -d . --delimiter=DELIM  use DELIM instead of TAB for field delimiter   with TAB Replaces the specified delimiter to divide the region 
  -f . --field=LIST    After the division, the data is listed according to the region number 
  -n with -b: don't split multibyte characters Not separating multi-byte characters 
 【 FOR EXAMPLE 】 
  [root@localhost ~]# cat /etc/passwd|cut -d: -f1
  root
  bin
  daemon
  adm
  lp
   extract /etc/passwd File first 1 Is the user name 
  [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1,2,3
   The ordinary 
  [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1 Unsplit byte 
   flat 
  [root@localhost ~]# cat /tmp/ah2.txt |cut -b 1 Chinese characters are multi-byte characters 

  [root@localhost ~]# cat /tmp/ah2.txt |cut -c 1
   flat 

sort


sort lines of text files Line sorting of text files 
 【 SYNOPSIS 】 
 sort [OPTION]... [FILE]...
 sort [OPTION]... --files0-from=F
 【 OPTION 】 
  -b . --ignore-leading-blanks
  -f . --ignore-case
    fold lower case to upper case characters  Ignore case 
  -i . --ignore-nonprinting
    consider only printable characters Ignore the blank 
  -M . --month-sort
     In accordance with the 3 Digit month order 
  -h . --human-numeric-sort   compare human readable numbers
     Sort using human-readable units 
  -g . --genaral-numeric-sort
     Use general numeric sorting to support scientific counting 
  -t . --field-separator=SEP
    use SEP instead of non-blank to blank transition
     Specifies the separator for the columns 
  -k . --key=KEYDEF
    sort via a key;KEYDEF gives location and type
     Specify column sort, 
  -n . --numeric-sort
    compare according to string numerical value
     Sort by the values in the string 
  -r . --reverse
     Antitone array 
  -c . --check check from srot input;don't sort
     Sort check, but not sort 
  -o . --output=FILE  write result to FILE instead of standard output
     Saves the result to a file without printing it 
  -u . --unique   with -c,check for strict ordering; without -c,output only the first of an equal run
     with -c Combination, perform strict sequence check; Not with -c Combination, only the output 1 The adjacent repeated rows are eliminated, and the repeated and adjacent rows cannot be eliminated. 

uniq


report or omit repeated lines Record or eliminate duplicate rows 
 【 SYNOPSIS 】 
uniq [OPTION]... [INPUT [OUTPUT]]
 【 OPTION 】 
  -c . --count prefix lines by the number of occurrences Line count (repeating rows and rows 1 Okay, there's a number of repetitions in front.) 
  -d . --repeated  only print duplicate lines, one for each group Only duplicate lines are printed 
  -D . --all-repeated[=METHOD]
  -f . --skip-fields=N
     Skip the former N A field 
  -s . --skip-chars=N
     Skip the first few characters 
  -i . --ignore-case   Ignore case 
  -u . --unique   only print unique lines Print only lines that do not repeat 
  -z . --zero-terminated  end lines with 0 bytes,not newline
  -w . --check-chars=N compare no more than N characters in lines
     The first N Do not match after 10 characters 

practice

1. List the user names of all users who have logged in on the current system. The same user can log in for several times and only be displayed once

who |cut -d' ' -f 1|uniq

2. Take out the relevant information of the user who logged into the current system last

cat /etc/passwd|grep "^`last -1|cut -d' ' -f1|head -1`"

3. Remove the shell on the current system, which is the most frequently used shell by users as their default

cat /etc/passwd|cut -d: -f7|sort -u|sort -rn|head -1

4. Change all the information of the last 10 users with the largest value in the third field of /etc/passwd to uppercase and save it to /tmp/ maxusers.txt

cat /etc/passwd|sort -t':' -k3 -n|tail|tr [a-z] [A-Z] 2&>1 /tmp/maxusers.txt

5. Take out the IP address of the current host,

ifconfig eno16777736| grep  "\<inet\>"|cut -d' ' -f 10

6. List the file names of all files ending in.conf under /etc/ and save them to /tmp/ etc.conf after converting them to uppercase

ls /etc|grep -o ".*\.conf$"|tr [a-z] [A-Z] >> /tmp/etc.conf

7. Show the total number of level 1 subdirectories or files in the /var directory

There is no direct command in linux to display the number of files in a directory. You can pipe the two commands together
The command to view the number of all files in the directory is as follows:
ls |wc -l
1. ls is the command to view all the files in the folder
2. wc-l is a command to count the number of lines in a file
3. The superposition of the above two commands is equivalent to the total number of files in the statistics folder


#!/bin/sh
find /tmp/homework -maxdepth 1 -type d | while read dir; do 
count=$(find "$dir" -type f | wc -l)
echo "$dir : $count"
done

8. Fetch the names of the 10 groups with the smallest value in the third field in the /etc/group file

cat /etc/group|sort -t':' -k 3 -n|head |cut -d':' -f1

9. Merge the contents of /etc/fstab and /etc/issue files into the same content and save them to /tmp/ etc.test

cat /etc/fstab /etc/issue >>/tmp/etc.test

Linux basics - user and group management

To improve its security, Linux restricts access to different files by creating users and groups of users and giving them permissions. Let's take a look at a few files to learn about user and group management commands:

/etc/passwd


[root@localhost ~]# cat /etc/passwd|head -3
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

The format of the contents of the documents is by ':' delimited string, its content includes the following: USERNAME: X: UID: GID: DESCRIPTION: HOME: SHELL

/etc/shadow


[root@localhost ~]# cat /etc/shadow|head -3
root:$6$1RSlswAIVbmtwWM5$WLZxHXzkL7.PvHb2ghYqbGvj3Cz4AB5sGRr33TwCX1cwSqV.syv0q1eqcF3NGUJeNNspAOt5C1rWfdsgBllB1.::0:99999:7:::
bin:*:16659:0:99999:7:::
daemon:*:16659:0:99999:7:::

The content format is similar to /etc/passwd, but the content is different:


USERNAME:ENCRYPTED PASSWD:DATE OF LAST CHANGE:MINIMUM PASSWD AGE: MAXIMUM PASSWD AGE:PASSWD WARNING:PASSWORD INACTIVITY:EXPIRATION

Here's a simple explanation:


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
0

UID

People interact with computers and can recognize characters and Numbers, etc., but computers can only recognize binary codes like 0 and 1. Therefore, UID is the computer-recognized ID number assigned to USERNAME. UID can be divided into the following categories:


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
1

GID

In the process of program design, we will share the data among part 1 users, and do not want to let the data be seen by outsiders. Therefore, we have the concept of user group, and the scope of user group is determined through GID

Here are the commands to set the above and related information:

useradd


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
2

passwd


update user's authentication tokens   
 【 SYNOPSIS 】 
  passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w warndays] [-i inactivedays] [-S]
  [--stdin] [username]
 【 OPTIONS 】 
  --stdin    Combines the pipe character to assign the standard input directly to the user password 
  -l . --lock   Locking the specified user 
  -u . --unlock    Unlock the specified user 
  -d . --delete    Quickly delete the user password 
  -e . --expire    Invalid user password 
  -n . --minimum   Minimum password change days 
  -x . --maximum   The maximum number of days a password can be changed 
  -w . --waring    The number of days of notification before the password expires 
  -i . --inactive   The number of days after password expiration 
  -S . --status    The user's password status 

groupadd


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
4

newgrp


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
5

Summarize the usage of user and group management class commands and complete the exercise


[:alpha:]   All upper and lower case letters 
[:upper:]   All capital letters  
[:lower:]   All lowercase letters 
[:alnum:]   All letters and Numbers 
[:punct:]   All punctuation 
[:blank:]   Blank key and TAB key 
[:space:]   Any blank character, space, tab , CR Etc. 
[:digit:]   Any number, 0-9
[:print:]   Any character that can be printed 
6

The above is Linux basic regular expression, user, group management command introduction, if you have any Suggestions can leave a message


Related articles: