What you get when you type ls *.c and hit Enter in shell

Forget the buggy linux error message penalties: this is not an actual error. Shell is just simply telling you the truth. Wondering what I mean? Stay tuned as I explain to you what actually happens when you type ls *.c and hit the Enter key in Shell. So, lets get started.

Introduction

First, lets begin with the actual ls command. The ls command is a very common task for every Linux power users, system administrators and veteran "guru" programmers. Normally, you can list the contents of your current working directory with ls and this is the most common usage of ls. To list the contents of your current working directory, just simply run ls as follows at the terminal or prompt: $ ls

Cool and simple, huh! But what the S-hell does that mean? Well, if you're a guru in software engineering then you most certainly know exactly what I'm talking about. But, if you're a little doodle baby coder in the world of programming (like me), then you should definitely be curious to know what ls command means and does especially when you use it with other options e.g. "ls *.c"

The Action

So far we could see that the ls command lists the contents of your current working directory. But what if you have to list all files with a particular file extension only, say .c the C language file extension? If a programmer was to type ls *.c the computer would interpret this as find all files in the current directory that have a .c (C programming language) file extension. This is exactly what happens when you type ls *.c and hit Enter in your shell. Expecting an explosion? Not today, sorry to disappoint you. That's all ls *.c does. So, if you want to list all files in the working directory that are made for the C Programming Language file extension then just go ahead and type ls *.c into your command line, hit the Enter key, and... Voila! You should be greeted with a list of all your files written in C in the current directory.

It's NOT an actual error

Well, so far, you got the better side of Shell today and I must say congrats. But, what if you performed this same command in an empty directory or a directory that contains all types of files other than C language file extension? If you did that, sorry my friend but Shell is not so forgiving. Shell expects forgiveness, it doesn't give it. You will surely get the dreaded "No such file or directory" message. Don't fret, its not an error per say. Shell is very sincere. It just truly means that you do not have any C language files or any files for that matter ending with the .c file extension in the current working directory. Well, forgive shell, just create one and be happy.

Conclusion

In a nutShell, The Shell's ls command list all contents in the current directory, while ls *.c lists all contents that has file extension ".c"

Happy coding friends. See you on the other side.

Advertisement
Advertisement
Advertisement
More Articles