Photo by Anas Alshanti on Unsplash

How to list all .c files in your current directory

The command ls *.c will list all files with the extension of .c in the current directory that you are in (does not include subdirectories). How does it work though? The rest of this blog will explain some concepts of ls and how to find any file extensions in the current directory you are in.

Referring from the manual man ls . This command is short for “list”, and does what you would think it’s name implies: lists something. The synopsis (command format) displays this command as:

ls [OPTION]… [FILE]…

In order to find one or many files with a specific file extension, we will not be using any [OPTION]. We will be filling in only [FILE], this will tell ls what kind of specific file do we want to list from this current directory?

So the code ls *.c is filling [FILE] with *.c , what does it mean?

*(asterisk) can represent any number of characters

Since we are trying to find multiple files of a specific extension, we will be using * in front of the file extension that we want to find. Below is a chart of how the code is read:

Because * can represent any line of characters, ls will list all files that pertain to the extension that you are specifying. .c is represented by a blue line, and it finds all files in the current directory with the extension of .c . * is represented by the red/pink line, and it finds any name that pertains to .c . So the result prints out all files with the extension of .c of any name.

Software Engineer living in Tokyo | Linux | Cats | https://github.com/kai-dg | https://haruspace.dev | https://ko-fi.com/harukai

Software Engineer living in Tokyo | Linux | Cats | https://github.com/kai-dg | https://haruspace.dev | https://ko-fi.com/harukai