Designed by pikisuperstar / Freepik

Let’s talk about finding specific files in the shell

Sebastián Paruma
3 min readJun 10, 2020

--

But first, we need to know what is “the shell”

“The shell is a program that takes commands from the keyboard and gives them to the operating system to perform.” ~William Shotts

Okay, but maybe you’re asking how you can interact with it…

Well… you need to use a terminal, it’s a program that opens a window where you can type commands and send them to the shell.

And now we get into the…

Command Line Interface (CLI)

When you open the terminal you will look a ‘$’ which is called a command prompt, this element is stored inside an “environment variable” called PS1.

This is what you see when the terminal opens.

You can interpret an ‘environment variable’ as multiple commands /operations stored in a little memory space inside the shell.

That being said, we can talk about the command we’re gonna use to find our specific files which is:

ls *.c

To understand it better, we will split it into two parts.

ls: Where does this command come from?

First, we need to know that ls is an alias.

“An alias allows us to create simple names for the long and complex original commands and then using it the same way”.

Remember when we talk about environment variables? Those enter here too, but this time we introduce another variable called PATH, it contains a list of directories separated by colons where the shell looks inside each one to find commands when you type it and press Enter.

ls is located in the directory /usr/bin/ls.

And finally, we will know…

What ‘ls’ actually do?

This is one of the most used commands in Unix-based systems and it’s so useful. We can use it inside directories which are folders that contain files or sub directories, when we type ls and press Enter we will see a list of all the files and sub-directories that are present in the current directory.

Here, we can see the files and sub-directories that are inside a directory called linux.

We also can add flags or options to the command ls to make it more powerful and complete. One of these options is “-l” which is used to list the files in a long format and another interesting one would be “-a” that shows up the hidden files. But, what if we use both together? Let’s see!

It lists the content in a long format including hidden files!

That’s so cool, isn’t it?

Now that we know what happens when you type ls and what this command does, we need to go through the second part of our initial command.

*.c

You’re now probably asking what is that ‘asterisk’ followed by a “.c”, the asterisk is what we call a wildcard, a shell feature that makes commands so powerful.

“Wildcards allow you to select filenames based on patterns of characters.”~William Shotts

The asterisk *’ wildcard matches any characters. For example, if we are looking to list the files inside our current directory that begins with the letter “s”, we can do it by typing:

ls s*

So, when we type ‘ls *.c’ we will have a list of all the files (or directories) that we have in the current directory that ends with ‘.c’.

Only the files that end with “.c” are listed.

Awesome! Now you have learned how you can easily find any kind of files you’re searching for inside your current directory.

--

--