Filename Metacharactersby Pigbrain

Metacharacters

MetacharacterMeaning
*Match any string of zero or more characters
?Match any single character
[abc...]Match any one of the enclosed characters; a hyphen can specify a range (e.g., a-z, A-Z, 0–9)
[!abc...]Match any character not enclosed as above
~Home directory of the current user.
~nameHome directory of user name
~+Current working directory ($PWD)
~-Previous working directory ($OLDPWD)

Example

ls
-----------------------------
  	a  apple  b  banana  c  case  d  duck  

prompt> ls a*
a  apple

prompt> ls a????
apple

prompt> ls [a-c]  
a  b  c  

prompt> ls [!a-c]  
d  


extglob (extended pattern matching)

  • extglob을 활성화 시키기 위해서는 다음 명령어 실행
    • shopt -s extglob
  • │를 이용하여 여러개의 패턴을 나열할 수 있다


optionMeaning
?(pattern)Match zero or one instance of pattern
*(pattern)Match zero or more instances of pattern
+(pattern)Match one or more instances of pattern
@(pattern)Match exactly one instance of pattern
!(pattern)Match any strings that don’t match pattern

Example

prompt> shopt -s extglob  

ls
-----------------------------
  	te11te  te12te  te1te  te21te  te22te  te2te

prompt> ls te?(1|2)te
te1te  te2te

prompt> ls te*(1|2)te
te11te  te12te  te21te  te22te

prompt> ls te+(1|2)te
te11te  te12te  te1te  te21te  te22te  te2te

prompt> ls te@(1|2)te
te1te  te2te



ls
-----------------------------
  	a  apple  b  banana  c  case  d  duck  

prompt> ls !(apple|b)
a  banana  c  case  d  duck  touch

Class

ClassCharacters matched
alnum Alphanumeric characters
alpha Alphabetic characters
blank Space or Tab
cntrl Control characters
digit Decimal digits
graph Nonspace characters
lower Lowercase characters
print Printable characters
punct Punctuation characters
space Whitespace characters
upper Uppercase characters
word [[:word:]] is the same as [[:alnum:]_] (not in POSIX)
xdigit Hexadecimal digits


Example

ls
-----------------------------
a  apple  b  banana  c  case  d  duck

prompt> ls [[:alnum:]]pple
apple


참고

Published 14 March 2016