So what is this CLI thing anyway?

by Peter Kelly (critter)

These tutorials are based on a basic, fully updated installation of PCLinuxOS 2009.2 using the KDE 3.5 desktop. There may be minor differences between different installations and desktop environments but the fundamental concepts are similar.

Most people who are new to Linux are often confused when seeking help and are told: "In a terminal type..." or "This can be done using the CLI ...".

Terminal? CLI? Console? What does it mean? Well in this context the three terms can be used interchangeably. CLI is short for command line interface and is another way of interacting with your computer, as opposed to the usual GUI or graphical user interface found in desktop environments like KDE or Windows.

So why bother? Well for the most part, you don't have to bother. The GUI will do almost everything that anybody could possibly need on a day to day basis. The keyword here though is almost. There are times when you need to do something that there is no button to click or menu item to select to perform it.

Anything you do in a GUI can be done using the CLI, plus a whole lot more. The down side is that you have to learn what commands to type. Not all commands, there are after all hundreds of them, but a basic working knowledge of a few dozen of the more popular ones will make you a proficient CLI user.

Getting started

Want to dip a toe in the water? Follow me.

pic1

Open PCLinuxOS control center.

Under the System heading click "Manage users on system" and create a new user, call it whatever you like - my user is called jane, log out and then log in as the new user. Now, if we manage to screw up, delete the user and re-create it. No damage done, your account is safe.

Click the menu button and under System->Terminals select Konsole - Terminal Program. You could select anything under that menu but this is the most suitable for what I have in mind. A window opens with a black background and something like this:

pic2

