Find part one here.
Let’s continue with familiarizing ourselves with how the shell works.
In the previous article, we talked about the ls command and using it to list files in a directory. One question I received about this was: what happens to that list after it’s printed?
An important thing to keep in mind is that most of the commands we are dealing with at this point are not interactive. This means that they do their job and then quit. In the case of ls, the program’s job is to spit out a list of the current files in the directory, and then it’s done: we move on to other commands. I’d liken it to requesting information by mail: you send a letter with your request, and some information is sent back to you. If that information changes in the future, you won’t know that unless you receive another letter informing you of it.
Let’s do an experiment to test this principle. In so doing, we will also learn an important Unix concept: redirection.
Open up your terminal. We’re going to create a directory to play around in.
We make a directory with the mkdir program. This name, although abbreviated, should make sense. The mkdir program needs to know what we want to call our new directory, so we pass it an argument to give it this information. (If you don’t remember what “passing an argument” means, read the previous article again.) You can call your new directory almost anything: however, let’s try to stick to letters and numbers and dashes (including spaces or special characters has some special rules involved that we don’t want to get into just yet).
mkdir testing-ground
The above example would make a directory called “testing-ground”. Where does it make it? Well remember, in the world of the shell, you are always located somewhere. As we mentioned in the last article, most shells will tell you your current location in the prompt (see this picture for a reminder of where your location is displayed). If we run the mkdir command as shown above, the “testing-ground” directory will be created inside our current location. If we are in the location “~/Documents”, the location (or path) of our new directory will be “~/Documents/testing-ground”.
What location do I start in? Upon opening a new terminal, most shells start you in your home directory. This is a special place that belongs to your user: you own it, and you can create and delete things inside it at will. Though Microsoft Windows actually does now have the concept of a home directory, many people either don’t use it, or don’t know they are using it. This is because on a standard Windows system, users and programs don’t think about permissions. There’s nothing stopping you from deleting something important, because you have the permission to do anything you want.
On the other hand, on a Unix system like Linux or OS X, if you try to delete a file that is important to the entire system, you will be denied with a stern message. Serves you right.
The “home directory” is so common in Unix systems, that shells provide a handy abbreviation for it, so that you don’t have to type out its full name: ~. If you look at that picture again, you’ll see my location is “~/Documents”–in other words, I’m located in the Documents directory inside my home folder.
Alright, so we issued the mkdir command. Now we need to go inside of it! To do so, we will use the main method of changing our location: cd. This stands for “change directory,” and indeed that is exactly what it does. The cd program also usually needs to know where we want to go: in this case, we want to go to our newly-made directory, so we pass it an argument with the directory’s name:
cd testing-ground
We are now in the new directory. Let’s see what’s inside it, by doing our familiar ls command:
ls -l
It tells us that the directory is empty; this should make sense, since we only just created it!
Let’s put something inside of it. How are we going to do this? Well, we could copy a file from somewhere else, or we could move a file from somewhere else, or we could create a new file altogether.
Let’s make a new, empty file by doing the following:
>jordanrules
Using a > to make a new file might seem weird. Don’t worry, we’ll explain it in a moment.
Let’s ask our friendly friend ls to list the files in the directory again, and see if it sees our new file:
ls -l
Sure enough, jordanrules is now showing up!
The echo command. Let’s take a moment to play with the echo program. It’s one of the most simple programs: you tell it something, and it tells you that same thing back! Indeed, it’s aptly named, since it’s like talking when there’s an echo: your echo will repeat exactly what you said. Let’s try it:
echo hello
echo echo echo echo
echo RENEW!
Useless? Not at all! This is the most important part of this article. If you understand nothing else from this rambling, understand the following thing:
Commands can be chained together.
Let’s say that one more time, for emphasis:
Commands can be chained together.
We are going to do our first “cool thing” in the Unix shell now, by chaining the echo command, which we just played with.
echo hello >jordanrules
So now we come to what that > sign really means: it’s one way of chaining commands! What we just did, was chained the result or output of the echo command and put it into the “jordanrules” file.
Don’t believe me? Let’s open the file and check!
There are lots of ways to open a file. Let’s use the program less to do it. Type:
less jordanrules
Sure enough, we see that the file now contains the word “hello”. (Press q to get out of the less program)
Let’s do this again, with another phrase:
echo blah blah blah >jordanrules
Now check the file again:
less jordanrules
That’s interesting! Our old word, “hello,” is gone entirely, and the file now only contains “blah blah blah”. What if we didn’t want to replace the file’s contents, but just add to it?
We can use a different chaining operator for that. (The term “operator” comes from math, where operators are things like + and -.) Here’s an example:
echo cheerio >>jordanrules
Now open the file with less again:
less jordanrules
Sure enough, the file contains what it did before, plus our new word. Neat, eh?
I know what you’re thinking. “What if I wanted to put the phrase ‘jordan > you’ in the file?” Obviously, we can’t just type echo jordan > you >jordanrules because the > sign is special! The shell would think we are trying to chain the echo command into a file called “you”. But don’t worry, the solution is easy:
echo 'jordan > you' >jordanrules
See what we did there? We put the phrase in single-quotes. In the shell, anything you enclose in single quotes is treated “literally”–this means you can use special symbols etc. in your phrase, and they won’t be treated specially.
For this reason, it’s a good habit to always put phrases that you give to echo inside of single-quotes, at least for what we are using it for now.
Anyway, feel free to play around with creating different files, putting stuff in them, making new directories, and moving around with cd. We’ll talk more about other commands (and of course, more about chaining) in the next article!
Comments are closed.