How do Coders Look for Files? Hint: It's not the File Manager




When you need to find a file or a list of files in a computer, you usually open the file manager, or Finder if you are working on a Mac. If you are looking for a specific kind of file like an excel file or a word file, you look at the column that shows the kind of file and you will see something like pdf document, excel file, or word file. How does your file manager know what kind of file it is? When you save a file in word, for example, you select a name for the file. Let's say you name it report. But, that's not the complete name; if you want to, you can choose the kind of file you want. So you can save it as a text file, a word file, a pdf file, etc. When you do that, the file is assigned an extension: report.txt; report.doc; report.pdf, etc.  The extension are usually three letters at the end that let you know what kind of file you are dealing with.

Imagine now that you want to make a list of all the word files in your computer. Let's suppose too that you have word documents all over the place and you don't know where in your computer they are located. You could browse folders and find those files or you could use the search box and you would be able to see them displayed in the file manager; but you wouldn't be able to create a file with them. How can you make that list in a few seconds?

The bad news: you would have to leave the comfort of your graphic  interface and explore unknown territory: the terminal.

Your graphic interface looks like this:



Your terminal looks like this:





It's just a window with a prompt, ready to execute your commands. This tool can help you do lots of things and, in this case, it can help you find any file you want and any group of files too.


Let's imagine now that you are interested in learning to program in C (a programming language) and you remember that your friend used your computer a few months ago to write some programs in C. You would like to find them, but you don't know their names, you don't know where they are and you don't know exactly when he made them. This is the information you would need to find them using the file manager. However, in the terminal, this search would only require that you to type a command in the terminal and in  seconds you would have the list of files that you are looking for.

What would you have to type?

 ls *.c  

and that's it!

How does this wonderful command works? Let's look at the meaning of its parts:
ls is the list command and it lists files and/or directories
* is a wildcard that expands into any group of characters (a string)
.c is the extension of a file written in C

In other words, we are telling the terminal to make a list of all the files that end in .c

If we used only ls, we would see all the files in a directory.


It works well; but if you have hundreds of files, you would need to filter the results and that's when it comes handy to use *.c to show only the files whose name end in .c

Now let's see what happens when we add the filter. You have the list that you were looking for!








Comments

Popular Posts