Linux learning - wildcards and special symbols

1. Wildcard

1.1 effect on documents

  • *: matches any 0 or more characters or strings, including empty strings
[root@VM-0-17-centos ~]# rm -f *.sh
  • ?: Match any 1 character
[root@VM-0-17-centos ~]# ls ?.*
a.out
[root@VM-0-17-centos ~]# ls ????.*  # Can be used multiple times
info.txt  test.sh

1.2 action on file / character

  • [abcd]: match any one character in brackets
[root@VM-0-17-centos ~]# ls [abcd].*
a.out
  • [a-z]: match any one character in a-z in brackets
[root@VM-0-17-centos ~]# ls [a-c].*
a.out
  • [! b-z]|[\^b-z]: does not match any one character in b-z in brackets
[root@VM-0-17-centos ~]# ls [!b-d].*
a.out
[root@VM-0-17-centos ~]# ls [^b-d].*
a.out

tips:

  • If [a-z] is used, it must be ensured that it is continuous. For example, [c-a] will report an error

2. Special characters

2.1 path and location related

  • ~: user home directory
[root@VM-0-17-centos ~]# cd ~
[root@VM-0-17-centos ~]# pwd
/root
  • -: last user's path
[root@VM-0-17-centos etc]# cd -
/root
[root@VM-0-17-centos ~]#   # After displaying, it will jump to the path of the last user
  • .: current directory
  • ..: upper level directory

tips:

  • echo $OLDPWD can return the last user's path, so cd - is actually cd $OLDPWD

2.2 quotation marks*

  • Single quotation mark '': strong reference. When outputting, the contents in quotation marks will be output as they are, and the commands in them will not be executed
[root@VM-0-17-centos ~]# echo '`date`'
`date`
  • Double quotation mark "": weak reference. If the contents of quotation marks contain commands (backquotation marks are required), variables, escape characters, etc., these contents will be parsed before output
[root@VM-0-17-centos ~]# echo "`date`"
Sat Feb 19 16:42:06 CST 2022
  • Backquote ` `: refers to a command, which is equivalent to $()
  • No quotation marks: when spaces are encountered, the whole will be segmented. It is best to use double quotation marks
[root@VM-0-17-centos ~]# a=psj ps2
-bash: ps2: command not found

2.3 other special characters

  • ;: Indicates the end of a command and is also a separator between commands
[root@VM-0-17-centos ~]# echo a;cat test.sh 
a  # echo a
#!/bin/bash  # cat test.sh
  • #: the comment content, which is also the command prompt of the root user, is used as a separator in vim:
:%s#i#j#g  # Replace all i's in the text with j's
  • $: indicates the contents of string variables. It is also a command prompt for ordinary users
  • \: escape character / escape character, which restores the character with special meaning and also acts as a newline character
  • |: pipe
  • {}: generate sequence, which can also be used as the separation between reference variables and ordinary characters
[root@VM-0-17-centos ~]# echo {1..5}  # Equivalent to seq 5
1 2 3 4 5
[root@VM-0-17-centos ~]# echo ${a}2
psj2
[root@VM-0-17-centos ~]# echo {1..a}  # There is no continuous sequence from 1 to a, so it is output as a string
{1..a}

tips:

  • The data stream (data content) transmitted by pipeline is not a file (name)
  • seq 1 1 5 indicates the start point and the end point of the step

2.4 special characters in Bash

  • &&: when connecting two commands, the former command is correct and the latter command will be executed. If the former error, the latter command will not be executed
[root@VM-0-17-centos ~]# echo a && cat test.sh 
a
#!/bin/bash
[root@VM-0-17-centos ~]# cho a && cat test.sh 
-bash: cho: command not found
  • ||: when connecting two commands, if the previous command is wrong, the latter command will be executed. If the previous command is correct, the latter command will not be executed
[root@VM-0-17-centos ~]# echo a  || cat test.sh 
a
[root@VM-0-17-centos ~]# cho a  || cat test.sh 
-bash: cho: command not found
#!/bin/bash
  • !: Indicates negation in bash; Mandatory in vim; Using it directly on the command line means finding a command that starts with a string and running it
[root@VM-0-17-centos ~]# !c
cho a  || cat test.sh   # Command line starting with c
-bash: cho: command not found  # Execute the command
#!/bin/bash

Keywords: Linux

Added by SteveMellor on Sun, 20 Feb 2022 08:34:20 +0200