Introduction to the Command Line

From LMU BioDB 2017
Jump to: navigation, search
  • For much of the computer work in this course, we'll be using a computer interaction style that's very different from what's familiar to most of us: the command line.
  • The reason for this is that the command line actually offers a surprising wealth of text processing tools, some of which are more powerful than the applications that we're accustomed to using.
  • It is clear, however, that one needs a lot more "up-front" training with the command line, and that's what this page hopes to provide.
  • To put things in context—everything “old” becomes new again: using the command line is ultimately a lot like “talking” to current digital assistants like Alexa, Cortana, and Siri, but with typing and reading instead of talking and listening.

Command Line Basics

Working with a command line is a cycle (i.e., a loop) of:

  1. The computer indicating that it is ready for the next command (via a prompt)
  2. The user (you) typing in a command
    • While typing a command, you may use the arrow, backspace, and other keys to edit what you've typed so far
    • To completely start over, hold down the control key then hit c (i.e., "control-c"); you'll go back to a fresh prompt
    • When you're ready with your command, hit the Enter or Return key
    • Many users experience some fear with typing a command—this is understandable, but rest assured:
      • Typically, the worst thing that happens is that the computer did not "understand" the command, thus doing nothing
      • While there are harmful commands, (a) the likelihood of your typing one at random is very low, and (b) most modern operating systems will prevent you from doing the really dangerous stuff anyway
  3. The computer performing the command, then showing you the result of that command
  4. "Rinse and repeat"

If all goes well, then with each command you type, you get closer to accomplishing your goal.

Which Command Line?

Although the general cycle is the same, at this point we should specify which command line we’re talking about. This matters because specific commands, features, and shortcuts may vary according to the command line you’re using. So, for this course, we are using the bash command line (sometimes called a “shell”). The commands that will be described are specific to bash. Other command lines exist, such as the venerable cmd on Windows and its newer sibling, PowerShell; the Linux operating system has many other command lines, such as sh, csh, and zsh. The macOS Terminal application defaults to bash and bash is also now available on Windows 10, so this wide availability is part of the reason for choosing bash as our specific command line environment.

For what it’s worth, short of the specific commands discussed, most of the concepts here—especially the ability to pipe commands and redirect output—are applicable across multiple command lines. Thus, the information on this page will largely transfer to other command lines with the exception of some command names. For example, on Windows cmd, the command for listing files is dir, not ls.

The Command History

To assist with this command entry cycle, modern command lines keeps track of the commands you type—essentially, a command history. This history shows up in a number of ways:

  • If you press the up or down arrow keys at the command prompt, you will move back and forth through your history. If you see the command you'd like to perform, press Enter or Return, and the computer will try to perform that command
  • You can do "variations" on past commands by immediately editing what shows up; that is, you can press the up or down arrow keys until you see a command that's similar to the one you want to do, then use the left/right arrow keys, backspace, and other keys to edit it
  • The control-c shortcut is always there if you want to start over

“Autocomplete” with the Tab Key

While working on the command line, the Tab key (the one to the left of the Q key) provides a convenient “autocomplete” function. No matter where you are in your command, it can be a good idea to tap the Tab key if you're in the middle of typing something out.

  • If the computer concludes that what you’ve typed in so far can mean only one thing, then it will “autocomplete” what you have by spelling everything out.
  • If the computer concludes that what you’ve typed in so far can still mean multiple choices, the first Tab will do nothing (though on some systems you may hear a beep); hitting Tab a second time will show you the computer’s “guesses” as to what you might be typing.
  • You can keep typing a few letters, then hitting Tab, until there is only one choice and the computer spells everything out for you. We’ll see instances of this later, in terms of the other commands.

In general, it’s a good idea to periodically type the Tab key, either when you can only remember the first few letters of a command or file name, or if you want to save some typing and know that you've typed in enough letters so that only one choice is available.

Your First Command: exit

The first command to try is:

exit

