Module 4.0 – Using Wildcard In Linux – (Globbing)

UNDERSTANDING THE SUBJECT MATTER

From the previous lessons, you have learnt how to copy, move, paste, list, delete files and directories. However, what you have not learnt is how to do all of these activities in a faster, easier, smarter and convenient way. Have you thought about deleting, copying, or listing a group of files and directories with the same pattern? that’s basically filtering. Well, with globbing, filtering of files is possible which is what we are going to look at in this lesson.

What Is Globbing In Linux

Globing is a technique a Linux shell use to match a search pattern by using special characters called wildcards.

What Is Wildcard In Linux

Wildcards are used to match a search pattern. The commonly used wildcards globbing uses are *, ?, and [].

The use of wildcards In Linux, not even only in Linux cannot be overemphasized. Wildcards can be combined together to make a great search pattern. Some of the wildcards that can be used in Linux are

* (Asterisk)

? (question mark)

[ ] (square bracket)

! (exclamation)

{} (curly bracket)

wildcard in Linux

What Is * Wildcard Used For?

The * is used to match all character.

What Is ? Wildcard Used For?

The “?” is used to match a single/one character and the character can be anything.

What Is [] Wildcard Used For?

The [] is used to match a range of character, including numbers.

The range of character includes

  • [abcde] or [a-e]
  • [ABCDE] or [A-E]
  • [5678] or [5-8]
  • [a-zA-Z]
  • [a-zA-Z0-9]

What is ! Wildcard Used For?

The ! is used to exempt a match/filter in []

What Is {} Wildcard Used For?

The “{}” is used to combine more than one globbing pattern in a filter/search with a comma.

How Does Wildcard Work?

These wildcards can be manipulated in different ways. It can be used once, more than once, or can even be combined together in a search/match pattern.

Let’s see the different ways wildcards can be used with examples in the “ACTION TIME” section.

ACTION TIME

How To Use Wildcard In Linux With Examples

How To Use The * Wildcard In Linux With Examples

In the “UNDERSTANDING THE SUBJECT MATTER” section, it was mentioned that * is used to match all character. So let’s do some examples with *

[root@HQEBPRD ~]# cd /sys/module/

[root@HQEBPRD module]# ls

8250                 kgdboc                scsi_dh_rdac
ac97_bus             kgdbts                scsi_mod
acpi                 libahci               scsi_transport_spi
acpiphp              libata                sd_mod
ahci                 libcrc32c             serio_raw
ata_generic          llc                   sg
ata_piix             md_mod                shpchp
battery              module                slab_common
blk_cgroup           mousedev              snd
block                mptbase               snd_ac97_codec
bridge               mptscsih              snd_ens1371
button               mptspi                snd_pcm
cdrom                netpoll               snd_rawmidi
cec                  nf_conntrack          snd_seq
configfs             nf_conntrack_ipv4     snd_seq_device
cpufreq              nf_conntrack_ipv6     snd_seq_midi
cpuidle              nf_conntrack_tftp     snd_seq_midi_event
crc32c_intel         nf_defrag_ipv4        snd_timer
crc32_pclmul         nf_defrag_ipv6        soundcore
crct10dif_pclmul     nf_nat                spurious
cryptomgr            nf_nat_ipv4           srcutree
debug_core           nf_nat_ipv6           sr_mod
dm_log               nf_nat_tftp           stp
dm_mirror            nfnetlink             sunrpc
dm_mod               nf_reject_ipv4        suspend
dm_region_hash       nf_reject_ipv6        syscopyarea
drm                  nf_tables             sysfillrect
drm_kms_helper       nf_tables_set         sysimgblt
dynamic_debug        nft_chain_nat_ipv4    sysrq
e1000                nft_chain_nat_ipv6    tcp_cubic
edac_core            nft_chain_route_ipv4  thermal
efi_pstore           nft_chain_route_ipv6  tpm
efivars              nft_compat            tpm_crb
ehci_hcd             nft_counter           tpm_tis
fb                   nft_ct                tpm_tis_core
fb_sys_fops          nft_fib               ttm
firmware_class       nft_fib_inet          tun
fuse                 nft_fib_ipv4          uhci_hcd
ghash_clmulni_intel  nft_fib_ipv6          uinput
hid                  nft_objref            usbcore
hid_magicmouse       nft_reject            usbhid
hid_ntrig            nft_reject_inet       uv_nmi
i2c_piix4            nls_utf8              virtio_pci
i8042                pcie_aspm             vmd
ima                  pciehp                vmw_balloon
intel_idle           pci_hotplug           vmwgfx
intel_rapl_perf      pcmcia_core           vmw_vmci
ip6_tables           pcspkr                vmw_vsock_vmci_transport
ip_set               printk                vsock
ip_tables            processor             vt
ipt_MASQUERADE       psmouse               watchdog
ipt_REJECT           pstore                workqueue
ipv6                 random                xfs
isofs                rcupdate              xhci_hcd
joydev               rcutree               xt_CHECKSUM
kdb                  rng_core              xt_conntrack
kernel               rtc_cmos              xz_dec
keyboard             scsi_dh_alua          zswap

