What happens when you type ls *.c?

Soledad Frechou
6 min readSep 20, 2020

--

All about organization

Having an organized environment is something yearned for in many areas of our life. Organization can happen in simple daily places such as a pantry, desk, overall home, school notes, to big businesses and intricate systems, amongst many others. Every organized environment, no matter how simple or complex it is, must be easily accessed by whoever is interacting with it. It has to be understood and navigated with ease and comfort and overall practicality.

Linux file system

The Linux file system, as most file systems, works like an internal network where all the information is stored — from operating system files, user information, user input and more. Files and directories can be created by the users, and managed by them, groups, and others. Navigation is carried out by prompting commands. The starting point, mother of all folders is the root folder, better known as the root directory. The file system develops inside it and can be made up of numerous amounts of directories within directories.

These directories can contain other directories inside, as well as files. Files can be of any extension or type and can be stored in any directory within this file system network. As previously stated, any organized environment must be easily accessed and navigated, and the Linux file system is no exception to this rule. There are several commands that come in handy to properly, easily and comfortably navigate the system and access the information within every directory in an organized and practical manner.

There are four main types of commands:

Executable programs, built in commands (part of the shell itself), shell functions (useful for scripting repetitive administration tasks) and alias (a replacement command to call other commands).

In this post, we will concentrate on executable programs. A program is a sequence of instructions that the computer understands, to carry out a specific action. These are considered core commands and are stored in the directory /usr/bin, a directory that the system refers to when carrying out a command.

Linux works with files and directories, and everything can be found in them.

Every time a core command is prompted, the user is “standing somewhere”. This means that the user is inside a specific place in this file network — and this place is called a directory. It is very important to know where you are “standing” — a.k.a where in the directory system you are, because whatever you prompt will be acting over the contents of that directory, or take this directories location as a starting point.

Core commands — ls

One of the most important core commands is ls. It is used to list the files and directories within the directory the user prompts it from. Basically, it shows everything stored in that directory. It literally lists the contents — a key word to remember and associate with the command: list — ls.

When prompted, ls will output the list of names of contents of the directory where it was prompted from, to the standard output and will show more, or less information, depending on the options prompted. This means that ls can be executed by itself, or options and arguments can be added to output further information and details about the content of the directory.

By itself, ls only prompts the names of files and directories.

Some options include:

ls — l for long format. This outputs not only the file name, but also the file type, the file permissions, number of hard links to the file, file owner, file group, file size (shown in bytes), and date and time.

ls — a will list the hidden files, since ls does not show them by default. Hidden files are those that begin with a dot (.):

ls — R will list the contents of the subdirectories recursively:

Linux is case sensitive! Therefore -r is not the same as -R!

And many options more.

Also, options can be prompted several at once to combine the outputs, like so:

ls -la will list the output in long format AND show hidden files:

In complex systems, a directory can be filled with hundreds of files. The output of ls in therefore very long and tedious to analyze when looking for a specific type of file or files. Here is where wildcards can come in handy.

Wildcards

Wildcards are symbols that can represent certain characters or group of characters. Kind of how a Joker works in card games. Wildcards are used as a substitute for characters and group the outcome in order to take actions upon them.

Combined with ls, wildcards can be used to filter the output. The most generic wildcard is *. This wildcard represents every character. This might seem useless, because if it represents every character, there is no obvious use for it. But, * can be combined with other characters to make up the filter needed.

When placed after a word or character, * represents everything that comes before said characters. When placed after certain characters, * represents everything that comes afterwards. This is better explained with examples:

Let’s say every item in my grocery list is made of its name followed by the category it belongs to:

If I were to find myself in the supermarket with this list, and was passing through the vegetable section, I would want to see only the vegetables in my list in order to get them all at once, and not having to walk back and forth from different sections of the supermarket every time. If I were to have my list on a directory, and every item was a file, I would prompt ls *vegetable, and the output would list ONLY those items ending with the extension “.vegetable”.

The * can also be used at the end of a text, to list files that begin with a certain letter for example (a*) or in the middle of a text to list files that begin with a certain letter(s) AND end with others, for example (a*.vegetables).

There are other types of wildcards that work differently than the *:

? = Matches a single character. Can be used more than once to represent as many single characters as needed. For example: “number:?2” will list only the files that are called “number:” and end with “2”, but only have one character before that “2”: number:02, number:12, number:22, number:32…

[xx] = Where the x’s represent any characters. This will match the characters enclosed in the brackets. For example: Photo[1–6]00 will match “Photo100”, “Photo200”, “Photo300”, “Photo400”, “Photo500” and “Photo600”.

Therefore, with the concepts stated above, we are now able to break down what ls *.c will do.

ls will list all files, but *.c will filter the output to only list those files with the “.c” extension. No matter the name, if the file ends with a “.c”, it will be listed.

Output of “ls *.c”

Conclusion

ls is one of many core commands in the Linux operating system that helps understand and display the contents of any directory

.ls is a very useful command and together with the options and wildcards the command can be optimized for the convenience of the user in order to improve the understanding and exploration of the files within any directory for a better file manipulation and listing.

--

--