5.4 Meta character, Pattern matching
A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches.
Wildcards are commonly used in shell commands in Linux and other Unix-like operating systems.
Wildcard | Matches |
---|---|
* | Any number of characters including none |
? | Any single character |
[ijk] | A single character either i , j or k. This is a character class. |
[x-z] | A single character between x and z |
[!ijk] | A single character except i , j and k (not in C Shell) |
[!x-z] | A Single character except x to z (not in C Shell) |
{pat1, pat2, ........} | Pattern 1 , Pattern 2 etc. (not in Bourne Shell) |
Note - Wildcards * and ? don't match a dot(.) character in file name and a / in file path. Therefore these two characters . and / must be matched explicitly.
* - any number of character in any filename / search string
e.g. $ lpr * (here * means all files, and this command will print all the files)
e.g. $ ls a*.txt (all file starting with a and .txt as extension)
? - any single character
e.g. $cp draft?.doc (it will copy draft1.doc draft2.doc draftb.doc etc. )
Character Class - set of characters enclosed by rectangular bracket [ ]
- It matches the single character in the class.
$ls chap[1-4] - it matches chap1, chap2, chap3 and chap4
! is used to reverse the searching criteria in character class. (It is not available in C Shell)
e.g. $ls *.[!co] - matches all filenames with single character extension not .c and .o
$ls [!a-zA-Z]* - matches all filenames that don't start with an alphabet.
$cp /home/srm/{progs, docs, data}/* . - all files from the three sub-directories progrs, docs, data will be copied to current directory. (Note- it doesn't work in Bourne Shell)
Q. What will be performed by the following commands -
- ls *.c
- mv * ../bin
- cp ????? progs
- rm *.[!l][!o][!g]
- ls data[0-1][0-9]
- cp -r /home/srm/{c,java} .
Expansion
किसी भी command को execute करने से पहले shell द्वारा command में प्रयुक्त meta-characters को resolve कर expand किया जाता है इस प्रक्रिया को expansion कहा जाता है |
$echo hello world
echo एक shell कमांड है यह दिए गए text को प्रिंट करता है |
$echo *
यहाँ * एक meta-character है तथा यह सभी files और directory name को व्यक्त करता है |
Path-name Expansion
expansion को निम्न प्रकार use किया जा सकता है -

यहाँ *s से अर्थ है कि केवल वही नाम show होंगे जिनके अंत में अक्षर "s" आता हो |
Tilde expansion
~foo = यहाँ foo किसी user का नाम है | तथा ~foo उस user की home directory को व्यक्त करता है |
नीचे दिए गए commands को run करके देखें -
$echo ~ यह command current user की home directory को show करेगा |
$echo ~foo यह command foo नाम के user account की home directory को show करेगा |
Arithmetic Expansion
arithmetic expansion केवल और केवल integers को support करता है |
arithmetic expansion में निम्न फॉरमेट का उपयोग किया जाता है -
$((expression)) यहाँ expression अर्थ गणितीय व्यंजक से है |
Brace Expansion
Preamble - text before pattern
Postscript - text after pattern
Brace expansion में comma separated लिस्ट अथवा integers और single characters की range शामिल किया जा सकता है |
