Using The Terminal

This page gives some basics for using the Terminal in Ubuntu, and the BASH shell. There isn't much in the way of specific commands, but the information in here will be useful as you move on to learn commands.

Introduction

When you're working with the command prompt, you're working with a level of Linux that just understands simple text-based commands. Using a graphical program like Nautilus, you would double-click a folder to see what that folder contains, whereas from a command prompt you would do the same thing by typing a command like ls and pressing Enter.

However, although it may not be obvious, when you type these commands in, you're not talking directly to Linux; there is actually an intermediary program in between, called a shell, which takes your commands and interprets them, before passing them on to the operating system. There are numerous “shells” you can use, but the one used by default in Ubuntu is called BASH1. So when you use the Applications->Accessories->Terminal menu, you're actually using BASH2, as pictured below.

terminal.png

The Command Prompt

You may notice that it says serna@i5015ubu:~$ in the screenshot above. This is the command prompt.

Yes, technically, these characters that come before the cursor are what is called the “command prompt.” However, people also use the term “command prompt” in a more general way, to refer to the Terminal, or the shell. If someone tells you to “pull up a command prompt,” they're asking for you to open the Terminal.

Just to add to the confusion, people also use the term “command line,” in addition to “command prompt.” So instructions that say “enter this at the command prompt,” and instructions that say “enter this at the command line,” mean the same thing.

Let's break down the command prompt, and see what it means:

  • First off, it says serna, because that's the username I was using at the time. This is followed by the @ character, and then i5015ubu, which is the name of the computer I was using at the time.
    • Your command prompt will be different, but it will be in the format username@computername.
  • After this comes the : character, and then the current working directory. In the screenshot above, that's ~, which is a shortcut for my home directory.
  • Finally comes the $ character. After that, you would type in your commands.

You will notice that the command prompt changes, as you navigate to other directories. For example, if I were to go to the /etc/X11/ directory, using the cd command, the prompt would change accordingly:

serna@i5015ubu:~$ cd /etc/X11/
serna@i5015ubu:/etc/X11$

For the examples on this site, I don't usually show the entire command prompt; I normally just put $.

By the way, the $ character does have a meaning; it means that you're logged in as a normal user. If you were logged in as root, the command prompt would use the # character, instead of $. (For more information on the root user, see the Security page.)

Keyboard Shortcuts

Here are some simple things you can do with the Terminal window, to make things easier.

Key(s) Description
Up and Down arrow keys Used to scroll through a list of previously entered commands. Discussed in detail below.
Tab Used for auto-completion. Discussed in detail below.
Left and Right Arrow keys When you're typing, you can move your cursor to the left or right using the left and right arrow keys; if you make a mistake, you don't have to Backspace back and delete everything you've typed until you find the mistake; you can simply use the left arrow key to go back, fix your mistake, and then use the right arrow key to get back to the end.
Home or Ctrl+a Moves to the beginning of the line
End or Ctrl+e Moves to the end of the line
Ctrl + right arrow Move cursor forward one word
Ctrl + left arrow Move cursor back one word
Ctrl+u Delete everything from the cursor to the start of the line
Alt+l Make every character lowercase, from the cursor to the end of the currently selected word
Shift+Page Up and Shift+Page Down Scrolls the output up and down, just like the Page Up and Page Down keys normally do, in programs that support scrolling
Ctrl+d End the current program. If you're simply at the command prompt — meaning that there is no program currently running — Ctrl+d will close the terminal window.

In addition to this, if you have copied something, and want to paste it into the command window, you can right-click and choose Paste. Or, if you have a middle button on your mouse (e.g. a scroll-wheel), you can click the middle button, to paste3.

If the terminal ever gets too cluttered, with all of the commands you've entered, you can use the clear command, to clear the screen. Nothing will be left on the screen except for one command prompt.

The Up and Down Arrow Keys

You will find, when you're using the command line, that there are some commands you end up using over and over again. Luckily, BASH makes it easy to re-execute a command that you've used recently.

From the command line, if you press the up arrow key, BASH will show you the previous command you entered; you can simply hit Enter, to execute it, or you can edit the command, to modify it before you execute it. If you keep hitting the up arrow key, BASH will keep going back through commands you have recently entered. Once you've started using the up arrow key, like this, you can use the down arrow key to go back the other way. If you keep pressing the down arrow key, you'll eventually end up back at an “empty” prompt, with no command.

If you would like to see a list of all of the commands you've entered (up to BASH's limit, whatever that might be), you can use the history command.

The Tab Key

Another very useful key when using BASH is the Tab key. Tab is used for autocomplete; for example, if you want to edit the /etc/x11/xorg.conf file, you could type in the following:

$ gedit /e|

For these examples, the | character represents the cursor's current location.

If, at this point, you were to hit Tab, BASH would try and complete what you have typed; it knows that you're looking for something off of the root directory, that starts with e. On my system, there is only one directory which matches that, which is /etc/, so BASH will fill it in for me, and the command prompt will now look like this:

$ gedit /etc/|

If there had been more than one file/directory that matched what I'd typed so far, BASH wouldn't be able to fill it in. But in this case, there was only one directory that started with e off of the root folder.

If I were then to type an X, and hit Tab again, BASH would look for a file/directory which matches it, and, again, on my system there would only be one that matches, which is X11, so it would fill that in for me too.

$ gedit /etc/X11/|

Note that Linux is case-sensitive, so if I were to type x instead of X, it wouldn't work.

We're almost there, all we need now is the name of the file, xorg.conf. However, if I just type the letter x and Tab, it won't be enough for BASH to figure out what I'm looking for; there is more than one file in that directory that start with x. However, I could hit Tab twice, BASH will give me some suggestions, like this:

$ gedit /etc/X11/x
xinit/         xorg.conf      xorg.conf.old  
xkb/           xorg.conf~     xserver/       
$ gedit /etc/X11/x|

These examples used the Tab key to autocomplete file and directory names, but it can also be used to autocomplete Linux commands, as well. If there is a command you use on a regular basis, you can try to start typing it, hit the Tab, and see what you get. Maybe BASH will know the command you're trying to enter, and maybe it won't. (Maybe you'll have to type another letter or two, before it can disambiguate it.) Then again, many Linux commands are so short, like rm and cd and ls, that you can probably type them in easier than using the Tab key.

Closing the Terminal

When you're done with the Terminal, you can click the X at the top of the window, just like you can for any other application. Or you can use the exit command or press Ctrl+d, which will close the Terminal.

This may seem pretty trivial; does entering exit really save anything over simply clicking the Close button? The fact is, when you get used to using the command prompt, you won't want to have to move your hands off of the keyboard, to the mouse, so you may find that it actually is useful.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.