Basic commands

By now you have some basic knowledge about directories and files and you can interact with the command line interface.  We can learn some of the commands you'll be using many times each day.

ls

The first thing you likely need to know before you can start creating and making changes to files is what's already there? With a graphical interface you'd do this by opening a folder and inspecting its contents. From the command line you use the program ls instead to list a folder's contents.

$ ls
Desktop  Documents  Music  Photos

By default, ls will use a very compact output format.  Many terminals show the files and subdirectories in different colors that represent different file types.  Regular files don't have special coloring applied to their names.  Some file types, like JPEG or PNG images, or tar and ZIP files, are usually colored differently, and the same is true for programs that you can run and for directories.  Try ls for yourself and compare the icons and emblems your graphical file manager uses with the colors that ls applies on the command line.  If the output isn't colored, you can call ls with the option --color:

$ ls --color

man & apropos

You can learn about options and arguments using another program called man (man is short for manual) like this:

$ man ls

Here, man is being asked to bring up the manual page for ls. You can use the arrow keys to scroll up and down in the screen that appears and you can close it using the q key (for quit).

You can also use man to learn about new programs.  Let's say you want to rename files using the command line but you don't know the name of the program that you can use for that.  You can use another program called apropos for that, like this:

$ apropos rename
...
mv (1)               - move (rename) files
prename (1)          - renames multiple files
rename (2)           - change the name or location of a file
...

Here, apropos searches the manual pages that man knows about and prints commands it thinks are related to renaming. On your computer this command might (and probably will) display more information but it's very likely to include the entries shown.

Note how the program names include a number besides them.  That number is called their section, and most programs that you can use from the command line will be in section 1.  You can pass apropos an option to display results from section 1 manuals only, like this:

$ apropos -s 1 rename
...
mv (1)               - move (rename) files
prename (1)          - renames multiple files
...

At this stage, the section number isn't terribly important. Just know that section 1 manual pages are the ones that apply to programs you use on the command line. To see a list of the other sections, look up the manual page for man using man man.

mv

Looking at the results from apropos, that mv program looks interesting. You can use it like this:

$ mv oldname newname

Just as the description provided by apropos suggests, this program moves files.  If the last argument happens to be an existing directory, mv will move the file to that directory instead of renaming it.  Because of this, you can provide mv more than two arguments:

$ mv one_file another_file a_third_file ~/stuff

If ~/stuff exists, then mv will move the files there.  If it doesn't exist, it will produce an error message, like this:

$ mv one_file another_file a_third_file stuff
mv: target 'stuff' is not a directory

mkdir

How do you create a directory, anyway?  Use the mkdir command:

$ mkdir ~/stuff

And how do you remove it?  With the rmdir command:

$ rmdir ~/stuff

If the directory you wish to remove is not empty, rmdir will produce an error message and will not remove it.  If you want to remove a directory that contains files, you have to empty it first.  To see how this is done, we will need to create a directory and put some files in it first. These files we can remove safely later.  Let's start by creating a directory called practice in your home and change the current working directory there:

$ mkdir ~/practice
$ cd ~/practice

cp, rm & rmdir

Now let's copy some files there using the program cp.  We are going to use some files that are very likely to exist on your computer, so the following commands should work for you:

$ cp /etc/fstab /etc/hosts /etc/issue /etc/motd .
$ ls
fstab  hosts  issue  motd

Don't forget the dot at the end of the line!  Remember it means "this directory".  As you can see, you have to give cp a list of files you wish to copy and a destination.  Now, if you go back to your home and try to remove the directory called practice, rmdir will produce an error message:

$ cd ..
$ rmdir practice
rmdir: failed to remove 'practice': Directory not empty

You can use the program rm to remove the files first, like this:

$ rm practice/fstab practice/hosts practice/issue practice/motd

And now you can try removing the directory again:

$ rmdir practice 

And now it works, without showing any output.

But what happens if your directories have directories inside that also have files, you could be there for weeks making sure each folder is empty! The rm command solves this problem through an option called -r, which stands for "recursive." In the following example, the command fails because foo is not a plain file:

$ rm foo/
rm: cannot remove `foo/`: Is a directory

So maybe you try rmdir, but that fails because foo has something else under it:

$ rmdir foo
rmdir: foo: File exists

So you use rm -r, which succeeds and does not produce a message.

$ rm -r foo/

So when you have a big directory, you don't have to go and empty every subdirectory.

But be warned that -r is a very powerful argument and you may lose data you wanted to keep!

cat & less

You don't need an editor to view the contents of a file. What you need is just to display it. The cat program fits the bill here:

$ cat myspeech.txt
Friends, Romans, Countrymen! Lend me your ears!

Here, cat just opens the file myspeech.txt and prints the entire file to your screen, as fast as it can.  However if the file is really long, the contents will go by very quickly, and when cat is done, all you will see are the last few lines of the file. To just view the contents of a long file (or any text file) you can use the less program:

$ less myspeech.txt

Just as with using man, use the arrow keys to navigate, and press q to quit.

Manipulating processes

Processes are programs in action. Programs in binary/executable form reside on your disk; when they are executed, they are moved into memory and become process. Each and every program we run is a process.

Interrupting (CTRL-C)

