Categories
Linux Tutorial Series

Linux Tutorial Series – 56 – Shell globbing (aka wildcards)

Here is the video version, if you prefer it:

Today we are going to talk about shell globbing (sometimes referred to as wildcards). They both refer to the same thing (“Globbing vs wildcards,” n.d.)⁠, so I will use the names interchangeably, or just stick to wildcards since it is shorter.

Wildcards enable us to specify a set of file names using a shorthand. (Barrett, 2016)⁠ Let’s look at an example. Say I had these files:

mislav@mislavovo-racunalo:~/Linux_folder$ ls

aba.txt ab.txt a.txt cb.txt file.txt

Good. And let’s say I wanted to print out the contents of all the files whose filenames start with a. I could do so the tedious way as follows:

mislav@mislavovo-racunalo:~/Linux_folder$ cat a.txt

mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

mislav@mislavovo-racunalo:~/Linux_folder$ cat ab.txt

The more efficient way of doing this is to write the equivalent of the 3 above statements, which is:

mislav@mislavovo-racunalo:~/Linux_folder$ cat a*.txt

What did I just do here? Am I a magician? Well, not really, so let’s look at what happened.

As I stated above, wildcards enable us to specify a set of file names using a shorthand. With this particular wildcard (a*.txt), I am saying: “Give me all the filenames that start with a, have zero or more consecutive characters afterwards, and end with a .txt”. So in some intermediary step, my command looks like:

mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt ab.txt a.txt

Now here is something important – the shell does all of this expansion (this is how it is called – turning a*.txt to all of the filenames) before it executes the cat command. So, the expansion of the wildcard is done before the command runs. (Ward, 2014)⁠

Here is a list of wildcards and their meanings; the wildcard and its meaning is delimited with a dash (Barrett, 2016)⁠:

  • * – zero or more consecutive characters
  • ? – any single character
  • [set] – any single character in the given set; [abcde] matches characters a, b, c, d and e, while [a-z] matches all lowercase characters from a to z
  • [^set] or [!set] – anything not in the set (both [^set] and [!set] have equivalent meaning); i.e. [^1] is anything but the digit 1

There are also some specifics:

  • If you want to include a literal dash in the set, put it first or last
  • To include a literal closing square bracket in the set, put it first
  • To include the ^ or the ! symbol literally, don’t put it first

Thank you for reading and hope you learned something useful!

References

Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Pages 28-30

Globbing vs wildcards. (n.d.). Retrieved January 11, 2020, from https://unix.stackexchange.com/questions/413357/globbing-vs-wildcards

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 17

NewsletterUpdates on interesting things I am doing

Subscribe to my newsletter to keep abreast of the interesting things I'm doing. I will send you the newsletter only when there is something interesting. This means 0% spam, 100% interesting content.

Leave a Reply

Your email address will not be published. Required fields are marked *