# Using the Command Line

&legalnotice;

## Using this Guide

"Under Linux there are GUIs (graphical user interfaces), where you can point and click and drag, and hopefully get work done without first reading lots of documentation. The traditional Unix environment is a CLI (command line interface), where you type commands to tell the computer what to do. That is faster and more powerful, but requires finding out what the commands are." -- from man intro(1)

This guide will make you familiar with basic GNU/Linux shell commands. It is not intended to be a complete guide to the command line, just an introduction to complement Ubuntu's graphical tools.

- All command names, file names or paths to directories will be shown using a `monospace font`.

- All the commands on this guide are to be issued from a command prompt in a Terminal and will be shown as:

      command to type

- Note that Linux is case sensitive. User, user, and USER are all different to Linux.

## Starting a Terminal

To open a Terminal choose &terminal;.

## File and Directory Commands

### cd

The `cd` command changes directories. When you open a terminal you will be in your home directory. To move around the file system you will use `cd`.

- To navigate into the root directory, type:

      cd /

- To navigate to your home directory, type:

      cd

  or

      cd ~

  The `~` character represents the current user's home directory. As seen above, `cd ~` is equivalent to `cd /home/username/`. However, when running a command as root (using `sudo`, for example), `~` points instead to `/root`. When running a command with `sudo`, the full path to your home directory must be given.

- To navigate up one directory level, type:

      cd ..

- To navigate to the previous directory (or back), type:

      cd -

- To navigate through multiple levels of directories at once, specify the full directory path that you want to go to. For example, type:

      cd /var/www

  to go directly to the `/www` subdirectory of `/var/`. As another example, type:

      cd ~/Desktop

  to move you to the `Desktop` subdirectory inside your home directory.

### pwd

The `pwd` command outputs which directory you are currently located in (pwd stands for print working directory). For example, typing

    pwd

in the `Desktop` directory, will show `/home/username/Desktop`.

GNOME Terminal also displays this information in the title bar of its window.

### ls

The `ls` command outputs a list of the files in the current directory. For example, typing

    ls ~

will show you the files that are in your home directory.

Used with the `-l` options, `ls` outputs various other information alongside the filename, such as the current permissions on the file, and the file's owner.

### cp

The `cp` command makes a copy of a file. For example, type:

    cp foo bar

to make an exact copy of `foo` and name it `bar`. `foo` will be unchanged.

### mv

The `mv` command moves a file to a different location or will rename a file. Examples are as follows:

    mv foo bar

will rename the file `foo` to `bar`.

    mv foo ~/Desktop

will move the file `foo` to your `Desktop` directory but will not rename it.

### rm

`rm` is used to delete files.

    rm foo

deletes the file `foo` from the current directory.

By default, `rm` will not remove directories. To remove a directory, you must use the `-R` option. For example,

    rm -R foobar

will remove the directory foobar, and all of its contents!

### mkdir

The `mkdir` command allows you to create directories. For example, typing:

    mkdir music

will create a directory named `music` in the current directory.

## System Information Commands

### df

The `df` command displays filesystem disk space usage for all partitions.

    df -h

will give information using megabytes (M) and gigabytes (G) instead of blocks (-h means "human-readable").

### free

The `free` command displays the amount of free and used memory in the system.

    free -m

will give the information using megabytes, which is probably most useful for current computers.

### top

The `top` command displays information on your GNU/Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press q.

### uname

The `uname` command with the -a option, prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using.

### lsb_release

The `lsb_release` command with the -a option prints version information for the Linux release you're running. For example, typing:

    lsb_release -a

will give you:

    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu &distro-rev;
    Release:        &distro-rev;
    Codename:       &distro-short-codename;

## Executing Commands with Elevated Privileges

The following commands will need to be prefaced with the `sudo` command. Please see RootSudo for information on using `sudo`.

### Adding a New Group

The `addgroup` command is used to create a new group on the system. To create a new group, type:

    addgroup newgroup

