Synthird's blog

Using the asterisk symbol in the Linux terminal

The asterisk symbol (The * symbol) is used to show files and/or folders that begin and/or end with specific characters.

Show files and/or folders that end with specific characters

For example, if you want to show only .txt files in the folder you're currently in, type:

ls -d *.txt

If you want to show only files and folders that end with a lowercase h, type:

ls -d *h

If you want to show only files and folders that end with sv, type:

ls -d *sv

Show files and/or folders that begin with specific characters

You can also use the asterisk to find files and/or folders that begin with specific characters.

For example, to show files and folders that begin with a lowercase a, type:

ls -d a*

To show files and folders that begin with ra, type:

ls -d ra*

Show files and/or folders that begin and end with specific characters

You can also show files and/or folders that begin and end with specific characters.

For example, If you want to show only .txt files that begin with a lowercase n, type:

ls -d n*.txt

If you want to show files and folders that begin with a lowercase r and end with a lowercase t, type:

ls -d r*t

If you want to show files and folders that begin with ra and end with a lowercase t, type:

ls -d ra*t

If you want to show files and folders that begin with a lowercase p and end with sv, type:

ls -d p*sv

If you want to show files and folders that begin with ma and end with sh, type:

ls -d ma*sh

Why the -d flag is used

The -d flag is used because without it, the ls command will go through the subfolders of the folder you're currently in.

To better explain what I mean, let's create a folder called animals:

mkdir animals

Next, create a .txt file called alligator inside the animals folder:

touch animals/alligator.txt

Next, create a .txt file called apple outside the animals folder:

touch apple.txt

Now type this command to see only files that begin with a lowercase a:

ls a*

As you can see, the ls a* command doesn't just show apple.txt and the animals folder, it searches through the animals folder to show alligator.txt:

apple.txt

animals:
alligator.txt

If we don't want to see alligator.txt, but see only apple.txt as well as the animals folder without going through it, we use the -d flag:

ls -d a*

You should see the animals folder and apple.txt, but not alligator.txt:

animals  apple.txt