Leaderboard ad


Standard Wildcards / Globbing Patterns in Linux

Standard Wildcards (globbing patterns) are using by nearly all command-line utilities.  Here we will explore some of the most widely used wildcards and give some examples of how to use them.

Star  / Asterisk *

The star wildcard or asterisk can represent any number of characters, to be more precise it can represent zero or more characters.

"st*" will match anything starting with "st", including "st" alone.  "*.txt" will match any file ending in the ".txt" file extension, and "*test*" will match any file with test in it's name.

List all files starting with "test":
[savona@putor tmp]$ ls test*
testnopreserve.txt  testpreserve.txt

List all files with "test" in their name:
[savona@putor tmp]$ ls *test*
savonatest.txt  testnopreserve.txt  testpreserve.txt

List all files ending in ".txt" file extension:
[savona@putor tmp]$ ls *.txt
hammer.txt  savonatest.txt  testnopreserve.txt  testpreserve.txt

Question Mark ?

The question mark can represent any single character. This is not limited to alphanumeric characters.

List all files that have a single character before "ammer.txt".
[savona@putor tmp]$ ls ?ammer.txt
hammer.txt  Yammer.txt

Here is an example using a non-alphanumeric character.
[savona@putor tmp]$ ls ?k.txt
%k.txt  ok.txt

Square Brackets []

Square brackets allow you to specify a range and characters or numbers.  You can use [a-z] which would mean all lower case letters as well as [0-9] which would mean all digits.

List all files that have ANY (because we are using the range "a" though "z") lowercase letter, followed by "k.txt":
[savona@putor tmp]$ ls [a-z]k.txt
ok.txt  zk.txt

Now we narrow the range to "a" though "p" and only receive one result:
[savona@putor tmp]$ ls [a-p]k.txt

The same can be done using a range of numbers.

List all files that are file2.txt through file 4.txt:
[savona@putor tmp]$ ls file[2-4].txt
file2.txt  file3.txt  file4.txt

Same thing, this time include number 1:
[savona@putor tmp]$ ls file[1-4].txt
file1.txt  file2.txt  file3.txt  file4.txt

Curly Brackets {}

Curly brackets are used for multiple matches.  Each string can be an exact name, or a wildcard. It will find anything that matches any of the given strings using an or relationship (one OR the other).

For example, if I had a directory with a lot of txt and doc files, I could copy them like so:

[savona@putor tmp]$ cp -v {*.doc,*.txt} /tmp
‘check.doc’ -> ‘/tmp/check.doc’
‘savona.doc’ -> ‘/tmp/savona.doc’
‘wildcard.doc’ -> ‘/tmp/wildcard.doc’
‘file1.txt’ -> ‘/tmp/file1.txt’
‘file2.txt’ -> ‘/tmp/file2.txt’
‘file3.txt’ -> ‘/tmp/file3.txt’
‘file4.txt’ -> ‘/tmp/file4.txt’
‘file5.txt’ -> ‘/tmp/file5.txt’
‘hammer.txt’ -> ‘/tmp/hammer.txt’
‘%k.txt’ -> ‘/tmp/%k.txt’
‘ok.txt’ -> ‘/tmp/ok.txt’
‘savonatest.txt’ -> ‘/tmp/savonatest.txt’
‘testnopreserve.txt’ -> ‘/tmp/testnopreserve.txt’
‘testpreserve.txt’ -> ‘/tmp/testpreserve.txt’
‘Yammer.txt’ -> ‘/tmp/Yammer.txt’
‘zk.txt’ -> ‘/tmp/zk.txt’

NOTE: You can use any of the other wildcards inside of the curly brackets.

Logical NOT [!]

This wildcard is used to NOT match something, or to exclude.  Anything inside the square brackets that is following an exclamation point will be excluded in any matches.

Remove all files named file*.txt except file2.txt:

[savona@putor tmp]$ rm -v file[!2].txt
removed ‘file1.txt’
removed ‘file3.txt’
removed ‘file4.txt’
removed ‘file5.txt’

Backslash \

A backslash is used to escape (or to make literal) a special character.  For example, what if you wanted to match a literal asterisks? The shell would see that as a wildcard as explained above.

EXAMPLE: Here we are searching for a file name with an asterisks in it (This is not recommended, just using it as an example).  But it returns other filenames because it interprets the asterisk as a wildcard.

[savona@putor tmp]$ ls notawild*.txt
notawildcard.txt  notawildone.txt  notawild*.txt

If we escape it using the backslash, the shell interprets the asterisks and a literal asterisks and only returns the one file.

[savona@putor tmp]$ ls notawild\*.txt

SIDE NOTE: You can also use single quotes to tell the shell not to expand wildcards, like so:

[savona@putor tmp]$ ls 'notawild*.txt'

No comments:

Post a Comment