Linux Command: Wildcards [Globbing]

Many of the Linux commands support patterns for file or directory names and also or some other things. Commands like ls, mv, rm, and cp support patterns wildcard characters.

Then patterns are created using special characters, which are called Wildcards. This mechanism of providing a pattern to a command is also known as Globbing.

Using the wildcards we can construct very complex patterns for filename or directory name patterns.

In this article, we are discussing the special characters used for the patterns.

Wildcards

Here are the wildcard characters-

WildcardDescription
*Any characters any number of times
?Any character single-time. Exactly one character.
[char_list]Any character from the char_list
[!char_list]Any character that is not in the char_list
[[:char_class:]]Any character belongs to :char_class:

Character Classes

Here are the classes that we can use as character classes-

Character ClassDescription
[[:alnum:]]Alphanumeric characters (letter or number)
[[:alpha:]]Alphabetic character only(any case upper or lower)
[[:digit:]]Digit only
[[:lower:]]Lowercase character only
[[:upper:]]Upper case character only

Examples

Let’s take a look at a few examples of using patterns, and how those patterns reflect-

CommandResult(example)
ls *.ymlabc.yml
docker-compose.yml
test.yml
ls *.logerror.log
apache.log
mail.log
ls error*.logerror_1.log
error_3.log
error_backup.log
ls file?file1
fileA
file2
ls error?.logerror1.log
error2.log
ls error[127].logerror1.log
error2.log
error7.log
ls error[!127].logerrorA.log
error3.log
error5.log
ls error[[:alnum:]].logerror1.log
error2.log
errorA.log
errorb.log
ls error[[:digit:]].logerror1.log
error2.log
error7.log
ls error[[:upper:]].logerrorA.log
errorB.log
ls *[[:upper:]].logtestA.log
testB.log
abcD.log
aE.log
ls backup-??-20*.csvbackup-04-2024.csv
backup-02-2021.csv
backup-01-2000abc.csv
ls report_[[:digit:]]*.pdfreport_1dec.pdf
report_9123.pdf
ls image_[!abc]*.jpgimage_kabc.jpg
image_n1.jpg
ls file_[[:lower:][:digit:]]*.logfile_a3$a.log
ls *[![:digit:]].{jpg,png,gif}100a.jpg
abc.jpg
first.png
ls ????[!a-z]*.txt1234K.txt
AbCdKKK.txt
ls {doc,report}_??[[:upper:]]*[!xyz].{pdf,docx}doc_12BK.pdf
report_99A-3233.docx

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.