ls *.c | Analysis for beginners | Linux
Today we are going to analyze what happens when you put this command in your terminal.
$ ls *.c
What does ls stand for?
Well… ls stands for list, and this already gives you an idea of what is happening when you type the command alone in your terminal.
What does it do?
This command will show you a list of every directory and file in your current directory.
Let's say you open your terminal in a directory that has 3 files and 3 folders, but you can’t remember the name of the file you need to see, at that moment you can use the command ls without any arguments.
$ ls
this_is_file_number_1 this_is_folder_number_1
this_is_file_number_2 this_is_folder_number_2
this_is_file_number_3 this_is_folder_number_3
Has you can see, by default ls will list everything in the current directory, but if you want it can do it in another location, just add the path at the end of the command.
$ ls /the/path/you/what/to/list
another_file
another_folder
Wildcard *
A wildcard is a character that will allow you to represent a pattern instead of a specific string.
This time we are using the wildcard * (asterisk), this is going to match everything, no matter which character or how many characters are, it will match it no matter what.
But what if what we want to match it’s all the strings that begin with a letter a and ends with a number 1?
a*1 will match:
aju7489fmvu1
a1
asomethingsomething1
Because all of those strings begin with and a and end with a 1, no matter what they got in the middle.
If you are interested in wildcards and want to know more go here.
File Extension
When you have a file in your computer, usually the computer knows what program to use to open a specific file, and that program accepts the file and knows how to read it… but… how?
Well… a little part of that magic is due to the file extension, that is a little prefix that goes right at the end of the file.
Take document.txt has an example, in that case, .txt is the file’s extension.
A file named coolsong.mp3 has a .mp3 extension.
So… let’s do it!!!
Now that you understand all of these concepts, imagine some ask you to search:
All the files inside a directory that ends with the extension .c no matter what name they have.
ls --> Will list everything in a directory (The current directory by default but it can be specified if needed)
*.c --> Will match every string that ends with a '.c' extension.ls *.c --> List every file and directory that with a .c extension, no matter how are they named before the extension.$ ls *.c
file_1.c
file_2.c
Hope now you understand this useful command… play with it and see how far you can go… and remember, a blog will never beat the official documentation.