What happens when you type ls *.c in shell prompt?

As you may know, Shell is a Linux program that processes commands and returns outputs in the comand line of a terminal, shell is used to interact with the operating system. With commands, you can do whatever you like, for instance: create, move or delete files and directories; see or look for the contents of a file; write texts, do math operations and a lot more.

Getting an output -know as stdout- after we input something -known as stdin- is made by the terminal so quickly that we can’t see the processes that the system runs in the meantime. The terminal shows us only the result of the command or an error message if the command is wrong.

So, after you write a command in the prompt -the default prompt string or the variable PS1 that controls the appereance of the command line-, this is passed into the terminal; in order to obtain and output from a command the system reads the command, look into the files which contains commands, here it checks if the command is a built in, if not, then goes through the PAHT enviroment variable. The PATH is a list of directories. Every time the shell looks for a command without a path, it searchs this environment variable in every directory in order, and tries to find the file in there, then it executes the first file it finds. After that, it executes the fork-exec-and wait mechanisms along with tty and then gives and output. If it is a built-in command, this will be located in the bin directory (is the case of ls), if the command is not there it will look into another directory and so on. Once the command is done, the Bash (shell) will show the prompt or comand line again.

But, what happens when you have a command like ls -wich lists the contents of a directory in the comand line- followed by other stuff? and when I talk about other stuff, I’m refering to add other commands, expansions or file extensions. In this case, I will talk about expansions and file extensions.

When you use ls command alone, it will list every file in the current directory.

An expansion uses a wildcard or a special character in order to substite an specific type of characters in a search, so if you add a wildcard to a command this will work over that specification, in our case * matches with every character, thus it will look for each file in the current directory you are located, so instead of typing the name of every file you just put * and this with expand into every name, before the shell process the command, this is called an expansion.

If you type ls * you will have the same result as only ls, because is excludes nothing.

So far I have talked about ls *, now I need to talk about what happens when one adds the extension of a file to a command, let’s say .c. In this case, the command will list every file (due to ls *) in the current directory that has this type of extension, it works pretty similar like when you dive into a directory in documents and you organize them by kind, but there you will see each type instead of only .c files. Keep in mind that this works due to the wildcard, if that is not present, it will give you an error.

If you type ls .c it will show an error, if you include * it will show only .c files in the current dir.

But you don’t need to type ls *.c every time you look for .c files in a directorie, instead, you can create a new command with that, this is called an alias (a short name for a command), the syntax used is: alias [name[=value]], so we can create this: alias ls=ls *.c. Next time you want to use this new command just type ls in the prompt. In order to remove that alias just type: unalias ls. But keep in mind that, before the shell looks for built-ins or commands in PATH, it looks for aliases, so if you want to give a command that has an alias but do not want to use that alias, put a backslash before the command, in order to scape it.

In conclusion, when using Linux you can do a lot of things through the bash prompt, you can mix things or specify your tasks, so think about what you want, then look for a command, expansions and other things you may need and code it out!