The endless terminal
An introduction to an essential tool for programmers
This is an ongoing series. Please check out the collection for the rest of the articles.
There are many ways to navigate and browse through an operating system. The way you’re probably most familiar with is called a GUI, or graphical user interface. This is a fancy term for the icons, menus, windows, progress bars, toolbars, etc. that let you interact with your computer. GUIs are great for many tasks, but there are other kinds of user interfaces worth learning and exploring. A CLI, or command line interface, is a particularly important alternative.
What’s a CLI? It looks something like this:
Actually, the image above is a terminal, or shell, which is a GUI application that provides access to the CLI. You can open like any other application and use it to control your computer via text commands; type commands into the window and the computer will react. Through the terminal, you can control every aspect of your computer. That wasn’t hyperbole — if something is possible on a computer, you can do it through the terminal. As you can imagine, this makes the terminal a very valuable tool for programmers interested in the finer details of their machines.
A common conception is that the terminal is just for computer geniuses and hackers and that it looks something like that banner at the top of the article. I promise you, though, that it’s nothing like that. The terminal is pleasant and customizable and we certainly won’t be doing any hacking during this post.
Before I explain how to use a terminal, let me back up a bit and explain how to open one. If you’ve installed Fedora (explained in the last post), then it’s very easy (in fact, it’s very easy no matter what Linux distribution you use, but each one works a bit differently so I’ll just explain how Fedora works).
First, turn on your computer and log into your account. When the desktop appears, type “terminal”. A grid of applications should appear and there should be one called “terminal”. Either click it or highlight it with the arrow keys and press “enter”. A window like the one above should show up.
So how do this apparently-really-powerful terminal work? The fundamentals are quite simple: type a command (an instruction for the computer) and press “enter”, and then witness the terminal as it precisely executes your instruction. Let’s see an example:
There’s a lot of text in the window, so let’s analyze each part of what’s happening. The terminal starts printing text at the top left of the screen. It moves to the right as it automatically prints a few pieces of information:
The text inside the [ brackets ] contains contextual information and “$” signifies where the cursor starts. “jack” is the username; “localhost” is where we’re logged in; “@” means that “jack” is the user on the computer called “localhost”. The strange sounding term localhost has two components: a host is a computer that is connected to the internet; local indicates that we are communicating with the local host as opposed to a host over the internet.
The last piece of information printed is the currently selected directory, often called the current working directory because it’s where you’re currently doing work in the computer. In this case it’s a directory named “1-communicating-ideas-is-difficult”, which stores all of the stuff that I needed to create the first article in this blog collection.
OK, so the terminal has given you a bit of information to help get you started. It’s clear which account you’re working under and you know which directory your commands currently apply to. If you start typing, text appears to the right of “$” and constitutes your command. This command is said to be in response to the command prompt your terminal is giving you. (Just to confuse you, the Windows terminal is also called the “command prompt”).
When I type ls,
I get this in return:
This is a list of each file and directory within my current working directory, which is named “1-communicating-ideas-is-difficult” (it’s a mouthful, I know — most directories have shorter names). ls stands for “list” and it lists the contents of the directory. ls prints the names of normal files in black and directories in blue. Image files are pink and executable files (i.e. programs you can run) are green. There are colors for other types of content, but that’s not too important right now.
What is important right now is pointing out that like most programs, ls has many different options. Options can be specified by typing things after the program name, like this:
-l stands for “long”, and as you can see below, it gives a longer and more detailed summary of each item in the current working directory:
That’s a lot of information, so let’s break it down. First, note that unlike when plain ls is run, ls -l prints each item on its own line. Each line is divided into columns separated by spaces, designed to be visually obvious (there are nine columns in the image above). The final column of each line contains the file name, but what are the rest of the columns telling us?
The first column looks pretty arcane, but it’s simply a very detailed explanation of who has permission to do what with each file. The r’s stand for “readable”, the w’s for “writable”, and the x’s for “executable”. Different kinds of users have different permissions, which is why there are many letters. We’ll talk more about this in a few posts from now.
You won’t always be interested in all of the columns. Take the second column: its meaning depends on whether the line describes a file or a directory. If it’s describing a file, the number in the second column indicates how many hard links refer to the file; if it’s describing a directory, the number indicates how many items are in the directory. Either way, you won’t necessarily care about that value — but that’s okay. The power of ls is its generality. You can find out anything you want about your computer if you know how to ask.
And most of the other columns are more useful. The two “jack” columns indicate two different kinds of ownership that I have over these files — the first “jack” refers to my username and the second refers to the eponymous group I belong to. The numbers to the right of the “jack” columns are the file sizes in bytes. If you use the -h flag, which stands for “human readable”, then the sizes are formatted more readably:
Programs like ls have so many options that they come with miniature user manuals. To see the manual page for ls, use the man program:
Entering this command will change your terminal screen to show you a description of ls, the list of options you can give it, copyright and author information, and more. You can navigate the page with the up and down arrow keys or page up and page down. To quit the program and return to the terminal, type ‘q’. Virtually every standard program on Linux includes a man page and the pages are usually very useful and comprehensive, albeit a bit dense.
This article would be too long if we examined each of the thousands of terminal programs, but we should introduce one more: cd. You might have noticed that while ls provides lots of information about your current working directory, it doesn’t provide any way for you to change it. That’s where cd comes in; it stands for “change directory” and “cd x” changes your current working directory to x, assuming x is a directory (otherwise cd will return an error, informing you that you can’t change directories to something that’s not a directory).
If you look back to the output of ls, you can see that there are no directories within our current directory — it’s a dead end. So let’s move up a directory by using the special name “..”:
Upon hitting the enter key, the working directory will change to the parent directory, changing the prompt accordingly:
“UCS” stands for Understanding Computer Science, the name of this blog. This directory contains a directory for every blog post, including the one we were just in:
If we wanted to go to the directory for the article you’re reading right now, we can move to it with cd and list its contents with ls:
There is much more to learn about the terminal and the next few posts will be dedicated to exploring and practicing how to use it. If CLIs seem clunky to you, don’t worry! There is a significant learning curve going from GUIs to CLIs, but I promise the journey is worth it. I actually prefer using a CLI to navigate around the operating system; I rarely find myself using the GUI file browser and I’m far from alone in this sentiment. Once you become comfortable with the terminal it becomes a shockingly efficient tool for manipulating your computer.
As a final selling point for CLIs, it will be a lot easier to follow my instructions when you can copy-and-paste the instructions directly into your terminal rather than having to follow a series of steps like “click the third icon, the one that looks like a paint brush, and then select the file drop-down menu…”
Image credit: fake terminal