In the /sys/module directory above,

  • To list/filter all the directories that start with intel, use the command,
[root@HQEBPRD module]# ls -ld intel*

drwxr-xr-x. 3 root root 0 Apr 14 22:31 intel_idle
drwxr-xr-x. 5 root root 0 Apr 14 22:31 intel_rapl_perf
  • To list/filter all the files and directories that start with intel, use the command,
[root@HQEBPRD module]# ls -l intel*
  • To list/filter all the directories that end with intel, use the command,
[root@HQEBPRD module]# ls -ld *intel

drwxr-xr-x. 5 root root 0 Apr 14 22:31 crc32c_intel
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ghash_clmulni_intel
[root@HQEBPRD module]#
  • To list/filter all the directories that contain intel, use the command,
[root@HQEBPRD module]# ls -ld *intel*

drwxr-xr-x. 5 root root 0 Apr 14 22:31 crc32c_intel
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ghash_clmulni_intel
drwxr-xr-x. 3 root root 0 Apr 14 22:31 intel_idle
drwxr-xr-x. 5 root root 0 Apr 14 22:31 intel_rapl_perf
[root@HQEBPRD module]#

For the purpose of this exercise, create temporary files in /media by using the command,

[root@HQEBPRD ~]# touch /media/tekneed1 /media/tekneed2 /media/tekneed3 /media/tekneed4 /media/tekneed4 /media/tekneed5 /media/need1tek /media/need2tek /media/need3tek /media/need4tek /media/need5tek
  • To copy all the files that start with tek to the /tmp directory, use the command,
[root@HQEBPRD ~]# cp /media/tek* /tmp
[root@HQEBPRD ~]#
  • List the files that start with tek in /tmp to verify.
[root@HQEBPRD ~]# ls -l /tmp/tek*

-rw-r--r--. 1 root root 0 Apr 14 23:04 /tmp/tekneed1
-rw-r--r--. 1 root root 0 Apr 14 23:04 /tmp/tekneed2
-rw-r--r--. 1 root root 0 Apr 14 23:04 /tmp/tekneed3
-rw-r--r--. 1 root root 0 Apr 14 23:04 /tmp/tekneed4
-rw-r--r--. 1 root root 0 Apr 14 23:04 /tmp/tekneed5
[root@HQEBPRD ~]#

To delete all the files that end with tek in /media directory, use the command,

[root@HQEBPRD ~]# rm -rf  /media/*tek

verify,

[root@HQEBPRD ~]# ls -l /media

total 0
-rw-r--r--. 1 root root 0 Apr 14 23:02 tekneed1
-rw-r--r--. 1 root root 0 Apr 14 23:02 tekneed2
-rw-r--r--. 1 root root 0 Apr 14 23:02 tekneed3
-rw-r--r--. 1 root root 0 Apr 14 23:02 tekneed4
-rw-r--r--. 1 root root 0 Apr 14 23:02 tekneed5
[root@HQEBPRD ~]#

you can see the different ways * can be used. If you also need to zip a particular set of files, the * wildcard can be used. Like I mentioned in “UNDERSTANDING THE SUBJECT MATTER” section, wildcards are used to match a particular pattern. All you need to do is to use it against a utility.

How To Use The ? Wildcard In Linux With Examples

In the “UNDERSTANDING THE SUBJECT MATTER” section, it was mentioned that ? is used to match a single/one character. So let’s do some examples with ?

In the “/sys/module” directory that was listed above,

  • To list/filter all the directories that have three characters, use the command
[root@HQEBPRD module]# ls -ld ???

drwxr-xr-x. 3 root root 0 Apr 14 22:31 cec
drwxr-xr-x. 6 root root 0 Apr 14 22:31 drm
drwxr-xr-x. 3 root root 0 Apr 14 22:31 hid
drwxr-xr-x. 3 root root 0 Apr 14 22:31 ima
drwxr-xr-x. 3 root root 0 Apr 14 22:31 kdb
drwxr-xr-x. 5 root root 0 Apr 14 22:31 llc
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd
drwxr-xr-x. 5 root root 0 Apr 14 22:31 stp
drwxr-xr-x. 3 root root 0 Apr 14 22:31 tpm
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ttm
drwxr-xr-x. 5 root root 0 Apr 14 22:31 tun
drwxr-xr-x. 3 root root 0 Apr 14 22:31 vmd
drwxr-xr-x. 5 root root 0 Apr 14 22:31 xfs

