What happens when you type `ls *.c`
What is “ls”? The first thing that I ask myself when I have to answer this question is to define all the terms and go in depth about what happens.
Before we jump in on the command itself, there are multiple things to explain about the shell. The first thing to look at is the “PS1” environment variable, commonly known as the prompt. The prompt is displayed by default and after a command is done, it is displayed again. The prompt can be made to display useful information to the user, usually username, host, date and current directory.
Then you type in your command “ls *.c”. It shows up after your prompt. Then you press “enter”. TA-DA! Something happened, you see the result and your prompt shows up again. But lots of things happened in between the time you pressed “enter” and the results are printed out.
The shell saw what you wrote and is interpreting it. To do that, it looks at the first element (in this case,
ls ) and checks first if there is an alias for it. Then it checks if there is a built-in command for it. In this case,
ls isn’t a built-in or an alias and so it will look in the “PATH” environment variable. “PATH” contains a list of colon separated directories which the system will search through until it finds the command asked for or exits with failure.
echo $PATH will usually outputs something along the lines of:
ls is a command in linux and other unix based operating system that lets the user to list information about files or directories. Have a go at everything “ls” has to offer by typing
man ls in your terminal. To recapitulate, “ls” will let you list files and directories (in your current directory by default).
Next is “*.c”: to understand this, we have to split it up even more. ‘*’ (asterisk) is an expansion wildcard in linux/unix (Wildcards). The asterisk lets you represent any number of characters such that it can mean anything from nothing ‘’ to white space ‘ ’ to any length of a string ‘abcdef…blah+halb-yrudoindis’. The representation “*.c” then means everything starting with any number of characters and ending with “.c”.
Putting it all together,
ls *.c means list all files and directories (in current directory) that end with “.c”.