The ls command and * wildcard

Isaac Wong
3 min readJan 16, 2018

--

So, you’ve pulled up to a Linux or Unix-like computer, and have somehow stumbled upon empty terminal that looks something like this eh?

An empty terminal!

This is the interface through which you can interact with the shell, “A shell is a program that provides the traditional, text-only user interface for Linux and other Unix-like operating systems.” ~The shell takes commands and provides them to the operating system to execute, or run.

But how to get started? Well, since you can’t see the files yet, the first thing that many people do list the contents with a command called “ls” — list.

command: ls

Type in “ls” at the prompt, and hit <ENTER>. The contents of the directory, or folder, that you are currently at will be shown, or listed. Every computer may have different files that are stored on the computer, so your output may be different than the one in the image above.

But let’s say that you have a large amount of files, but you are only interested in files with the extension .c from that project you were working on. How could you quickly sort through that?

Yikes! So many files? How to find my .c files?

One way to do that is to use a powerful feature called wildcards. A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasing the flexibility and efficiency of searches. One of the most used wildcards is the star or asterisk wildcard “*”. This wildcard is used to represent any character, or even no characters at all!

This is best understood through example. Going back to the issue when we are trying to find all of our files with the extension .c, you could use the following command to find the file quicky:

things are much cleaner using command: ls *.c

Instead of listing all the files in the directory with “ls”, when the command “ls *.c” was used the shell expanded the * to include all files, but you specified that you only wanted to find all files that ended with the “.c” at the end. Underneath the hood, the “*” wildcard is expanded to include all files ending in .c even before the shell even acts on it! This is something called expansion.

You are not limited to using the “*” wildcard at the beginning of files. Let’s say you wanted to find all of your major_project files. You could similarly use the wildcard “*” at the end of a list search:

command: ls major_project*

Using the command “ls major_project”, this user was able to locate all her files from her major project.

But it doesn’t stop there! Now let’s say we wanted to find any file we have with the word “project” in it. How would one do this?

command: ls *project*

Using the “ls *project* command, the user has now located any file with the word “project” located in the filename, regardless of the file extension, or any prefixes to the name.

Hopefully with these examples, you now have a better understanding of the use of “ls” and the asterisk “*” wildcard character in Linux and Unix-like computer systems.

--

--