Linux based regular expression, user, Group Management command Introduction _linux

Source: Internet
Author: User
Tags echo command posix readable stdin

wildcard character (Globbing)

Wildcard characters are similar to metacharacters, and wildcard characters are mainly used for matching filenames, while metacharacters are mainly used for string matching;
Here are a few common wildcard characters:

* represents any character that matches any number of digits
? Represents a match to any character
^ Expression takes counter, does not contain meaning
[] represents any one of the characters within this interval
{} represents a collection
\ escape character, so that the character with special meaning loses its original meaning
| Represents ' or ', matching a set of optional characters

Metacharacters

Metacharacters is a special character used to describe a character.
the common metacharacters and meanings are as follows:

*      repeat the previous character 0 or more times
.      Match any character once
\+     match the preceding character 1 or more times
\?     Match the preceding character 0 times or 1 times
\{m\} matches the preceding character    m times
\{m,n\}   matches the preceding character at least m times, at most n times
^      match character at the beginning of
$      Matching characters match blank lines at the end of a     line ^$. Spaces, 0 do not count
\<     matching characters in the first
\>     matching characters in the final
\<string\> exact match string
\ (xy\)   XY represents a grouped
\1     pattern from the left, the first opening parenthesis and the pattern matching the closing parenthesis to match the character

In addition to the above commonly used meta characters, there are some special metacharacters:

[: Alpha:]  All uppercase and lowercase letters [:
Upper:] All  capital letters 
[: Lower:] All  small Letters
[: alnum:] All  letters and numbers
[:p UNCT:]  all punctuation
[: Blank:]  blank key and TAB key
[: space:]  any blank character, space, tab, CR, etc.
[:d igit:]  any number, 0-9
[:p rint :]  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 matching content to highlight the processing-i,--ignore-case ignore C ASE distinctions in both the pattern and the input files. (i-specified by POSIX.)
     Ignores character case matching-v,--invert-match invert the sense of matching, to select non-matching lines. (-V is specified by POSIX.) Show rows that do not match-o,--only-matching Print only the matched (non-empty) parts of a matching lines, with each such part On a separate output lines. Shows only the matching part of the-q,--quiet,--silent silent mode, and does not enumerate any content-w,--word-regexp the line of the complete match of the word-D,--directories=a Ction to handle directories; ACTION is ' read ', ' recurse ', or ' skip ', directory representation: Read Only, recursive, skip-r,-r,--recursive like--directories=recurse-c,--count p Rint only a count of matching lines the number of files matched to file-b,--before-context=num print NUM lines of leading the context list matching to the previous NUM Line-a,--after-context=num Print NUM lines of trailing context lists the following lines that match to the post NUM line-c,--context=num print NUM lines The output context list 

Cut-and

Print selected parts of lines from every FILE to standard output enumerates the selected portions of each row to the standard output, that is, a field in the Fetch row
"Synopsis" Cut
OPTION ... [FILE]
... "OPTION"
  -b,--bytes=list   Select only these bytes are delimited by byte
  -c,--characters=list  Select only Characters by character
  -d,--delimiter=delim  use Delim instead the tab for field Delimiter  tab replaces the specified separator to subregion   after the-f,--field=list is divided, the data is listed according to the number of regions-
  n with-b: Don ' t split multibyte characters do not separate multibyte characters
' for EXAMPLE '
  [root@localhost ~]# cat/etc/passwd|cut-d:-f1
  root
  bin
  daemon
  ADM
  LP
  extraction/etc/ The first field contents of the passwd file, which is the user name
  [root@localhost ~]# cat/tmp/ah2.txt |cut-nb 1,2,3
  trivial
  [root@localhost ~]# Cat /tmp/ah2.txt |cut-nb 1 undivided byte
  flat
  [root@localhost ~]# cat/tmp/ah2.txt |cut-b 1 Chinese characters belong to multibyte characters

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

Sort

Sort lines of the line of text files in "synopsis" sort [OPTION] ... [FILE] ... sort [option] ...--files0-from=f "option"-b,--ignore-leading-blanks-f,--ignore-case fold lower case t o Upper case characters ignore capitalization-i,--ignore-nonprinting consider only printable ignore blank characters Press Three-digit month sort-h,--human-numeric-sort Compare human readable numbers use a human-readable unit sort-g,--genaral-numeric-sort use universal values
    Sort, support scientific count-t,--field-separator=sep use SEP instead of Non-blank to blank transition Specify the separator-k,--key=keydef Sort via a key; Keydef gives location and type to specify column sorting,-n,--numeric-sort compare according to string numerical value based on number of strings Value sort-r,--reverse reverse order-c,--check check from Srot Input;don ' t sort sort check but not sort-o,--output=file write result t o file instead of standard output saves the results to a file without outputting the-u,--unique with-c,check for strict ordering; Without-c,output only the one equal run with-C performs a rigorous sequential check; the first result is output only with the-C combination, the overlapping rows are eliminated, and the overlapping and adjacent lines cannot be removed. 

