If you have ever worked with a Linux system you should be familiar with the ls command, it is how you know what you are working with within a given directory (typically your current working directory); however, if you have never done anything Linux related before than a command such as “ls *.c” might as well be a foreign language, and in a way it is. The best way to understand something like this is to break it down into parts and go from there, so let’s start with the basic terms you might need.
Shell- Shell is just another term for user-interface, and comes in one of two varieties: command-line interface(CLI) or graphical-user interface(GUI). You are probably already familiar with a GUI as just about everything now days uses one, from your Windows or Mac computer to your smart phone and gaming consoles such as Playstation or Xbox. These interfaces have graphic cues with which you can navigate the system’s contents and usually provides a greater deal of freedom when it comes to moving around the system. CLI on the other hand operates using text commands. If you have ever opened your computer’s command prompt then you might be familiar with a CLI, and this is where the ls command really comes into play.
Language- Language is pretty much what you think it is, the grammar, syntax, and vocabulary used to create computer programs. Some common programming languages that you might have heard of include Java, Python, C++, and more. When working with a Unix system you will use a lot of Bash commands.
Command- Commands are the primary way a programmer interacts with a language.
OPTIONS- Options are different ways that a command can be modified. When looking at a commands description, if OPTIONS is followed by … then that command can accept multiple options at once, further altering just how the command behaves.
FILE- this is what the command acts on. Like with options, if FILE is followed by … the command is able to act on multiple files at once.
Directory- Directory is just another name for folder, or a location where multiple files and other folders/directories can be stored.
Path- A path is where the user is located within a system, listed as a series of directories. When trying to access a certain file the path will end in the files name.
WILDCARD- A wildcard is a special character that can match any symbol or character given, similar to how a wild card in uno is any color. The most common wild card is *
Bash- Bash is both the Unix shell and a command language used to navigate within the shell. Ls is a command within this language.
ls- Short for “list directory contents” ls is a command within bash that displays the contents of a given directory. If no further option is given it will sort the files in the directory alphabetically. ls uses the format of:
ls [OPTIONS]… [FILE]…
In the scenario ls *.c, ls is our command, we are using no options and *.c acts as our FILE. *.c specifically means “Any file or directory ending in .c” While this will usually be a C file, used to store code written in the programming language of C, there are some cases where .c will simply just be the ending of a file/directory name. So by calling ls *.c it will start checking for any and all files and directories that end in .c, and display them in alphabetical order.
So know that we know what it all means we can look at what happens when you enter ls *.c.
- The command is interpreted by your shell uses the bash language
- It recognizes that it is performing a listing (ls) and begins checking the files in the given directory (since none was given it will use the current directory).
- The shell will check those files against the parameters given, in this case our *.c (so test.txt will be ignored, but test.c will be added to its internal list)
- Once it has the list of files that meet our criteria, it will sort the file based on the options we gave it. Once again we did not give any options that altered how it sorts so it will default to alphabetical order.
- Finally it will display the sorted list to the screen for the user to view.
Now that we know what happens when you use ls *.c, you can apply that knowledge to perform other file searches, such as using ls *.txt to find any .txt (also known as text files) in your directory.