This is called the prompt, because it is prompting you to type something. It is also giving some information: The current user is jane who is at a computer with the "hostname" home (If you haven't changed the hostname of your computer your prompt will say localhost). The ~ character (called tilde) is shorthand for "my home directory" which is where jane is in the filesystem. The $ denotes jane is an ordinary user not the super-user root, whose prompt usually ends with a hash #. This is the default in PCLinuxOS but can be changed to include almost anything you want, for example, some people like to have the current date in the prompt.

Looking around

So the prompt tells us that jane is in her home directory. Let's change that. Type the following and press enter:

cd /usr/bin

Make sure that you use only lower case letters, as Linux is case-sensitive.

The prompt now looks like this:

pic3

The command cd means change directory and the /usr/bin part is telling the command which directory to change to ".". But the directory shown in the prompt says bin not /usr/bin and there are lots of directories named bin in the Linux file system.

It is very important that we know exactly where we are if we are going to start mucking about with things - no nice graphics here. To check, type in the command pwd, short for "print working directory", and press enter.

pic4

This confirms where we are and supplies a new prompt ready for you to type the next command. Let's go back, type cd and press enter (you know to press enter by now) Typing the command cd on its own is a quick way of getting home from anywhere in the file system.

Nothing very impressive so far, and I said that you can do anything in here just like in a GUI. You can.

Type firefox. See, you still have access to all of your graphical applications while you are running a terminal in KDE. But now you can't type anything on the command line so close firefox and you get the use of the command line again in your terminal (or console if you prefer). I'll show you later how to run a command and still have access to the command line.

Let's have a look around in this directory.

Type ls (it means list directory contents, in this example the contents of janes home directory)

pic5

This is a very brief and not very informative listing of the directory contents. If we want more information, then we can use a modified version of the ls command. For example, type ls -l (notice the space). Now type ls -a. See the difference? The -a and -l are known as options, and most Linux commands accept a host of options.

The -a (all files) gives you fewer details but more files. By default any file that begins with a period is hidden, this is nothing to do with security but merely a convenience to reduce clutter.

The -l option means provide a long listing. You can even combine options to get the results that you want.

Try ls -al.

How do you remember these options? You don't have to. For almost any command, typing the name of the command followed by --help will show you what is available. In practice you will use only a few of the available options.

Type ls --help.

That went past too quickly to read so we need to slow it down.

Type ls --help | less

The less command shows less information in one go.(The | is the vertical bar character above the backslash character. At least that's where it is on my keyboard) I'll explain later what this is all about. You can press enter or the space-bar for a new page and the page up / page down keys work. Press 'q' to get out.

To summarize then, Linux commands take the following general form: {cmd} {options} {argument}

  • cmd is the name of the command e.g. ls
  • options such as -a or -al are ways of modifying the output or result of the command and are usually prefixed with one or more dashes.
  • argument is information that you supply to the command, such as a directory to go to like /home.

Armed with the preceding information you are ready to go exploring.

Logged in as a normal user you, can do no system damage with the following commands, which merely display information or move your current position within the filesystem. Some of the information displayed may look a bit cryptic at the moment, but I will explain it all when we have been through a few more commands.

ls
cd
pwd
free    display memory usage
df      display disk usage
date    print the time and date to screen
cal     print a calendar on the screen

Making changes

This is where we have to be a little more careful, as we will be making changes to files and directories and deleting some. But that is why we created this dummy account. (You did that, right?)

cd enter puts us back in the home directory from wherever we were in the file system.

Create a new directory named mydir1 with the command mkdir mydir1. Check that it has been created with the ls command.

Now let's create a new file. One way to do this is to use the command touch.

touch myfile1

This isn't what touch was designed for but is what it is used for mostly these days. ls will show the new file but it isn't in our new directory so let's move it.

mv myfile1 mydir1
ls                   The file has gone.
ls mydir1            There it is in our new directory so move to our new directory.
cd mydir1
file myfile1         Shows the file type to be empty.
pic6

Remember the vertical bar character that we used with the less command? It took the output from the ls -help command and fed it to the less command so that we could read it in our own time. The > (greater than) character can be used in a 'similar' way.

ls -l /                will give a listing of the root of the file system.
ls -l / > myfile1

Here the output is captured before it is printed to screen and then stuffed into our file. This is part of something called redirection, which we will go into in more depth in a later episode.

file myfile1 now shows the file to be of type 'ASCII text'. To look at the contents we can use the command cat (short for concatenate).

cat myfile1

pic7

I hate typing in long commands

So do most people. You may have noticed that Linux commands tend to be short (e.g. ls, cat, df, cd, rm). Linux has its' roots in Unix, an operating system dating back 40 years, to the days when there was no graphical interface and all work had to be done on the command line. Over the years people have developed many ways to reduce the amount of typing and speed up regularly performed tasks, as you will discover.

When you run a terminal session, such as Konsole, under Linux (notice how the terms terminal, console and CLI are blending together), you are actually running an application known as a shell. The shell takes what you type and interprets it into something that the computer can work with. There are lots of shell programs available for Unix/Linux. The original shell was known as the Thompson shell or sh on the command line. This was developed by Stephen Bourne to include many spelling correction -mma aved 9/20/09 3:59 PM more features but was still known as sh. The most popular shell today is called 'bash' (bourne again shell). The default shell in PCLinuxOS is bash. You can check this by typing echo $SHELL on the command line.

Bash is a very powerful program that can save you no end of typing. Press the up arrow a few times and you will see the last few commands that you typed at the prompt ready to be edited or executed. The down arrow takes you back down. Type history and you get a numbered list of all the commands that you have used in this session.

Type "!(The number)" enter e.g. !38 and that command is echoed to the screen and then executed.

Another time saver is a feature known as 'command line completion'. It works like this:

Type hi press the tab key.

You get prompted with a list of all the commands you can execute that begin with the letters 'hi'.

Press the 's' key and tab and the command line completes the command history waiting for you to add any options or arguments then enter to execute the command. This not only saves on typing but serves as a reference when you can't quite remember the command and cuts down on typing errors.

If you are part way through typing a command and notice a spelling mistake, you can use the following key combinations to get to it:

ctrl + a takes you to the beginning of the line.

ctrl + e takes you to the end of the line.

alt + b takes you to back a word.

alt + f takes you forward a word.

There are more, but these few are easy to remember and are usually sufficient.

If you have previously typed a long command and don't want to have to re-type it, then ctrl-r starts a reverse search - just start typing the command and as you type, the shell will match what you type to the most relevant previous command. When you have an exact match just press enter.

Type a letter, any letter e.g. 'a' then press tab and return.

pic8

Answer 'y' and press return.

That gives you some idea of the number of commands we have to play with (almost 2000 in a basic installation of PCLinuxOS 2009.2). Don't worry. You will only ever need a very small number of these commands but, if you need to do something, then there is a command or set of commands that will do it for you.

So you've dipped a toe in the water, how does if feel? Hopefully not too bad, because next time we are going to wade in waist deep, doing some things with root permissions. That is where the command line comes in to its own.