Typing this command ends your “command session” with the computer. Most of the time, this closes the window into which you’ve been typing your commands (typically called a “terminal”). Sometimes, you get a message that your session is finished, but you still need to close the window manually. In any case, exit means you’re done, and you can quit whatever program you were using to get to the command line (e.g., Terminal, PuTTY, etc.).

  • Since we just mentioned the Tab key, try this—type only the first two letters of exit:
ex
  • Now hit the Tab key twice. You should see a list of commands that start with ex, similar (but not identical) to this:
ex         exec       execsnoop  exit       expand     expect     export     expr       extcheck   
  • Now type the letter i:
exi
  • If you hit the Tab key twice again, you’ll see that the list would be narrowed down just to commands that start with exi
  • If there is just one command that matches what you’ve typed so far, the command line then “autocompletes” to that command—saving you some typing!

Of course, this might not be useful for a command as short as exit, but in any case this helps demonstrate the Tab key’s functionality.

Files and Folders on the Command Line

Before we move on into actual text processing commands, let’s look at some key concepts and commands for just “getting around” the files on a computer using the command line.

How Files and Folders Look on a Command Line

Just like with the computers we use everyday, we can access our files and folders on the command line. Unlike the computers that we use everyday, we don't see any icons, folders, or pictures; instead, command lines represent files and folders as text expressions called paths.

You've probably seen displays like the ones below on your computer. Click on them to see them full-sized:

Windows-icons.jpg    Windows-thumbnails.jpg Mac-folders.png

On a command line, the folders displayed by the windows above are expressed, respectively, as:

/WINDOWS
/Users/Public/Pictures/Sample Pictures
/Users/dondi/Documents

(some operating systems hide the Users folder from the window, and that’s why you might not see Users in the screenshot)

The way to read a path is to separate out the slash (/) characters; starting from the left, each slash marks off a folder that is inside another one. The very last item after the last slash represents the actual file or folder that is indicated by the path. Thus, you would interpret a path like:

/home/dondi/public_html

To actually mean the public_html file or folder inside the dondi folder inside the home folder, which in turn sits at the "top" or "root" of the file system—that is, it's a folder that isn't inside any other folder. As a tree (or an upside-down tree for biologists, since the “root” is at the top!), the path above looks like this:

/ (root)
|
+-home
   |
   +-dondi
      |
      +-public_html
  • If there is a folder called data within the folder above, its path would be:
/home/dondi/public_html/data
  • If there is a file called prokaryote.txt within the above data folder, then its path would be:
/home/dondi/public_html/data/prokaryote.txt

Your “Working” Directory: pwd

On everyday computers, we frequently see a window—typically when we're trying to save our work or browse our desktop—that gives us a concept of “where” we are among all of our files. While we don’t have such a window on the command line, the computer does keep track of what folder we’re in. This folder is called the working directory.

Terminology alert: The word directory is a synonym for what everyday computers call a folder. They mean the same thing—a folder or directory is an entity on the computer that keeps files, some of which may themselves be more folders or directories. For this write-up, folder and directory will be used interchangeably, to get you accustomed to seeing both words.

If, at any time, you forget "where" you are in your files, this command will display your working directory:

pwd

You'll notice that, because commands involve a lot of typing, they tend to be brief or even abbreviated. While this makes them harder to remember, they still resemble what they mean to do; for example, pwd stands for print working directory—which is exactly what that command does.

Your “Home” Directory

When you connect to a command line for the first time, the working directory typically starts at your user account’s home directory for that command line. This is the folder in which your user account is allowed to create files and otherwise do all kinds of other work. Most computers today have this concept; on Windows computers, your home directory is typically displayed as a folder icon with a name like Joe's Documents. On macOS computers, the home directory is typically displayed as a house icon with the same name as your login. Mobile devices don’t typically show files as much as personal computers do, but they’re there, with an implicit home directory for the mobile user.

Terminology alert: The word invoke is commonly used to mean “make the computer perform a command,” in this case by typing the command and hitting the enter or return key.

Getting Around

On everyday computers, you move from folder to folder by clicking or double-clicking the folder icon that you like. Some systems have a “folder up” button that you can use to move to the folder that contains your current folder.

The command line has similar commands, with the difference being that they are typed instead of invoked with a mouse.

List Files: ls -F

