Shell we List it All?
By Hector Lozano & Jacob Chavera
When you find yourself presented with a copious quantity of information that comes with reviewing a code from another programmer, you find that making the information easily accessible to be a necessary skill. In consideration of Unix’s creation by AT&T employees working at Bell Labs, Linux became one of the uttermost beloved Unix-based operating systems used. The command Line interface, or CLI, is one of the operating system Linux’s fascinating features. The Command Line Interface grants users the ability to interact with their computer using shell. It was amazing to be able to enter a command in your environment and have it run then return a result. This being how Linux’s shell was known as a Read, Evaluate, Print, Loop environment.
Now imagine yourself looking to learn about something specific, like let’s say information on how a spleen works. You will find yourself in an anatomy textbook, which will contain an enormous amount of information on human anatomy. If you, as a student, began reading and found the need to be looking for more specific information, primarily on the human spleen, you will find yourself using the index to save some time and reading. This is why the ls command is one of the most used Linux commands that provides a user the ability to list out all the files or directories in the Command Line Interface.
The ls command can be used in various ways in a CLI. Its primary function is to list content. The synopsis use of the ls command is normally, ls [option]... [file].... If you have the time to read through the ls command man page you will find that there are varied [options] to add before the [file] call to arrange your list in numerous orders. In this article, you will learn how to use and why you would like to use ls -l. To show a bit more clarity of how ls -l works, we will add the wildcard character asterisk ( * ). The wildcard is a placeholder so that the full names are not needed to be typed. In this article, we will break down what happens when you type “ls -l *.c” in the shell command line and hit Enter.
A brief introduction to understanding the command line interface:
The command line interface allows a user to perform tasks by entering commands. All you, the user, will need to do is enter a specific command, press Enter, then await the response.
Basic use:
-Typing pwd (Print Working Directory) and hitting the Enter key, will inform you of what directory and or file you are currently working in.
-Typing cd(Change Directory) will allow you to change or navigate to and from your current directory to another. If you follow cd with a directory name, you will be navigated to the said directory.
The command line interface in the shell will normally prompt you for a command. This prompt will indicate itself with a dollar sign ( $ ) as the default character. This prompt is informing the user that the shell is waiting for the user to enter a command. The standard input will be read by a function such as the getline function. The used function will be used to read the STDIN (Standard Input) data. This will cause the input to be read and parsed using the strtok function. This function, for further information, is strtok(3) man page, extracts tokens from strings (strings are one-dimensional arrays of characters terminated by a NULL character ‘\0’. Once information is stored, the information that has been parsed will be compared to built-in commands like cd, echo, exit, and help. If a built-in command is found, the shell will run the command, and print the request of a new prompt. Otherwise, if no path has been specified by a '/' (forward slash) character, each directory in PATH is searched in order for the command name and, if found, the command token is replaced by its absolute path before executing. In order to execute, a fork() system call is used to create a separate child process which will run our set of command tokens with another system call like execve() while the parent sits on wait() (also a system call) until that process exits. By using these system calls, we're able to directly tell the system kernel to create a totally different process, which is required when running execve(), as it takes the place of the current process. The main difference between the terms "system call" and "function," is in the direct link system calls have to the operating system. Calls such as wait(), read(), and write() cause the calling process to wait until they finish, extending execution time, so they are best kept to a minimum. Many functions also call system calls (putchar uses the write() system call), so a deeper understanding is needed to write processor-efficient code."
The command ls in shell tells the system to list information about a file or files of your current directory. Entering this command will show all of the files and folders in the directory. Once the binary executable for ls is found, a syscall fork() is made, which creates a child process to the parent process. The wait () syscall suspends the execution of the shell process while the ls executes. Once the child process terminates, it ultimately returns to the waiting shell process. What is seen through all this process is a list of data. While this is helpful, the data being displayed will still need to be narrowed down to offer a view of information that is more clear.
When we enter ls, the computer will first search for a pre-defined alias in the system. If the writer of the code does not assign an alias, the computer will then perform the default task, which would be allowing access to the directory list. This command, however also needs the -l command to perform the task at hand. Following the command ls with -l flag allows you to look at the list of contents in a long format, making the user able to look through the file system. This displays additional useful information like file types, a number of hard links to the file, permissions, sizes, time and date of last modification, file owner and file group, and of course file names.
The picture shown here is an example of how the information in ls –l is organized once it is retrieved. The red box is the first field that indicates — for file, or d for a directory (though l for a link is also a result that could be shown). The lines in the blue box represent the owner of the file. The green box shows anyone else with file permissions. The yellow box shows links and directories within the directory you are viewing. And finally, the brown box contains information on the time and dates the entries were created. By including the –l flag the user can obtain important information about their data in an organized way.
Have we forgotten to inform you about something we mentioned? Do not worry, we still have more information to break down. We still have the *.c to clarify. Remember that “ * ” is known as a wildcard in shell. This represents ALL that match what is next called. For our example, we are checking for files that end with a .c. The program will cycle through all listed items found with ls -l and pull out what is ending with a “. c “.
Once finished, shell will save the exit status of the program as zero “0” for successes. The status will be used if the next request of shell is to exit. Shell will add a history entry for the ls call. This process indexes all the previous commands in a numeral order for future reference.
Having organized access to the information contained in a program has many benefits. Being able to quickly jump to potential broken code, find previously imputed code, and viewing changes with time stamps are all options when typing ls -1 into shell. This simple command and flag add convenience and organization to an enormous amount of data.
This can be a little much to understand at first glance. Trust us when we say, that we spend time on understanding how this program works. There was a breakdown to review and testing to see how it all came together. Which lead us to want to write this blog. We found it would be useful to have a personal point of view in regards to the process of ls -l *.c . We hope this was useful and clear information to better understand a basic understanding of shell and the ls command.
Have some fun with the shell, learn new things. There is a lot of great resources on the internet. You will find much information on the ls man page alone. There are so many flags that can provide different ways to list and or organize information.
We thank you for your time and for visiting our blog.