To list/filter all the directories that have three characters before “_” , use the command

[root@HQEBPRD module]# ls -ld ???_*

drwxr-xr-x. 6 root root 0 Apr 14 22:31 ata_generic
drwxr-xr-x. 6 root root 0 Apr 14 22:31 ata_piix
drwxr-xr-x. 3 root root 0 Apr 14 22:31 blk_cgroup
drwxr-xr-x. 6 root root 0 Apr 14 22:31 drm_kms_helper
drwxr-xr-x. 3 root root 0 Apr 14 22:31 efi_pstore
drwxr-xr-x. 4 root root 0 Apr 14 22:31 hid_magicmouse
drwxr-xr-x. 4 root root 0 Apr 14 22:31 hid_ntrig
drwxr-xr-x. 6 root root 0 Apr 14 22:31 i2c_piix4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ip6_tables
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ipt_MASQUERADE
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ipt_REJECT
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_compat
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_counter
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_ct
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_inet
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_objref
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_reject
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_reject_inet
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nls_utf8
drwxr-xr-x. 3 root root 0 Apr 14 22:31 pci_hotplug
drwxr-xr-x. 3 root root 0 Apr 14 22:31 rng_core
drwxr-xr-x. 3 root root 0 Apr 14 22:31 rtc_cmos
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_ac97_codec
drwxr-xr-x. 7 root root 0 Apr 14 22:31 snd_ens1371
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_pcm
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_rawmidi
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_seq
drwxr-xr-x. 5 root root 0 Apr 14 22:31 snd_seq_device
drwxr-xr-x. 7 root root 0 Apr 14 22:31 snd_seq_midi
drwxr-xr-x. 5 root root 0 Apr 14 22:31 snd_seq_midi_event
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_timer
drwxr-xr-x. 3 root root 0 Apr 14 22:31 tcp_cubic
drwxr-xr-x. 2 root root 0 Apr 14 22:31 tpm_crb
drwxr-xr-x. 3 root root 0 Apr 14 22:31 tpm_tis
drwxr-xr-x. 2 root root 0 Apr 14 22:31 tpm_tis_core
drwxr-xr-x. 5 root root 0 Apr 14 22:31 vmw_balloon
drwxr-xr-x. 6 root root 0 Apr 14 22:31 vmw_vmci
drwxr-xr-x. 5 root root 0 Apr 14 22:31 vmw_vsock_vmci_transport

you can see the combination of two wild cards in the above example. The * wildcard represents all in the example.

To list/filter all the directories that have at least three characters and ends with core, use the command

[root@HQEBPRD module]# ls -ld ???*core

drwxr-xr-x. 3 root root 0 Apr 14 22:31 debug_core
drwxr-xr-x. 3 root root 0 Apr 14 22:31 edac_core
drwxr-xr-x. 3 root root 0 Apr 14 22:31 pcmcia_core
drwxr-xr-x. 3 root root 0 Apr 14 22:31 rng_core
drwxr-xr-x. 6 root root 0 Apr 14 22:31 soundcore
drwxr-xr-x. 2 root root 0 Apr 14 22:31 tpm_tis_core
drwxr-xr-x. 4 root root 0 Apr 14 22:31 usbcore

To list/filter all the directories that have at least six characters and ends with core, use the command

[root@HQEBPRD module]# ls -ld ??????*core

drwxr-xr-x. 3 root root 0 Apr 14 22:31 debug_core
drwxr-xr-x. 3 root root 0 Apr 14 22:31 pcmcia_core
drwxr-xr-x. 2 root root 0 Apr 14 22:31 tpm_tis_core
[root@HQEBPRD module]#

How To Use The [] Wildcard In Linux With Examples

In the “UNDERSTANDING THE SUBJECT MATTER” section, it was mentioned that [] is used to match a range of characters. So let’s do some examples with []

In the “/sys/module” directory that was listed above,

  • To list/filter all the directories that end with numbers, 4 and 6, use the command,
[root@HQEBPRD module]# ls -ld *[4,6]

drwxr-xr-x. 6 root root 0 Apr 14 22:31 i2c_piix4
drwxr-xr-x. 3 root root 0 Apr 14 22:31 ipv6
drwxr-xr-x. 6 root root 0 Apr 14 22:31 nf_conntrack_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_conntrack_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_defrag_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_defrag_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_reject_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_reject_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv6
[root@HQEBPRD module]#
  • To list/filter all the directories that have numbers , use the command
[root@HQEBPRD module]# ls -ld *[0-9]*