To see the files inside your current folder (i.e., your working directory), type this:

ls -F

Here, think of ls -F as “list files.” While the exact contents of your home directory may vary, ls -F typically produces something like this:

Desktop/    Downloads/  Pictures/  public_html/  Templates/
Documents/  Music/      Public/    sandbox.txt   Videos/

Each name is a file inside your folder—so it’s really a lot like the screenshots shown before, but without icons. Since we don’t have icons, how can we tell if a name represents another folder? That is indicated by the slash (/) at the end of the name. In the listing above, every entry except for sandbox.txt is a folder.

Note: The Bash on Ubuntu application on the lab computers start up in an empty folder/directory by default, so you won't see anything with just ls -F. Two alternatives:

  • Add -a to see all files: ls -aF —there are some files present that are not visible by default.
  • cd to a different folder (cd /usr/lib works) then invoke ls -F from there.

Change Directory: cd

Let’s say that you want to change your working directory to something else. The command for this is:

cd <new directory>

To remember this, cd stands for change directory. The <new directory> part in the command above is the name of the directory that you want to change to. Thus, if you want to “go” to the public_html directory/folder shown previously, you would type:

cd public_html

After doing this, type:

pwd

...to convince yourself that you did successfully go to the new directory; type:

ls -F

...to see what's in there (it might be empty).

What about that "folder up" button? That is indicated by the special name .. (two periods one after the other). To “go up” a directory, type:

cd ..

If you then type:

pwd

You should be back in the folder that contained public_html.

A few shortcuts are available when the directory you want is someone (or your own) home directory.

  • If you type cd by itself:
    cd
    ...you will “teleport” back to your home directory, no matter where you are.
  • If you type cd followed by ~<username>, where you substitute <username> for another account in the system:
    cd ~dondi
    ...you will “teleport” to that user’s home directory, no matter where you are. ~<username> (that first “squiggly” symbol is called a tilde) is the aforementioned shorthand for expressing a user’s home directory.

The Tab Key and cd

Recall that the Tab key offers an “autocomplete”-like feature on the command line. This totally works with the cd command. For example, if your home directory has folders named Documents and Downloads (and those are the only folders that begin with a capital “D”), typing this while on your home directory:

cd D

...then hitting Tab, will make the system automatically extend what you typed to:

cd Do

That’s because the only two choices that begin with D actually also begin with Do, so the system is “sure” that what you want at least starts with that. As before, if you hit Tab one more time, you’ll get:

Documents/  Downloads/ 

...since those are the actual possible choices for cd.

So remember, hit Tab early and often! 🤖

  • Note: The Tab key also works with ls -F, but we’ll skip over that for now.

Basic File Commands

There are lots of file commands, but for introductory purposes we’ll present the two basic ones which you have probably used in a non-command way already.

Copy File: cp

To copy a file from one place to another, use this command:

cp <file to copy> <destination of copy>

You have probably copied files before, such as from a laptop to a flash drive. In mouse/touch environments, this operation is typically a drag-and-drop—you hold down a mouse or trackpad on the icon of the file to copy, then drag it to the icon of the flash drive destination. Usually, the mouse cursor changes to show that you are about to perform a file copy (a common sign is the appearance of a “+”). In addition, there is a safety net that warns you if you are about to copy a file into a destination where a file of the same name already exists.

In Windows, you can also click on an icon to select the file, choose Copy from a menu, then navigate to the destination and finally click Paste. Note the similarities in the pattern, regardless of the specific mechanism: indicate the file to copy, then indicate where the copy should go. If you think of the cp command in this way, that may make the learning go more smoothly.

In the end, though, there are some differences to note:

  • Already mentioned is the way we are used to receiving warnings if we are about to copy over a file that already exists. The command line does not do this by default. If you would like to play it safer and be given that warning, add a -i to the command, separated by spaces:
