Brother Bird's linux Private Vegetables Learning Notes bash Usage of Wildcard, Redirection, Pipeline, etc. in "Twenty-three"

Keywords: CentOS ssh Linux

  1. Wildcard:

    * Any character
     ?: Any single character
     []: Any of them
     [-]: Sequence of a paragraph
     [^]: Not one of them
    # Display file names beginning with cron
    
    [root@CentOS ~]# ll /etc/cron*
    
    # Display the filename of exactly five letters under / etc /.
    
    [root@CentOS ~]# ll /etc/?????
    
    # Display the file name with numbers under / etc /.
    
    [root@CentOS ~]# ll /etc/*[0-9]*
    
    # Display file names that do not start with lowercase letters at / etc / below
    
    [root@CentOS ~]# ll /etc/[^a-z]*
    
    # Copy files that are not lowercase letters at the beginning of / etc / to / tmp
    
    [root@CentOS ~]# cp -a /etc/[^a-z]* /tmp

    Special symbols in bash:

  2. redirect

    Standard input: code 0, using < or <
    Standard Output: Code 1, use > or >>.
    Standard error output: code 2, 2 > or 2 > after use
     > The original file will be overwritten
     >: Added to the source file
     1 >: Overwrite the specified file with the correct result
     1 >: Append the correct result to the specified document
     2>: Overwrite the error result to the specified file
     2>: Append the error result to the specified file
    <: Import by file
     < <: End keyword, end of input
    # Store stdout and stderr in different files
    
    [root@CentOS ~]# find /home -name .bashrc > list_right 2> list_error
    
    # Discard the error message and display the correct result on the screen
    
    [root@CentOS ~]# find /home -name .bashrc 2> /dev/null
    
    # Write all the results of the command to the list file (either of the following can be used)
    
    [root@CentOS ~]# find /home -name .bashrc > list 2>&1
    [root@CentOS ~]# find /home -name .bashrc &> list 
    [root@CentOS ~]# cat > catfile < ~/.bashrc
    [root@CentOS ~]# ll catfile ~/.bashrc
    -rw-r--r--. 1 root root 176 3month   9 09:24 catfile
    -rw-r--r--. 1 root root 176 9month  22 2004 /root/.bashrc
    
    # The two files are the same
    
    # "... "Designate an end sign:
    
    [root@CentOS ~]# cat > catfile << "eof"
    > this is a test
    > OK noe stop
    > eof
    [root@CentOS ~]# 
  3. tee: Two-way redirection
    Flow to files and screen output
    Parameters:
    - a: Adding data to files in a cumulative manner
    last: This command is used to list information about users who have logged on to the system in the past.

    # Output last to test.list:
    [root@CentOS ~]# last | tee last.list |cut -d " " -f1
    # Save ls data in ~/ home file, and the screen also has output information.
    [root@CentOS ~]# last | tee ~/.homefile | more
    # The files in the back street of tee will be overwritten, and - a will add up the information.
    [root@CentOS ~]# last | tee -a ~/.homefile | more
  4. && command execution criteria:
    CMD1 & & cmd2: and relationship, one false is false, the front is correct and the back is executed
    cmd1 || cmd2: or relationship, true, before execution after the previous error
    cmd1 & & cmd2 | | cmd3: cmd1 correctly executes cmd2, cmd1 incorrectly executes cmd3
  5. cut select command
    Parameters:
    - d: Subsequent delimiter for segmentation
    - f: According to subparagraph - d, which paragraph should be selected?
    - c: Remove fixed character intervals in units of characters

    # Show paragraph 5 separated by:
    
    [root@CentOS ~]# echo $PATH | cut -d ':' -f 5
    
    # Separated by: showing 3 and 5 segments
    
    [root@CentOS ~]# echo $PATH | cut -d ':' -f 3,5
    
    # Delete the export information after the 12th character
    
    [root@CentOS ~]# export
    declare -x CVS_RSH="ssh"
    declare -x USER="kevin"
    declare -x USERNAME="kevin"
    declare -x WINDOWID="52428804"
    declare -x WINDOWPATH="1"
    [root@CentOS ~]# export | cut-c 12-
    CVS_RSH="ssh"
    USER="kevin"
    USERNAME="kevin"
    WINDOWID="52428804"
    WINDOWPATH="1"
  6. grep: Select commands
    Parameters:
    - a: Find data in the form of text files from binary files
    - c: Calculate the number of times a string is found
    - i: Ignore case and case
    - n: By the way, output line number
    - v: Reverse selection
    - color=auto: Display the color of the keywords you will find

    # Display the line where root appears in last
    
    [root@CentOS ~]# last | grep 'root'
    
    # Display the part of last that does not appear root
    
    [root@CentOS ~]# last | grep -v 'root'
    
    # Take out the last output as long as there is root and display a column
    
    [root@CentOS ~]# last | grep 'root' | cut -d ' ' -f1
    
    # Take out the lines containing MANPATH in / etc/man.config
    
    grep --color=auto 'MANPATH' /etc/man.config
  7. Sort, uniq, wc: sort command
    last: This command is used to list information about users who are currently logged in to the system in the past
    1 . sort

    # Use last to take the output data only to account number and sort them
    
    [root@CentOS ~]# last | cut -d ' ' -f1 | sort

    2. uniq: Used to delete duplicate rows and display only one
    Parameters:
    - i: Ignore case and case
    - c: Counting

    # Use last to list accounts, take only account columns, sort them and then take only one.
    
    [root@CentOS ~]# last | cut -d ' ' -f1 | sort | uniq
    
    # Use last to list the accounts, only take the account columns, sort out only one, and want to know how many times each person logs in.
    
    [root@CentOS ~]# last | cut -d ' ' -f1 | sort | uniq -c

    3. wc: the overall data for calculating output information
    Parameters:
    - l: List only rows
    - w: Number of words only
    - m: List only the number of characters

    # Statistics / etc/man.config exactly how many words, lines, characters there are
    
    [root@CentOS ~]# cat/etc/man.config | wc
    
    # The total number of people logged in this month
    
    [root@CentOS ~]# ;last | grep [a-zA-Z] | grep -v 'wtmp' | wc -l
    
    # You should know how many accounts are in the account document:
    
    [root@CentOS ~]# cat /etc/passwd | wc -l
  8. Character conversion
    1. tr: text substitution
    Parameters:
    - d: Delete one of the strings
    - s: Replace strings

    # Delete:from/erc/passwd file
    
    [root@CentOS ~]# cat /etc/passwd | tr -d ' ' 
    
    # Delete line breaks in DOS files (^ M can be replaced by r)
    
    [root@CentOS ~]# cat /root/passwd | tr -d '\r' > /root/passwd/linux

    2. col: tab key to space
    Parameters:
    - x: Turn the tab key into a space key

    #Turn tab into a space key
    
    [root@CentOS ~]# cat /etc/man.config | col -x cat -A | more

    3. join: Processing data between two files
    Parameters:
    - t: Default spaces as delimiters and compare the first field
    - i: Ignore case and case
    - 1: Which field is used to analyze the first file?
    - 2: Which field does the second file use to analyze?

    # Integrate / etc/passwd and / etc/shadow data as root
    
    [root@CentOS ~]# head -n 3 /etc/passwd /etc/shadow
    [root@CentOS ~]# join -t ':' /etc/passwd /etc/shadow
    
    # Integrate the GID of the fourth field of / etc/passwd and the third field of / etc / group, and integrate the two files
    
    [root@CentOS ~]# head -n 3 /etc/passwd /etc/group
    [root@CentOS ~]# join -t ':' -1 4 /etc/passwd -2 3 /etc/group

    4. paste: paste the two lines together
    Parameters:
    - d: Subsequent separator, default tab key
    - If filename is written as -, it represents standard input

    # Paste the same line of / etc/passwd and / etc/shadow together
    
    [root@CentOS ~]# paste /etc/passwd /etc/shadow
    
    # Read / etc/group first. Then paste it with the above question and take out only the third line.
    
    [root@CentOS ~]#  cat /etc/group | paste /etc/passwd /etc/shadow - | head -n 3

    5. expand: turn the tab key into a space
    Parameters:
    - t: Following the number, a tab key represents eight spaces. We can also customize how many spaces a tab key represents.

    # Take out the words of / etc/man.config with the first three lines of MANPATH
    
    [root@CentOS ~]# grep '^MANPATH' /etc/man.config | head -n 3
    
    # Continue with the above question and list all the symbols.
    
    [root@CentOS ~]# grep '^MANPATH' /etc/man.config | head -n 3 | cat -A
    
    # To follow the previous question, set the tab key to 6 characters
    
    [root@CentOS ~]# grep '^MANPATH' /etc/man.config | head -n 3 | expand -t 6 - | cat -A
  9. split: Cut command
    Parameters:
    - b: Cut file size
    - l: Cut in rows

    # My / etc/passswd has more than 700 KB, if you want to split it into 300 KB files
    
    [root@CentOS ~]# split -b 300k /etc/termcap termcap
    
    # Merge three files into one file
    
    [root@CentOS ~]#  cat termcap* >> termcap
    
    # Use ls-al to output information and record a file every ten lines
    
    [root@CentOS ~]# ls -al / | split -l 10 - lsroot
    [root@CentOS ~]# wc -l lsroot
  10. xargs: parameter substitution
    Parameters:

Posted by evaoparah on Sun, 14 Apr 2019 10:24:31 -0700