drwxr-xr-x. 3 root root 0 Apr 14 22:31 8250
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ac97_bus
drwxr-xr-x. 5 root root 0 Apr 14 22:31 crc32c_intel
drwxr-xr-x. 5 root root 0 Apr 14 22:31 crc32_pclmul
drwxr-xr-x. 5 root root 0 Apr 14 22:31 crct10dif_pclmul
drwxr-xr-x. 7 root root 0 Apr 14 22:31 e1000
drwxr-xr-x. 6 root root 0 Apr 14 22:31 i2c_piix4
drwxr-xr-x. 3 root root 0 Apr 14 22:31 i8042
drwxr-xr-x. 5 root root 0 Apr 14 22:31 ip6_tables
drwxr-xr-x. 3 root root 0 Apr 14 22:31 ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 libcrc32c
drwxr-xr-x. 6 root root 0 Apr 14 22:31 nf_conntrack_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_conntrack_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_defrag_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_defrag_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_reject_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nf_reject_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_chain_route_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv4
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nft_fib_ipv6
drwxr-xr-x. 5 root root 0 Apr 14 22:31 nls_utf8
drwxr-xr-x. 6 root root 0 Apr 14 22:31 snd_ac97_codec
drwxr-xr-x. 7 root root 0 Apr 14 22:31 snd_ens1371
[root@HQEBPRD module]#

To list/filter all the directories that start with u,v,w or x , use the command,

[root@HQEBPRD module]# ls -ld [u-x]*

drwxr-xr-x. 4 root root 0 Apr 14 22:31 uhci_hcd
drwxr-xr-x. 5 root root 0 Apr 14 22:31 uinput
drwxr-xr-x. 4 root root 0 Apr 14 22:31 usbcore
drwxr-xr-x. 4 root root 0 Apr 14 22:31 usbhid
drwxr-xr-x. 3 root root 0 Apr 14 22:31 uv_nmi
drwxr-xr-x. 4 root root 0 Apr 14 22:31 virtio_pci
drwxr-xr-x. 3 root root 0 Apr 14 22:31 vmd
drwxr-xr-x. 5 root root 0 Apr 14 22:31 vmw_balloon
drwxr-xr-x. 7 root root 0 Apr 14 22:31 vmwgfx
drwxr-xr-x. 6 root root 0 Apr 14 22:31 vmw_vmci
drwxr-xr-x. 5 root root 0 Apr 14 22:31 vmw_vsock_vmci_transport
drwxr-xr-x. 5 root root 0 Apr 14 22:31 vsock
drwxr-xr-x. 3 root root 0 Apr 14 22:31 vt
drwxr-xr-x. 3 root root 0 Apr 14 22:31 watchdog
drwxr-xr-x. 3 root root 0 Apr 14 22:31 workqueue
drwxr-xr-x. 5 root root 0 Apr 14 22:31 xfs
drwxr-xr-x. 3 root root 0 Apr 14 22:31 xhci_hcd
drwxr-xr-x. 5 root root 0 Apr 14 22:31 xt_CHECKSUM
drwxr-xr-x. 5 root root 0 Apr 14 22:31 xt_conntrack
drwxr-xr-x. 2 root root 0 Apr 14 22:31 xz_dec
[root@HQEBPRD module]#

How To Use The ! Wildcard In Linux With Examples

In the “UNDERSTANDING THE SUBJECT MATTER” section, it was mentioned that ! is used to exempt a match/filter in []

In the “/sys/module” directory that was listed above,

To list/filter all the directories that don’t contain a to x from the start, both uppercase and lowercase, use the command,

[root@HQEBPRD module]# ls -ld [!a-xA-X]*

drwxr-xr-x. 3 root root 0 Apr 15 15:50 8250
drwxr-xr-x. 3 root root 0 Apr 15 15:50 zswap

How To Use The {} Wildcard In Linux With Examples

In the “UNDERSTANDING THE SUBJECT MATTER” section, it was mentioned that {} is used to combine more than one globbing pattern in a filter/search with a comma.

In the “/sys/module” directory that was listed above,

To list/filter all the directories that end with numbers ipv4 and ipv6, use the command,

[root@HQEBPRD module]# ls -ld {*ipv4,*ipv6}

drwxr-xr-x. 3 root root 0 Apr 15 15:24 ipv6
drwxr-xr-x. 6 root root 0 Apr 15 15:24 nf_conntrack_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_conntrack_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_defrag_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_defrag_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_reject_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nf_reject_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_chain_nat_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_chain_nat_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_chain_route_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_chain_route_ipv6
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_fib_ipv4
drwxr-xr-x. 5 root root 0 Apr 15 15:24 nft_fib_ipv6

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*