cp -i <file to copy> <destination of copy>
The “i” stands for “interactive,” which may make it easier to remember.
  • Don’t forget, you can use various commands in any order. You can use ls or cd at any time to get a handle on what files are around and where. After performing a copy, you can use those commands again to make sure that the file really did get copied. Part of getting used to the command line is the ability to string individual commands together in a meaningful way.
  • We are accustomed to copying files to a folder or directory, and indeed cp can work that way. However, it does have one other option which might not immediately be obvious in other user interface styles: you can copy a file and give the copy a new name in a single command. For example, if you want to copy a file called genetic-code.sed from the ~dondi/biodb/data folder into your home directory but want to rename the copy as gc.sed, you can do this in one line:
cp ~dondi/biodb/data/genetic-code.sed ~/gc.sed
(remember the ~ shortcut for home directories above)
  • Finally, the .. shorthand for the folder “above” the current one still holds here. That is another pattern to realize about the command line: the “vocabulary” of shorthand and symbols typically apply across whole families of commands. Thus, once you learn how to use one command well, chances are that you are a leg up in learning other related ones.

Move File: mv

Sometimes you don’t want to copy a file, but just move an existing one from one folder to another. The command for this is mv (“move”), and its structure is very much like that of cp:

mv <file to move> <new location of the file>

Note again the conceptual similarity between this command and what you may be used to (i.e., drag-and-drop of a file icon from one place to another; selecting a file, choosing Cut from a menu, then choosing Paste at the file’s new location): all commands indicate the file to move, then the destination of the file. It’s just how you express this that differs for each mechanism.

Most of the bullet points for cp also apply to mv, including the -i safety net option (remember what we said about having that shared “vocabulary?”). Plus, mv has one last twist of its own...

Rename File: mv (!)

Yes, the command to rename a file is also mv. That’s because the command line does not distinguish between a move and a rename—renaming a file is simply “moving” it from a file of one name to a file of another. Thus, renaming a file is:

mv <old name of file> <new name of file>

Note how this reflects a certain minimalism or non-redundancy in how commands are defined—instead of creating a whole new command for some operation, if another command effectively does the same thing, then the choice is to use that instead of defining another one.

View File: cat

Since command line interaction does not have floating, separate windows, viewing a file has to happen within the command line window. There are two commands to do this; the first one, cat, is the simplest way to display a file:

cat <filename>

You replace <filename> with the name of the file that you’d like to display. For example:

cat expasy.html

Invoking this command would display that file’s contents to you, and in the case of a large file, you’d see everything scroll by really quickly!

For large files that trigger a lot of scrolling, merely enlarging the command line window won’t necessarily help: one can always come up with a text file that is bigger than the command line window. We’ll deal with that in the next section.

  • As you might have guessed, Tab autocompletion works with cat. You can type:
    cat exp
    ...and the system autocompletes to whatever file(s) begin with the letters e x p.
  • In case you were wondering, cat is short for concatenate. The reason for this name actually has to do with other things that you can do with cat, which we will skip for now.

“Paging:” more or less (really!)

Fortunately, there is a command that takes the size of the command line window into account, and will allow you to look at a text file a screenful at a time. This command is more (as in, it waits for you before it displays “more” information). On some systems, someone thought they were being clever by also making a program called less available, which does the same thing. They might have been thinking that “less is more.”

more (or less) takes the same information as cat; namely, a filename:

more <filename>

or

less <filename>

For files that span more than one window’s worth, you initially see just what fits in that window. The bottom of the command line window then says:

--More (0%)--

This indicates that you haven’t seen the entire file yet, and that you’re around 0% of the way down.

From here, pressing some keys allows you to move around the file:

space bar Moves forward a command line window’s worth at a time
enter or return Moves forward a line at a time
b When viewing files, moves backward a command line window’s worth at a time
q Quits from more or less
  • After you quit from more (or less), don’t forget that the up and down arrow keys allow you to look at previously-typed commands. Thus, if you want to look at the file again, don’t bother typing the whole command over—just hit the up arrow key, then press enter or return, and you’re back in more, paging through that file.
  • more and less actually have quite a few, well, more features while paging through a file. These will have to wait for another time.
  • more and less are called paging programs, since they allow you to “page” through large files.

Putting Commands Together

