What happens when you type ls *.c

Mauricio Sierra Cifuentes
4 min readSep 16, 2020

--

Command Linux ls
first let's divide and study the ls * .c command in parts:ls*.c
ls | * | .c
The ls command lists the content and optional information for directories and files. Running the ls command with no options lists the files contained in the current directory, sorting them alphabetically. but in this blog we will learn what happens when we use ls * .c*: is a wildcard used to imply that it will filter for any content..c: it is a file format extension. it only tells us what type of file we are going to look for in that caseResume:
In summary, the ls * .c command will list the information of all the files that have the extension .c format
ls *.c

what happens in the background?

When the command ls is entered, the keyboard driver recognizes that characters have been typed and passes them to the shell. The string is passed as one single string .This is being split into tokens by removing the white spaces. So now, our command has two tokens, “ls” and “*.c”.This is stored in an array of strings. This whole process is termed as Tokenization.

Now that we have the tokenized array , we have to search if each token has an alias defined .If an alias is found it is saved as a token after removing the spaces like before and again its checked for aliases. Ususally alias is stored in the following locations :~/.bashrc, ~/.bash_profile, /etc/bashrc, /etc/profile.

Next step is to check if each token is a built in function or not. If the command is built in, the shell executes the command directly, without invoking another program. ‘cd’ is a built in command. ‘ls’ is not a built in , so now system needs to find the executable for ‘ls’.

Next is for the bash to interpret the command. The first search for the command ‘ls’ is performed at $PATH. $PATH is an environmental variable which stores the path locations of all the common executable programs. This search is performed by calling a series of functions like find_user_command() ,find_user_command_in_path ,find_in_path_element. Each location specified in the PATH variable is searched for the executable corresponding to the command ‘ls’. BASH calls the function stat() to check for the existence of this executable in each of the paths.

Finally when it finds the file at the location ‘/usr/bin/ls’ , BASH performs execve() command to execute the file. There is a bunch of other things that needs to be done before the binary /usr/bin/ls is executed — the program has to be read from the disk, it’s binary format needs to be found and the appropriate handling code needs to be invoked which will read the binary into memory.

So finally…. the program is in memory and is ready to execute when it gets a chance. So, the next questions is ..how does ls command read the directories and files from disk ? For this, a list of functions are executed internally to achieve the final output.The ls utility uses a function to read the directory contents, which in turn invokes a system call to read the list of files in the directory by consulting the underlying filesystem’s inode entries. Depending on which filesystem the path specified to ls is formatted, the function used to read the directory contents will vary.So once all the entries have been retrieved , the system call returns .The final steps of the shell would be to print the prompt again. The prompt is saved as environment variable PS1.The list of files is then returned to the prompt .

  1. So finally…. the program is in memory and is ready to execute when it gets a chance. So, the next questions is ..how does ls command read the directories and files from disk ? For this, a list of functions are executed internally to achieve the final output.The ls utility uses a function to read the directory contents, which in turn invokes a system call to read the list of files in the directory by consulting the underlying filesystem’s inode entries. Depending on which filesystem the path specified to ls is formatted, the function used to read the directory contents will vary.So once all the entries have been retrieved , the system call returns .The final steps of the shell would be to print the prompt again. The prompt is saved as environment variable PS1.The list of files is then returned to the prompt .

--

--