Wildcards
Shell wildcards are used for filename and directory name matching. They are processed by the shell and appear exclusively in command arguments.
Wildcard Patterns
| Pattern | Description |
|---|---|
* |
Matches any sequence of characters, including empty strings |
? |
Matches exactly one character |
[] |
Matches any single character within the specified range |
[a-z] |
Matches any lowercase letter |
[A-Z] |
Matches any uppercase letter |
[0-9] |
Matches any digit |
[a-zA-Z] |
Matches any letter |
[[:upper:]] |
Matches uppercase letters (equivalent to [A-Z]) |
[[:lower:]] |
Matches lowercase letters (equivalent to [a-z]) |
[[:alpha:]] |
Matches all letters |
[[:digit:]] |
Matches all digits |
[[:alnum:]] |
Matches alphanumeric characters |
[[:space:]] |
Matches whitespace characters |
[^] |
Matches any character not in the specified set |
[^0-9] |
Matches non-digit characters |
Wildcard Examples
List files starting with "arr":
$ ls arr*
arr1.sh arr2.sh arr3.sh
Find files with "arr" followed by exact one character:
$ ls arr?.sh
arr1.sh arr2.sh arr3.sh arrb.sh
Display files beginning with 'i' or 'm':
$ ls [im]*
ips myfile
Show files starting with alphabetic characters:
$ ls [[:alpha:]]*
ahead arr1.sh arr2.sh arr3.sh arrbb.sh arrb.sh arr.sh
List files starting with digits:
$ ls [0-9]*
1 2 3 4 5
Display alphanumeric starting files:
$ ls [[:alnum:]]*
1 3 5 arr1.sh arr3.sh arrb.sh File1 i m
2 4 ahead arr2.sh arrbb.sh arr.sh File2 ips myfile
Regular Expressions
Regular expressions use special character sequences too search, replace, or delete text within files. Unlike wildcards that operate on filenames, regex processes file content.
grep Command
The grep command filters and searches text patterns.
Syntax:
grep [OPTIONS] PATTERN [FILES...]
Common grep options:
| Option | Description |
|---|---|
-n |
Display line numbers |
-o |
Show only matched text |
-q |
Quiet mode (no output) |
-l |
Print matching filenames only |
-A NUM |
Show NUM lines after match |
-B NUM |
Show NUM lines before match |
-C NUM |
Show NUM context lines around match |
-c |
Count matching lines |
-E |
Use extended regex |
-i |
Case-insensitive matching |
-v |
Invert match |
-w |
Match whole words |
-x |
Match entire lines |
-r |
Recursive search |
Examples:
Display line numbers for matches:
$ grep -n "ro*" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
Extract only matching text:
$ grep -o "redhat" /etc/passwd
redhat
redhat
Show 3 lines after match:
$ grep -A 3 "adm" /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
Case-insensitive word matching:
$ grep -wi "dns" /etc/passwd
unbound:x:997:996:Unbound DNS resolver:/etc/unbound:/sbin/nologin
Basic Regular Expressions
BRE supports fundamental metacharacters:
| Character | Meaning |
|---|---|
^ |
Match at line start |
$ |
Match at line end |
. |
Match any single character |
* |
Match preceding element zero or more times |
[str] |
Match any character in str |
[^str] |
Match character not in str |
[a-b] |
Match character range a to b |
\ |
Escape special characters |
BRE Examples:
Find lines starting with "root":
$ grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
Search for lines ending with "nologin":
$ grep "nologin$" /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
Match "b", any character, then "n":
$ grep "b.n" passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
Find empty lines:
$ grep -n "^$" passwd
4:
7:
8:
Extended Regular Expressions
ERE provides additional metacharacters:
| Character | Meaning |
|---|---|
+ |
Match preceding element one or more times |
? |
Match preceding element zero or one time |
{n} |
Match exactly n times |
{n,} |
Match n or more times |
{,m} |
Match at most m times |
{n,m} |
Match between n and m times |
| `(s | t)` |
Use grep -E or egrep for extended regex.
ERE Examples:
Find lines ending with "bash":
$ egrep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash
Extract 3-4 digit numbers:
$ egrep "[0-9]{3,4}" /etc/passwd
games:x:12:100:games:/usr/games:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
Find whitespace-prefixed lines:
$ egrep "^[[:space:]]+[^[:space:]]+" /etc/grub2.cfg
load_env -f ${config_directory}/grubenv
set default="${next_entry}"
Extract IP addresses from ifconfig:
$ ifconfig | egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
192.168.72.150
255.255.255.0
192.168.72.255
Match multiple usernames:
$ egrep "(root|redhat|spark)" /etc/passwd
root:x:0:0:root:/root:/bin/bash
redhat:x:1000:1000:redhat:/home/redhat:/bin/bash