Uniq

The omit repeated lines records or excludes duplicate rows
"Synopsis"
Uniq [OPTION] ... [INPUT [OUTPUT]]
"OPTION"
  -c,--count prefix lines by the number of occurrences row count (repeat row, previous recurrence)
  -d,--repeated only  print Duplicate lines, one for each group prints only duplicate rows
  -d,--all-repeated[=method]
  -f,--skip-fields=n skips the
    first N fields
  -s,--skip-chars=n
    skips the first few characters
  -i,--ignore-case  ignores case
  -u,--unique only   print unique Lines print only-z,--zero-terminated end lines with  0 bytes,not newline-w,--check-chars=n
  No More than n characters in lines
    no match after nth characters

Practice

1, list the current system of all logged-in users of the user name, the same user login multiple times, only show once

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

2, remove the last login to the current system of the user information related

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

3, remove the current system on the user as its default shell of the most of the shell

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

4, the value of the third field in the/etc/passwd of the largest 10 users of the information all capitalized after the save to the/tmp/maxusers.txt

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

5, remove 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 convert their names to uppercase and save them to the/tmp/etc.conf file

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

7. Display the total number of subdirectories or files in the next level of/var directory

There are no direct commands in Linux to show the number of files in a directory, and you can combine two commands by piping
View the number of files in the directory command as follows:
LS |wc-l
1. LS is the command to view all files within a folder
2, Wc-l is the number of statistics file lines of command
3, above two command overlay, equal to the total number of files under the statistics folder

#!/bin/sh
find/tmp/homework-maxdepth 1-type d | while read dir; does 
count=$ (find "$dir"-type F | wc-l)
ECH O "$dir: $count" Done

8, remove the third field in the/etc/group file the smallest number of the 10 groups of names

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

9, the contents of/etc/fstab and/etc/issue files are merged into the same content and saved to/tmp/etc.test

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

Linux Basics-User, group management

To improve its security, Linux restricts access to different files by creating users and groups of users and assigning appropriate permissions. Below we will study the user and the user group's related management command first to look at several files:

/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 file content format is a string separated by ': ', which 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 the/etc/passwd format, but the content is different:

Username:encrypted passwd:date of last change:minimum PASSWD age:maximum PASSWD age:passwd Warning:password INACTIVITY:E Xpiration

This simple explanation is:

USERNAME: That
is, username ENCRYPTED PASSWD: encrypted password date of
change: The last time the password was modified (days, in days from 1970-1-1)
MINIMUM PASSWD: The shortest possible password date (days, relative to the last modified date of the third column)
MAXIMUM PASSWD: Maximum modifiable password date
PASSWD WARNING: Number of days before password expiration (relative to the number of days before the four column)
PASSWD Inactivity: The idle date of the password (there will be a certain number of days after the password expires)
Expiration: Notes

Uid

People interact with the computer, people can recognize characters and numbers, but the computer can only recognize binary code 0, 1, so the UID is the ID number assigned to the corresponding computer in the username, and the UID can be categorized into the following categories:

0: System administrator, root UID
1-999: System users, these users are users of the service class program in the background of the system; Users can also specify user 1000-2^32-1 for system users
: ordinary Users

Gid

In the process of programming, we will share the data between some users, rather than let the data for outsiders to see, so there is the concept of user groups, users through the GID to determine the scope of the user group

Let's introduce the following commands to set up the above and related information:

Useradd

Create a new user or update default new user information
"Synopsis"
  useradd [options] LOGIN
  useradd-d
  use radd-d [Options]
"Options"
  D-useradd commands partial default parameters, can modify
  -c,--comment    Add User's description information, finger Username view more intuitive
  -d,--home-dir    Specifies the user's home directory, must be an absolute path
  -e,--expiredate   user account expiration date, format default: Yyyy-mm-dd
  -f,--inactive    Specifies whether the user password is invalid (-1: Never expires, expires will force the password change; 0: Immediate failure)
  -g,--gid      User Main group, the group must be available
  -g,--groups     users to attach the group, The genus group can be set multiple-
  m to         Force no home directory-
  m to force the         home directory
  -r,create  System user to create systems users
  -o,--non-unique   a user (duplicate UID) that is allowed to create an existing UID
  -s,--shell     Specify the user's shell environment
  -u,--uid the      uid value of the specified user

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 combine the   pipe character to assign the standard input directly to the user password-
  l ,--lock  locks the specified user
  -u,--unlock   unlock the specified user
  -d,--delete   quickly deletes the user password
  -e,--expire   Invalid password  minimum number of days to modify-n,--minimum password-x,--maximum The maximum number of days to
  modify a  password
  -w,--waring   the number of days before the password expires
  -i,--  idle days after inactive password expires
  -s,--status   user's password status