The above command will create a new group called newgroup.

### Adding A New User

The `adduser` is used to create new users on the system. To create a new user, type:

    adduser newuser

The above command will create a new user called newuser.

To assign a password for the new user use the `passwd` command:

    passwd newuser

Finally, to assign the new user to the new group, type:

    adduser newuser newgroup

## Options

The default behavior for a command may usually be modified by adding a -- option to the command. The `ls` command, for example, has a -s option so that `ls -s` will include file sizes in the listing. There is also a -h option to get those sizes in a "human readable" format.

Options can be grouped in clusters so

    ls -sh

is exactly the same command as

    ls -s -h

Most options have a long version, prefixed with two dashes instead of one, so even

    ls --size --human-readable

is the same command.

## "Man" and getting help

command --help and man command are the two most important tools at the command line.

Virtually all commands understand the -h (or --help) option which will produce a short usage description of the command and it's options, then exit back to the command prompt. Type

    man -h

or

    man --help

to see this in action.

Every command and nearly every application in Linux will have a man (manual) file, so finding them is as simple as typing `man command` to bring up a longer manual entry for the specified command. For example,

    man mv

will bring up the `mv` (move) manual.

Move up and down the man file with the arrow keys, and quit back to the command prompt with q.

    man man

will bring up the manual entry for the `man` command, which is a good place to start.

    man intro

is especially useful - it displays the "Introduction to user commands" which is a well-written, fairly brief introduction to the Linux command line.

There are also `info` pages, which are generally more in-depth than `man` pages. Try

    info info

for the introduction to info pages.

### Searching for man files

If you aren't sure which command or application you need to use, you can try searching the man files.

- `man -k foo`, will search the man files for foo. Try

      man -k nautilus

  to see how this works.

  This is the same as the `apropos` command.

- `man -f foo`, searches only the titles of your system's man files. For example, try

      man -f gnome

  This is the same as the `whatis` command.

## Cutting and pasting

Often, you will be referred to instructions that require commands to be pasted into the terminal.

You might be wondering why the text you've copied from a web page using Ctrl C won't paste in with Ctrl V , the shell uses a different combination for paste Shift Insert. This can also be done using the mouse, by right-clicking in the terminal and clicking Paste from the menu.

You can also select the required commands in your web browser and then click the middle button on your mouse (or both buttons simultaneously on a two-button mouse) in the terminal to paste in the select command.

These actions may mean something entirely different to other terminal emulators. Consult your emulator's documentation for details.

## Other Useful Things

### Save on typing

Up Arrow or Ctrl p | Scrolls through the commands you've entered previously.
Down Arrow or Ctrl n | Takes you back to a more recent command.
Enter | When you have the command you want.
Tab | A very useful feature. It autocompletes any commands or filenames, if there's only one option, or else gives you a list of options.

### Change the text

The mouse won't work. Use the Left/Right arrow keys to move around the line.

When the cursor is where you want it in the line, typing inserts text, it doesn't overtype what's already there.

Ctrl a or Home | Moves the cursor to the start of a line.
Ctrl e or End | Moves the cursor to the end of a line.
Ctrl k | Deletes from the current cursor position to the end of the line.
Ctrl u | Deletes the whole of the current line.
Ctrl w | Deletes the word before the cursor.

## More Information

The following online guides are available:

- AptGetHowto - using apt-get to install packages from the command line.

- Commandline Repository Editing - adding the Universe/Multiverse repositories through the command line.

- grep Howto - grep is a powerful command line search tool.

- find - locate files on the command line.

- CommandlineHowto - longer and more complete than this basic guide, but still unfinished.

- HowToReadline - information on some more advanced customization for the command line.

For more detailed tutorials on the Linux command line, please see:

- - basic BASH tutorials, including BASH scripting

- - Java-based tutorials

- - a massive online book about system administration, almost all from the command line.