What happens when you type ls *.c
We will see what happens when executing the ls * .c command in a linux sell
For understand what * .c is, let’s first understand a few concepts.
- What is the “ls” command?
- * is a wildcard that means all.
- c is a extension of C files.
what is the Unix command to list all the files in your current directory?
Command Ls
The ls [ls] command lists the content and optional information for directories and files. When running the ls command without options, it lists the files contained in the current directory, sorting them alphabetically
* Is a wildcard that means all
Linux uses wildcards or regular expressions for character or character sequence substitution. These wildcard characters can be very useful when we launch the command to search for a file, or several files or directories whose name contains a certain sequence of characters.
C extension.
C is a programming language therefore all programs written with it must have a “.c” extension to be identified. If we want to list all C files in the current directory, we can use the command:
But,What happens when you type ls *.c?
- 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.