Groupadd

Create a new group
"Synopsis"
  Groupadd [options] group
"option"
  -g,--gid    creates a group that specifies the GID
  -k,--key    Modify the/etc/login.defs value to refer to the contents of this file
  -o,--non-unique   Create a group of existing GID
  -p,--password  Create a group password and now basically do not use
  -r,--system-group to  create a system group

Newgrp

Log in to a new group
"Synopsis"
  newgrp [-] [group]
This command is only useful when the user's primary group is replaced, for example, username has a primary group Group1, a subordinate group Group2, Group3; now toggle user's primary group to GROUP3 Use this command

Summarize how user and group management commands are used and complete the exercise

Create group Distro with GID 2016; [root@localhost ~]# groupadd-g 2016 distro [root@localhost ~]# tail-1/etc/group distro:x:2016: Chuang Build User Mandriva, whose ID is 1005; basic group bit distro [root@localhost ~]# useradd-g distro-u 1005 Mandriva [root@localhost ~]# tail-1-/etc/p ASSWD mandriva:x:1005:2016::/home/mandriva:/bin/bash Create user Mageia with ID bit 1100; home directory for/home/linux; [Root@localhost ~]# Useradd-u 1100-d/home/linux Mageia [root@localhost ~]# tail-1/etc/passwd Mageia:x:1100:1100::/home/linux:/bin/bash to
User mageia Add password, password is mageedu [root@localhost ~]# passwd mageia changing for user password.

New Password:retype New Password:passwd:all authentication tokens updated successfully. Delete User Mandriva but keep their home directory; [root@localhost ~]# tail-3/etc/passwd mariadb:x:1000:1000::/home/mariadb:/sbin/nologin Hadoop: X:1001:1001::/home/hadoop:/bin/bash Mageia:x:1100:1100::/home/linux:/bin/bash [root@localhost ~]# Ls/home/hadoop The Linux Mandriva creates a user Slackware with an ID of 2002, a base group of distro, and a subordinate group of Peguin; [root@localhost ~]# groupadd Peguin [root@localhost useradd-u 2002-g Distro-gpeguin slackware [root@localhost ~]# tail-2/etc/passwd ~]# mageia:x:1100:1100::/h Ome/linux:/bin/bash Slackware:x:2002:2016::/home/slackware:/bin/bash [root@localhost ~]# tail-2/etc/group mageia:x : 1100:peguin:x:2017:slackware modifies Slackware's default shell to/bin/tcsh; [root@localhost ~]# usermod-s/bin/tcsh] Slackware [ Root@localhost ~]# tail-2/etc/passwd mageia:x:1100:1100::/home/linux:/bin/bash slackware:x:2002:2016::/home/ SLACKWARE:/BIN/TCSH for user Slackware New sub Group Admins [root@localhost ~]# usermod-ag Admins slackware [root@localhost ~]# tail -4/etc/group distro:x:2016:mageia:x:1100:peguin:x:2017:slackware Admins:x:2018:slackware Adds a password for Slackware, 
The minimum password requirement is 3 days, the maximum is 180 days, warning is 3 days; [root@localhost ~] #passwd-N 3-x 180-w 3 Slackware [root@localhost ~]# tail-1/etc/shadow slackware:!! : 16658:3:180:3::: [root@localhost ~] #echo ' Slackware ' |passwd--stdin Slackware Add user OpenStack, its ID number is 3003, basic group is clouds, The additional group is Peguin and Nova; [Root@localhost ~]# groupadd clouds [rooT@localhost ~]# groupadd Nova [root@localhost ~]# useradd openstack-u 3003-g clouds-g Peguin,nova [root@localhost ~]# TAIL-1/etc/passwd openstack:x:3003:2019::/home/openstack:/bin/bash Add System user MySQL, request its shell for/sbin/nologin; Root@localhost ~]# useradd-r mysql-s/sbin/nologin [root@localhost ~]# tail-1/etc/passwd-Mysql:x:996:994::/home/mysql: /sbin/nologin uses the echo command to add a password for Openstack [root@localhost ~]# echo ' Openstack ' |passwd--stdin to Openstack the changing
Password for user OpenStack. Passwd:all authentication tokens updated successfully.

Above is the Linux foundation of the regular expression, user, Group Management command Introduction, if you have any suggestions can leave a message

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.