Wildcards in Linux System
Tags: linux, ubuntu, computerscience, beginners
Published: 2020-05-08 11:36:34

In the previous posts, we have learned the following :
1. Use `pwd` to **find** our current working directory
2. `cd` to **get into** a directory
3. `ls` to **list** the directory contents
4. `file` to **determine the contents** of a file
5. `less` to **view** the text file content

Now what seems to be missing is how can we manipulate the files and directories/folders. 

But before we can start with the manipulation of files and folders we need to know something else.

Since the Linux system uses filename so much it provides us with special characters to help us rapidly specify groups of filenames. These special characters are called **wildcards**.

Okay, the wildcards seem very helpful but what characters represent wildcards and how do we use them?

Let us look at all the wildcards that shell provides courtesy of <a href = "http://linuxcommand.org/tlcl.php">"The Linux Command Line"</a>

 ![wildcards](https://dev-to-uploads.s3.amazonaws.com/i/vnxp1uywkv4ct9g5vdqy.png)

Okay so we know what wildcards are, let's go through some examples to understand the use of these wildcards:
 
- List all the files **ending** with **.txt**

![star wildcard](https://dev-to-uploads.s3.amazonaws.com/i/k8elol84thxn5wk17ktu.png)

In the above example, we first use the `ls` command to list the directory content and then since we only wanted the files that end with `.txt` we used wildcard `*` and created the command `ls *.txt`

So let's break down the command

`ls *.txt` where
`*` matches any character and
`*.txt` means any file that ends with **.txt**.

- List all the .txt files **begininig** with **f**

![wildcard-star-f](https://dev-to-uploads.s3.amazonaws.com/i/wdfvctz3n5sghrl7k499.png)

In the above example, we first use the `ls *.txt` command to list the directory content with ".txt" in the end and then since we only wanted the files and folders that start with `f` and end with `.txt` we used wildcard `*` and created a command `ls f*.txt`.

So let's break down the command

`ls f*.txt` where 
`f` specifies that the filename should have an `f` at the start,
`*` specifies that there can be any characters in between and
`.txt` means any file that has **.txt** in the end.

- List all the .txt files that begin with "textFile" and have exactly 1 character after that. For example **textFile9.txt**

![wildcard question](https://dev-to-uploads.s3.amazonaws.com/i/wpclr2adoofb5jziqr9h.png)

In the above example, we first listed all the text files in the directory using `ls *.txt` and then we used wildcard `?` and created command `ls textFile?.txt` to filter the results as per our requirement 

So let's break down the command

`ls textFile?.txt` where
`textFile` specifies that files should begin with "textFile"
One `?` after the `textFile` since we wanted files that have exactly one character after "textFile"
`.txt` means any file that has **.txt** in the end.

So similarly if we needed .txt files that started with "someText" and have exactly three characters after that we will create the command
`ls someText???.txt`

Now the `?` allows us to be generic with our filter i.e. it represents any single character but what if we want to filter our result even more

What if we wanted to get only the **files and folders** that have **numbers 2, 4, or 6**. 

So the file can start with any character and end with any character but it should contain at least one of the three numbers

![wildcard bracket](https://dev-to-uploads.s3.amazonaws.com/i/c33kax5t35sxfyd3abiy.png)

In the above example, we used the wildcard `[]` and created a command `ls *[2,4,6]*` to get the desired results.

So let's break down the command

`ls *[2,4,6]*` where
`*` represents any character
`[2,4,6]` represent that the file and folders with either number 2 or 4 or 6

Similarly, let's find out

- the files and folders that **start with** either **letter a or b or s**

![command bracket abs](https://dev-to-uploads.s3.amazonaws.com/i/oftp7x9gb23phyvig7k1.png)

In the above example, we used the wildcard `[]` and created a command `ls [abs]*` to get the desired results.

So let's break down the command

`ls [abs]*` where
`*` represents any character
`[abs]` represent that the file and folders that start with either a or b or s

Okay so till now we have seen the use of wildcards `*`, `?`, and `[]` now let us look at what are the **class wildcards** and how do we use them

![wildcards class commands](https://dev-to-uploads.s3.amazonaws.com/i/3f8n26imo89j1izlboi1.png)

The above table contains the most commonly used **class wildcards**. Now let us take an example to understand the use of class wildcard.

- List all the files and folders that do not end with a number

![wildcard class digit](https://dev-to-uploads.s3.amazonaws.com/i/pyflnjprnb5hto0rzzyu.png)

In the above example, we used the wildcard `[[:class:]]` and created a command `ls *[![:digit:]]` to get the desired results.

So let's break down the command

`ls *[![:digit:]]` where
`*` represents any character
`!` represent that negation
`[:digit:]` represents any numeral

Just like the above example, we can use the other classes as well

Okay, so that’s all the about wildcards, we now have a powerful tool in our fingertips.

So we are done with the Wildcards in Linux System and in the next part, we will start with some very useful commands for manipulation of files and folders in Linux System.

I hope you understood the wildcards and the use of wildcards in the Linux System. Please, let me know if there are any questions.