Typing ls -la
in your terminal initiates a complex series of operations that involve the shell, the kernel, and various system calls. Let’s look into what happens when you type `ls -la` on terminal:
Command Reading: When you type
ls -la
and pressEnter
, the shell reads your input using thegetline()
function. This function reads an entire line from the input stream, including the newline character, and stores it in a dynamically allocated buffer.Command Tokenization: The shell then uses the
strtok()
function to tokenize the input string.strtok()
splits the input based on delimiters like spaces or tabs, identifying the command (ls
) and its arguments (-la
).Alias and Built-in Check: The shell checks if the first token (
ls
) is a shell alias or a built-in command. Ifls
is an alias, it is expanded to its corresponding command string. If it is a built-in function, the shell executes it directly.Command Lookup: If
ls
is neither an alias nor a built-in function, the shell searches for it in the directories listed in thePATH
environment variable.File System Access: The shell locates the
ls
executable, typically found in/bin
or/usr/bin
. This involves file system calls to check for the existence and permissions of thels
file.Process Creation: Once located, the shell uses a system call like
fork()
to create a new process that will execute thels
command. Thefork()
system call duplicates the current process, creating a child process that is almost identical to the parent. Importantly,fork()
returns0
to the child process, signaling it to act as a child, while the parent process receives the Process ID (PID) of the child.Binary Loading: The new process created by
fork()
executes thels
binary using theexecve()
system call. This replaces the current process image with a new one loaded from thels
executable.Arguments Passing: The
-la
arguments are passed tols
, instructing it to display a long list of directory contents (-l
) and include hidden files (-a
).System Calls: The
ls
command makes various system calls to the kernel, includingopen()
,readdir()
, andstat()
.open()
: Opens the directory for reading.readdir()
: Reads directory entries, retrieving file names.stat()
: Gathers file metadata such as size, permissions, and timestamps.
Directory Reading:
ls
reads the contents of the current directory by callingreaddir()
, which returns entries for each file and directory, including hidden ones due to the-a
flag.Metadata Collection: For each file,
ls
callsstat()
to collect metadata, including file type, permissions, number of links, owner, group, size, and last modification time.node Consultation: The
ls
utility uses functions to read directories and files from the disk by consulting the underlying filesystem's inode entries. Inodes contain metadata about each file or directory, such as its size, ownership, permissions, and pointers to the data blocks on the disk.Output Formatting: The
ls
command formats the collected data. The-l
flag ensures that details like permissions, links, owner, group, size, and timestamps are displayed in a human-readable format.Permissions: Permissions are displayed in a string format (e.g.,
-rwxr-xr-x
), which is a human-readable representation of the permission bits.File Types: The first character of each line in the output indicates the file type (
-
for regular files,d
for directories, etc.).Standard Output: The formatted data is written to the terminal's standard output (stdout). The terminal application then renders the text on your screen.
Terminal Interaction: The terminal might apply color coding and other formatting based on its configuration and the capabilities of the terminal emulator.
Exit: Once
ls
has finished executing, it calls the_exit()
system call with an integer0
to denote normal execution. The_exit()
system call informs the kernel that the process has completed its task, allowing the kernel to free up the resources allocated to the process.
References
https://medium.com/@birnbera/what-happens-when-you-type-ls-c-in-your-terminal-dfd353725f71
https://medium.com/swlh/the-command-line-with-ls-l-6efaf01cc4c0
https://medium.com/basic-command-ls-linux/what-happens-when-you-type-ls-l-in-the-shell-df224d5c727e
https://medium.com/@mitalisg/linux-under-the-hood-what-happens-when-you-type-ls-l-b8432e533794