Wildcards in Linux commands (with practical examples)
Wildcard in Linux is used for pattern matching. Globbing expands the wildcard pattern into a list of files and/or directories or paths – and they can be used for conjunction with most Linux commands. We will show here some most used examples….
* → Matches all
# This will list all txt files ls -l *.txt # This will list all files which begins with letter "a" ls -l a* # This will list all files which begins with letter * and ends with txt ls -l a*.txt
Examples…
[darko@kompjuteras]$ ls -l ### All files total 4 -rw-r--r--. 1 root root 0 Aug 20 20:48 amigo -rw-------. 1 root root 1713 Apr 22 14:46 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Aug 20 20:48 andaluzija.txt -rw-r--r--. 1 root root 0 Aug 20 20:48 random-file.txt [darko@kompjuteras]$ ls -l *.txt -rw-r--r--. 1 root root 0 Aug 20 20:48 andaluzija.txt -rw-r--r--. 1 root root 0 Aug 20 20:48 random-file.txt [darko@kompjuteras]$ ls -l a* -rw-r--r--. 1 root root 0 Aug 20 20:48 amigo -rw-------. 1 root root 1713 Apr 22 14:46 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Aug 20 20:48 andaluzija.txt [darko@kompjuteras]$ ls -l a*.txt -rw-r--r--. 1 root root 0 Aug 20 20:48 andaluzija.txt
! → exclude from search
# This will exclude all files who begins with a, b, c and A, B, C ls -l [!a-C]* # This will exclude all files who begins with a or Z ls -l [!aZ]*
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:32 Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l [!a-C]* -rw-r--r--. 1 root root 0 Aug 20 21:32 Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l [!aZ]* -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:32 Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me
? → matches the exact number(s) of characters (? for 1, ?? for 2, etc)
# This will list all files with one character before point and who ends with .txt ls -l ?.txt # This will list all files who have 3 characters on a name, but begin with a ls -l a?? # This will list all .txt files who have 3 characters before point, but starts with ab ls -l ab?.txt # This will list all files who have 2 characters only in name ls -l ??
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 20:56 abc -rw-r--r--. 1 root root 0 Aug 20 20:53 abc.txt -rw-r--r--. 1 root root 0 Aug 20 20:53 a.txt -rw-r--r--. 1 root root 0 Aug 20 20:53 gg -rw-r--r--. 1 root root 0 Aug 20 20:53 kjh.file [darko@kompjuteras]$ ls -l a?? -rw-r--r--. 1 root root 0 Aug 20 20:56 abc [darko@kompjuteras]$ ls -l ab?.txt -rw-r--r--. 1 root root 0 Aug 20 20:53 abc.txt [darko@kompjuteras]$ ls -l ?? -rw-r--r--. 1 root root 0 Aug 20 20:53 gg
[ ] → include any of the characters included between brackets. Matches exactly one character, relate to positions.
# This will list all files who begins with characters c or b ls -l [cb]* # This will list all files who begins with characters b or k ls -l [bk]* # This will list all files who begins with ca and where next leters are n or r ls -l ca[nr]*
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 21:01 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:01 cacak -rw-r--r--. 1 root root 0 Aug 20 21:01 candy -rw-r--r--. 1 root root 0 Aug 20 21:01 car -rw-r--r--. 1 root root 0 Aug 20 21:01 konj [darko@kompjuteras]$ ls -l [cb]* -rw-r--r--. 1 root root 0 Aug 20 21:01 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:01 cacak -rw-r--r--. 1 root root 0 Aug 20 21:01 candy -rw-r--r--. 1 root root 0 Aug 20 21:01 car [darko@kompjuteras]$ ls -l [bk]* -rw-r--r--. 1 root root 0 Aug 20 21:01 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:01 konj [darko@kompjuteras]$ ls -l ca[nr]* -rw-r--r--. 1 root root 0 Aug 20 21:01 candy -rw-r--r--. 1 root root 0 Aug 20 21:01 car
[Something-SomethingOther] → will list a ranges
# This will list all files who begins with small letters a, b, c or d ls -l [a-d]* # This will list all files who begins with 1,2,3 or 4 ls -l [1-4]* # This will list all files who begins with A, B, a or b ls -l [a-B]* # This will list all files who begins with 1,2,...,9, a, b, A, B ls -l [1-B]*
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 21:15 1fajl -rw-r--r--. 1 root root 0 Aug 20 21:15 4fajl -rw-r--r--. 1 root root 0 Aug 20 21:17 5fajl -rw-r--r--. 1 root root 0 Aug 20 21:15 ananas -rw-r--r--. 1 root root 0 Aug 20 21:18 Babusnica -rw-r--r--. 1 root root 0 Aug 20 21:15 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:15 bugarska -rw-r--r--. 1 root root 0 Aug 20 21:15 cicvara -rw-r--r--. 1 root root 0 Aug 20 21:15 dinar -rw-r--r--. 1 root root 0 Aug 20 21:15 jelena -rw-r--r--. 1 root root 0 Aug 20 21:18 Kikinda -rw-r--r--. 1 root root 0 Aug 20 21:15 snezana [darko@kompjuteras]$ ls -l [a-d]* -rw-r--r--. 1 root root 0 Aug 20 21:15 ananas -rw-r--r--. 1 root root 0 Aug 20 21:18 Babusnica -rw-r--r--. 1 root root 0 Aug 20 21:15 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:15 bugarska -rw-r--r--. 1 root root 0 Aug 20 21:15 cicvara -rw-r--r--. 1 root root 0 Aug 20 21:15 dinar [darko@kompjuteras]$ ls -l [1-4]* -rw-r--r--. 1 root root 0 Aug 20 21:15 1fajl -rw-r--r--. 1 root root 0 Aug 20 21:15 4fajl [darko@kompjuteras]$ ls -l [1-B]* -rw-r--r--. 1 root root 0 Aug 20 21:15 1fajl -rw-r--r--. 1 root root 0 Aug 20 21:15 4fajl -rw-r--r--. 1 root root 0 Aug 20 21:17 5fajl -rw-r--r--. 1 root root 0 Aug 20 21:15 ananas -rw-r--r--. 1 root root 0 Aug 20 21:18 Babusnica -rw-r--r--. 1 root root 0 Aug 20 21:15 barajevo -rw-r--r--. 1 root root 0 Aug 20 21:15 bugarska
[[:named:]] → Named character classes…will list named values
# alpha - all letters # alnum - letters and numbers # digit - only numbers # lower - lowercase # space - files with spaces # upper - upercases # This will list all files who begins with letters ls -l [[:alpha:]]* # This will list all files who begins with letters or numbers ls -l [[:alnum:]]* # This will list all files who have numbers in name ls -l *[[:digit:]]* # This will list all files who have spaces in name ls -l *[[:space:]]* # This will list all files who have UPERCASES ls -l *[[:upper:]]* # This will list all files who have lowercases ls -l *[[:lower:]]*
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 21:37 %#%# -rw-r--r--. 1 root root 0 Aug 20 21:32 7njs -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 bbb -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces -rw-r--r--. 1 root root 0 Aug 20 21:32 %Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l [[:alpha:]]* -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 bbb -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l [[:alnum:]]* -rw-r--r--. 1 root root 0 Aug 20 21:32 7njs -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 bbb -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l *[[:digit:]]* -rw-r--r--. 1 root root 0 Aug 20 21:32 7njs -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b [darko@kompjuteras]$ ls -l *[[:space:]]* -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces [darko@kompjuteras]$ ls -l *[[:upper:]]* -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces -rw-r--r--. 1 root root 0 Aug 20 21:32 %Other -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l *[[:lower:]]* -rw-r--r--. 1 root root 0 Aug 20 21:32 7njs -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 bbb -rw-r--r--. 1 root root 0 Aug 20 21:30 D2b -rw-r--r--. 1 root root 0 Aug 20 21:30 File with spaces -rw-r--r--. 1 root root 0 Aug 20 21:32 %Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda*
\ → escape characters…if you search for file who contain wildcard character
# This will list all files who have question mark (?) in name ls -l *\?* # This will list all files who have start (*) in name ls -l *\**
Examples…
[darko@kompjuteras]$ ls -l ### All files total 0 -rw-r--r--. 1 root root 0 Aug 20 21:37 %#%# -rw-r--r--. 1 root root 0 Aug 20 21:30 aaa -rw-r--r--. 1 root root 0 Aug 20 21:30 CCC -rw-r--r--. 1 root root 0 Aug 20 21:32 %Other -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? -rw-r--r--. 1 root root 0 Aug 20 21:31 So#Me -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda* [darko@kompjuteras]$ ls -l *\?* -rw-r--r--. 1 root root 0 Aug 20 21:44 shvatio? [darko@kompjuteras]$ ls -l *\** -rw-r--r--. 1 root root 0 Aug 20 21:47 Zvezda*