What really happens when you enter “ls *.c” into a shell command line?

Cody Paral
2 min readJan 16, 2018

--

To better understand what commands do you have too know what the shell is. The shell is an interactive user interface with an operating system. It takes commands from the user and executes them. Most of the time shells have built in commands that perform specific tasks. “ls” is a command in the shell. If there isn’t an alias for “ls” the computer will know that, that command means lists all the files and directories inside of a directory. A directory is basically a folder, like the one on your desktop.

So know we know that the first part of “ls *.c” means list the files in a directory. The next part of the command is “*”. This is a wildcard. In the shell there are special characters that have specific meanings. In this case the “*” means all files and directories in a directory. You can use the wild card by itself, to tell the computer to copy or move all the files in a directory, or you can use it to select specific files. For example to list all the pictures in a folder with the extension .JPG you could use “ls *.JPG” or if you wanted to list all files starting with A you could use “ls A*”. What side you put what your looking for matters. Think of it this way, “*” means all files. So if your looking for something at the begging of a name you would put it before the “*”. If your looking for something at the end of the name you would put it at the end of the “*”.

That means in the case of “ls *.c” we’re listing all the .c files in the directory. How does it work? Before the “ls” command is ran the computer will look for arguments, which is any options you want “ls” to perform alongside listing files and directories. In this case we are telling it to list only “.c” files, so the computer will look for all of the “.c” files first. Then once all the “.c” files are found it will list them.

--

--