Introducing the CLI (command line interface)
The command line is just one of many names people use to refer to a text-based user interface where you enter commands. Another popular name is the terminal.
To quickly get going with using the command line, open a terminal by hitting Ctrl+Alt+t.
Now to find out where you are in your file system's directory structure, type this command:
This basically prints (that is displays) the current working directory.
Below is a screenshot of what things should look like, assuming you're using the default gnome terminal.
Some of you may be now thinking: "Hang on, what actually happened here? how did entering
pwd result in my home directory getting displayed?". That's a good question.
What actually happened is this: the text you entered got interpreted by a shell (a piece of software who's job is to run system commands). In this case, the shell checks that there is a program or command called
pwd and runs it on your behalf. So at a basic level the shell's job is to actually execute programs and commands for you.
The shell is another name that people sometimes use to refer to the CLI or a terminal. To learn more about how the BASH shell works, read Getting started with BASH post.
Yet another command line interface is a virtual console. For now we won't get into too much detail about these. However, if you would like to know a bit more about the differences between terminals, shells, and virtual consoles then read Terminal basics.
Throughout this website, we'll use the terms command line and terminal interchangeably, sometimes we'll throw shell into the fray also!
For example, if we say open a shell, we just mean get to the command line... it doesn't matter whether it's a terminal or virtual console!
At a guess, most of you will enter commands on the command line of gnome terminal running the BASH shell, simply because they are the defaults in a lot of Linux desktops including Ubuntu.
OK, with that out of the way, lets get started ...
OK, so what is this "CLI" used for?
The command line can be used for and as a means to do many things. These include but are not limited to these sorts of tasks:
- Manipulating files and directories
- Manipulating disks
- Controlling user access and permissions
- Editing files
- Configuration of the operating system (for example optimisations and performance tuning with the
- Configuration of software (for example by editing
- Installation/updating/removal of software (for example with the apt-get command)
- Executing commands and utilities
- Scripting/programming (for example using bash scripting or another interpreter such as python / perl / lua / ruby, or even building binaries for compiled languages such as C and C++ with compilers such as gcc)
- Desktop/Server security hardening for example Firewall configuration and periodic file integrity checking
- Various types of system resource monitoring for example network, disk io, cpu, and memory usage monitoring.
There's not much that cannot be done from the command line.
The command manual: using
Beginners will want to know how they can find out more about a particular command or utility.
The way to do this is to use the
info utility to view the manpage or info page.
Sometimes both of these informational resources are available for a given piece of software, although the coverage and the level of detail can vary significantly (Also, sometimes they point to the same piece of information!).
To use these, simply precede the command in question by either man or info and hit return (press q to exit).
For example, to find out more about the
ls command, you can use:
To quit from either, just hit the q key on your keyboard. Sometimes these can contain quite a lot of information. For example check out bash:
(Note that for BASH both
info point to the same underlying information, however in a lot of cases the content is usually different so its worth having a quick read of both).
But how will I know what "man" page to use?
If you have some sort of idea what you are looking for, you can use the
apropos command to search through the man page names and their descriptions:
Say you wanted to find out how to remove files, you could try searching with the string
apropos -s 1 "remove"
which should point you toward a list of commands which include the
rmdir commands which we'll explain more about shortly.
Viewing text based files
For simple viewing of text files when no editing is required, use the
more command or the
To use the
more command simply run it followed by the name of the file who's contents you wish to view:
More is quite primitive, only allowing you to display text as you move down through the file. To move down one line at a time hit space; alternatively hit Enter to move down one page at a time. To exit from more, hit Ctrl+c or just
To use the
less command, run it like so:
The less command is much more suited to most peoples viewing needs because it allows you to scroll anywhere using the arrow keys or the Page Up / Page Down keys.
To perform a simple forward search (down the page), hit /, then type the text you want to search for, then hit Enter.
To perform a simple reverse search (up the page), hit ?, then type the text as before, and hit Enter.
For either search direction, hitting n will go to the next search match.
To exit from less, just hit the q key.
Creating new files
To add an empty file, you can use the
touch command. For example to create a file called
Otherwise to create a file, just open a new file in whatever software you are using, then save that. Taking
nano as an example, you could open a file called
Then type something if you like, then hit Ctrl+O to save the file. You'll be prompted about what name to use (in case you've changed your mind about the filename). Alternatively, you could hit
X to exit, which would prompt you about saving the file before exit.
Creating new directories
To create a new directory you would use the
mkdir command, for example:
Removing files and directories
To remove files and directories, you can use the
rm command; for a file use:
For a directory, use:
rm -r dirname
If you would like to be prompted before files (and/or dirs) get removed, you should use the
-i option. An example of this when removing a directory is:
rm -ir dirname
Another way to remove directories (if they are empty) is with the
Command line navigation
In order to carry out your work at the command line, a user will need to know how to navigate the system using a shell. A good starting point is getting to grips with the
As mentioned already, to find out where you're currently at in the directory structure of the file system, use the
pwd command (which basically prints the working directory), for example:
To change from the current working directory to another, use the cd command. For example to change to our Downloads directory, we'd use:
To find out what files are present in a directory, use the
ls command; for example to list the directory contents of our home directory, we'd use:
To operate commands in relation to your home directory, you can use the
~ key as a shortcut. So instead of typing
/home/username, just use
~ instead. For example to
cd to your home directory, just enter:
and to list the files in your home directory, enter:
Finding files and searching file contents
Installing new software
The Ubuntu Software Centre GUI can be used to install and manage your software. Alternatively, or along side this, you can use the [apt-get](/2012/10/a-faq-style-introduction-to-apt-get-and.html) command line utility to manage the software installed on your Ubuntu system. There are also other options like synaptic and aptitude. They all use the same underlying
In the following sections, we'll give you a flavour of what's available for command line usage (but bear in mind we're barely touching the surface of what software you can install on your Ubuntu system!).
Choosing an editor
If you want to get productive in the terminal regardless of whether your using a server or desktop installation, you'll need to choose a non GUI based editor and become familiar with it (that is one that doesn't involve a graphical user interface). Some of the options available for you to choose are vi, vim and nano. Note that
emacs lovers can use a non GUI version for example running
emacs -nw ... however, you have more chance of finding
vim pre-installed on a box
You'll be able to use this non GUI based editor on servers and desktops where there's no GUI editor available, so it's a handy skill to learn because you'll almost always be able to use it for your editing needs.
Resource monitoring tools
Tools for monitoring the resource usage of your desktop, laptop or server can prove to be very useful for troubleshooting and optimisation related purposes. You can carry out various types of resource monitoring with the terminal including network / disk / CPU / memory usage monitoring.
Example utilities include:
Data transfer & networking tools
There are lots of network related programs available for use with the command line.
You can check on the status of your network cards using
ifconfig or the more complete ip utility. You can also "shape" network traffic and interact with the routing and neighbor (arp) tables using the
Firewall related tools
You should always use a firewall to help keep unauthorised traffic away from your system. The
iptables program and its more beginner friendly wrapper called
ufw (Uncomplicated Firewall) are available for configuring your firewall.
Job automation tools
You can schedule commands and utilities to run at specific predetermined times using utilities like
cron. This can be really handy for running backup scripts and other jobs that need to run on a regular basis which are best automated.
Hardware and BIOS info tools
You can list the hardware configuration of your system using
It's also possible to get a dump of the BIOS information from your box using
biosdecode or the more detailed
To display info about USB buses and devices, you can use
lsusb. You can also get some information about PCI buses and the devices connected to them by using
File compression and archiving tools
There are many tools available for zipping/compressing files to save space.
Options include gzip, zip, and bzip2. Their decompression counterparts are gunzip, unzip, and bunzip2 respectively.
The tar utility can be used to archive several compressed files into one "package" (and it can also carry out the compression/decompression stages).
Encryption related tools
There are also encryption related tools such as openssl and gpg available with the terminal. These will allow you to keep sensitive data private with encryption and to verify that data came from a specified source with digital signatures.
For more complete control of your system, a good understanding of using the CLI/terminal (and thus the bash shell) is a mandatory skill to have.
It's a skill set that may take time to develop but is well worth it.