There are additional commands to cover, but for now we have enough to look at perhaps one of the most powerful aspects of a command line interface: putting multiple commands together. This ability lets you let one command work off the results of another command, producing a classic example of “the whole is greater than the sum of its parts.”

The “Anatomy” of a Command

Think of a command as a box, with text coming in one side, followed by text coming out another:

Structure of a single command.

Well, text is text, right? What’s stopping us from using the output text of one command and making it the input text of another one? The answer is, nothing. This can be done:

Multiple commands “piped” together.

If you think about the text data “flowing” from one command to another, with each command performing some processing on each successive stream of text, you’ll see the origin of the word for this activity: piping. We are essentially forming a pipe through which text “flows,” and at each stop, the text changes a little bit, depending on what the specific command does.

Creating a Pipe on the Command Line

A single character is responsible for creating a pipe: |. We typically call this the “vertical bar,” but in this context, it is called the “pipe character.” Let’s start with an example.

First, let’s learn a few more seemingly trivial commands. These are useful for learning how commands can string together. Later on we will see additional commands that do some real work, whose capabilities multiply when used together.

Say What I Say: echo

echo is very simple—it repeats whatever you type into it:

echo "Hello world"

…will make the command line display Hello world (without the quotes). We will use this command to create very simple pipes for now; you’ll see later that you can then substitute echo for other commands that produce data to process.

Reverse What I Say: rev

The rev command is also quite simple, and it does what its abbreviation implies: it reverses whatever data it is given. How do you give it data? One way is to just type it in:

rev

...whereupon typing the text “Oh say can you see” and “by the dawn's early light” (hitting Enter after each phrase) results in:

Oh say can you see
ees uoy nac yas hO
by the dawn's early light
thgil ylrae s'nwad eht yb

Type Control-d on a blank line to tell any command that there’s no more input data to process, and you’ll be back on the command line.

So that’s rev in isolation. However, the very point of this section is to be able to use commands in conjunction with each other. Here, finally, is your first piped command:

echo "Oh say can you see, by the dawn's early light" | rev

Even though it doesn’t look much different from the “type as you go” style, the implication here is more powerful: rev doesn’t care what comes before the pipe. Thus, you can also say:

pwd | rev

Or better yet:

ls -F | rev

You might need to stare at these for a bit to fully grasp what’s happening.

Go ahead, take a moment.

That’s pretty much all there is to it—if you think of the output of one command as being a possible input for another, just stick a | in between the two commands, and the data will flow through them, getting processed as they go.

Sending Data to a File

When processing data with commands, there is always a terminus: a point where the data is the way you want it. So far, that terminus to us has simply been the screen—we see what our commands have produced and leave it at that. In some cases, however, you want to store this data—i.e., save it in a file. This is quite easy to do on the command line; it is called output redirection and is triggered by the greater-than symbol (>), here used for its arrow-like visual appearance rather than its mathematical meaning:

pwd > current-directory.txt
ls -F > file-list.txt
echo "Hello world" | rev > reversed-hello.txt

Invoking these commands will not display the results on the command line; instead they will create the files specified after >.

Just be careful with the filename (and/or path) that you specify: if files with those names were there before, their content will be completely replaced by the output of the command chain.

Sending data to a file will come in particularly handy when we access web servers from the command line. That way, instead of staring at a flood of code scrolling across the window, we can put this code in a file, for examination at our leisure by an appropriate application.

Concluding Summary

This is a lot to digest at once, so here are the main points to take home:

  • Command line interaction is a cycle of typing in commands then viewing their results
    • Assorted shortcuts, like a history and the Tab key, try to minimize the pain of constant typing
  • Files and folders/directories are available on the command line just like on any other computer, but are just expressed and accessed without graphics
    • Files and their locations are represented as paths
    • At any time, there is one working directory
    • pwd displays the working directory, while cd changes it
  • Commands can be “strung together” in a pipe, thus enabling multiple operations on the same data stream
    • The vertical bar character ( | ) is used to indicate that one command should “pipe” its output as input to the next one
    • The greater-than symbol (>) is used to redirect the output of a command sequence into a file or path that you specify

Additional Material

Interested in deepening your command line fu? Additional handouts can be found here: