Linux - the ls command and the *.c pattern

The purpose of this article is to explain what the ls *.c command does when the user types it and hit enter in the terminal. But first, it is important to understand what a command is and the difference between terminal, console and shell.

A command is a specific instruction given to a computer application to perform some kind of task or function. Commands must always be entered into a command line interpreter exactly. Entering a command incorrectly (wrong syntax, misspelling, etc.) could cause the command to fail or worse, could execute the wrong command or the right command in the wrong way, creating serious problems.

Other important concepts to understand are terminal, console, shell and kernel. While learning Linux we get confused with the actual meaning of these terms, sometimes we mix up their usage which is technically incorrect so having a clear grasp of what every word actually means and how and where they should be used is very important.

  1. Terminal: For those having Linux OS, when we click to open the icon having the name “Terminal”, a window is opened, so in simple terms that rectangular frame or window is called terminal. We can say the terminal is a dumb thing so it does not know what to do with the input, so it needs another program to process it, and in most cases, it’s the Shell.
  2. Console: In the case of Windows OS, the “Console” performs the same operations performed by the terminal, so we can say for Windows OS, the Console is the alias name for the Terminal.
  3. Shell: After writing our commands on the terminal, when we press the Enter key, the terminal passes those commands to another program to figure out what the user wants to do, and in most cases, that program is the Shell. The shell is the mediator or the interface between the end-user and the kernel. There are various shell programs we can choose from namely TCSH, KSH, Seashell, Bash, and many more, but the default one on most Linux systems and on the Mac is the GNU Bash.
  4. Kernel: It’s a computer program that forms the innermost component of an OS after the Shell. ?As soon as the shell converts the user’s command into kernel-understandable form, the kernel is responsible to execute the commands with the help of its 2 components which are OS libraries and Device Drivers interacting with Application software and device hardware respectively.

To explain the full ls *.c command line, we are going to break this line in 2 parts. first, we have ls which is one of the basic commands that any Linux user should know. For example, when the user types ls in the terminal and hit enter, the terminal shows all files and directories that are within the file system. For example, in the picture below, the user has entered the ls command and the terminal shows a total of 36 files within the current directory.

Current directory shows a total of 36 files using the command "ls"

The second part of the code is the *.c pattern, which is a way of telling the command ls to list only the files that have a .c extension, which are usually C source code files. In the second picture, the user has entered the full command line ls *.c, and it can be seen that only files with .c extension are shown in the terminal.

Entering

Another brief example, if the user has a directory that contains the following files:

  • hello.c
  • world.c
  • readme.txt
  • Makefile

Then, using ls *.c will show only the first two files:

  • hello.c
  • world.c

In summary, this can be helpful if the user wants to see only the C files in a directory and avoid getting distracted by other files or directories.

It is also possible to use other options with ls to customize the output.?For example, using -l will show you the files in a long listing format, which includes information such as file permissions, owner, group, size, date, and time.?Using -a will show you all files, including hidden ones that start with a dot.?Using -h will make the file sizes human-readable. You can combine multiple options together, such as ls -lah *.c to see all the details of the C files in a human-friendly way.



Bibliography:

Fisher, Tim. What is a Command for Computers. July, 2023. Link: What Is a Command for Computers? (lifewire.com)

What is a terminal, Console, Shell and Kernel. October, 2020. Link: What is Terminal, Console, Shell and Kernel? - GeeksforGeeks


要查看或添加评论,请登录

社区洞察

其他会员也浏览了