Any process you are running in your shell can be stopped at any point while it is still running by pressing the CTRL and C keys together. This will shut the process off almost immediately and you may lose data in any file it is writing (but it won't leave very important stuff half finished, like closing files).

ps and kill

We can use the ps and top commands to view processes running on our machine.

The ps command, when you run it without any arguments, will display processes run by the current user.

$ ps
 PID TTY          TIME CMD
 3922 tty2    00:00:00 su
 3923 tty2    00:00:00 sh
 3941 pts/0    00:00:00 cat
 3942 pts/0    00:00:00 ps

Here we find there are 4 processes that we are running from our terminal. The 4 columns  are interpreted as follows.

Process ID   Terminal   CPU Time   Program/Command 

Each process has an identifier by which the operating system tracks it. This is an integer number that is given to each new process, and is called the PID (for "process ID"). The gap between the PID 3923 for sh and the PID 3941 for cat merely shows that somebody started processes on the machine in between the times these two processes started.

The second column in the output of ps specifies the terminal to which the process is attached or the terminal that controls the process. You can use the tty command to find out which terminal you are presently in.

$ tty
/dev/tty2

Now, you may well expect that your machine has a lot more processes than the ones you see runing a simple ps without arguments. In fact, it shows only the processes you started from the terminal in which you issue the command. ⁞ On a graphical desktop, that command doesn't show the programs you start from menus or by clicking on icons. The system also runs a lot of its own processes in the background. To see everything, add the -e option:

$ ps -e
  PID TTY          TIME CMD
    1 ?        00:00:01 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 migration/0
    4 ?        00:00:00 ksoftirqd/0
    5 ?        00:00:00 watchdog/0
    6 ?        00:00:00 migration/1
    7 ?        00:00:00 ksoftirqd/1
    8 ?        00:00:00 watchdog/1
    9 ?        00:00:00 events/0
   10 ?        00:00:00 events/1
   11 ?        00:00:00 khelper
   44 ?        00:00:00 kblockd/0
   45 ?        00:00:00 kblockd/1
..........................................
..........................................
 3534 tty1     00:00:00 getty
 3535 tty2     00:00:00 login
 3536 tty3     00:00:00 getty
 3537 tty4     00:00:00 getty
 3538 tty5     00:00:00 getty
 3539 tty6     00:00:00 getty

In case you want to terminate a process that you started, you can do so from a terminal using the kill command.

$ kill 3941

Here we provide the PID as the argument. Remember that the kill argument is non-interactive and non-verbose by default and hence must be used carefully. Also, you can kill only your own processes. Also if the program has truly crashed in may not respond to to the instruction use the -9 option in that case.

Processes and jobs (background)

If you want to run something in the background and return control to your terminal, just put an ampersand (&) after the command name.

$ firefox &
[1] 3694
$

The shell prints a brief message and gives you another dollar sign prompt. Firefox is now running (and should pop up a window of its own, because it's a graphical program). You can continue to execute other commands in your terminal.

What are the two numbers printed after you put the program in the background? The number in square brackets is a special number assigned to each program you run in the background; it's called a job number. In this case, the job number is 1 because we don't have any other programs currently running in the background in this shell.

The second number, which is 3694 in this case, is the process number we saw in an earlier section.

To bring your job back in the foreground just type fg. It will take over your terminal as commands usually do,until it finishes.

If you have multiple jobs in the background, you can pass the job to the fg command either as a process number:

$ fg 3694
or as a job number:
$ fg %1

To distinguish the job number from a process number, you must put a percent sign (%) before the job number.

If you want to run a process in the background that's now running in the foreground, type Ctrl-Z, which suspends the job, then issue the command bg.

To find out what jobs you've put in the background (and their status), enter jobs:

$ jobs
[1]-  Running                 firefox &
[2]+  Exit 2                  sort > big_file_sorted 2> big_file_err

Firefox is still running, but the second job exited with an error status of 2.

Redirection

Output redirection is one of the very powerful, and easily misunderstood, parts of the shell. To decrease misunderstandings, we'll keep to the basics.

The > operator is for redirecting output. In a very simple example, if you want to list the files in a directory, you type "ls". That output goes to your screen. If you want that list instead to go to a file, however, you'd do something like this:

$ ls > my-file-list 

The file "my-file-list" now contains all of the files in your directory. (If you ran the same command again, you'd also get the file "my-file-list" in your list.) 

 The > operator is a "clobbering" operator -- if you are outputting to an existing file, it will overwrite the old contents. Sometimes, especially if you are keeping a logfile, what you want is the >> operator. It works the same way as the > operator, except that it appends to the end of an existing file. (If the file doesn't yet exist, it creates it.)

There are other places you can redirect output to, like device special files such as terminals, or /dev/null, which is an infinitely big empty bucket (or more accurately it just ignores all input). If you have a program that you know will produce voluminous output you don't care about, you could do this:

$ bigprogram > /dev/null

The program will execute normally, but you won't see its normal output. (You would, however, see any of its error output; more detail below under File Descriptors).

The < operator is for redirecting input. Most programs that would expect input from your terminal are happy to accept it from another source instead, such as an existing file.

For example, if you wish to email the contents of myfile.txt to joe you could do this:

$ mail joe < myfile.txt

The redirection operators are particularly relevant for jobs running in the background. If you run a non-graphical program in the background, it just runs quietly unless it produces output. Any output it produces goes to your terminal as usual, so you may want to avoid confusion by redirecting output to a file. For instance, the following places the output from sort into big_file_sorted and any error messages into big_file_err. This way, no output can confuse you by appearing at the terminal:

$ sort big_file >big_file_sorted 2>big_file_err &
A program running in the background cannot accept input from the terminal. So if you mistakenly put such a program in the background (and don't redirect input from a file through the < operator), it will just stop when it has to accept input.