How to use Linux Commands

One of the biggest changes that catches Windows users moving to Linux is the way that software is installed. Instead of downloading an executable file from some website or other, running it and hoping it doesn't clobber your existing library files (DLLs) or install some dubious adware or malware, Linux distributions maintain repositories of software, which are all packaged up for that distro and tested for compatibility with the rest of the distro.

On this page of our guide to everything you need to know about Linux Commands, we will look at how this is done by distros that use the Advanced Packaging Tool (apt) software management system, as developed by Debian and used by distros from Ubuntu to Raspbian on the Raspberry Pi.

Repositories

A repository is a collection of software packages for a distro. Each major release of a distro will have its own repositories, and the packages will have been built for and tested with that release, but a repository is more than a collection of files.

Each repo (as they are usually called) is indexed, making it easy to find what you want. It can also be quickly checked for updates for your package manager without any need to visit websites to check for updates, or the need for software to 'phone home' to check.

More importantly, each package in a repo is signed with the repository's GPG (encryption) key, which is checked when installing packages. This means you can trust the software installed from there to be what it says it is, and not some infected trojan that's been uploaded maliciously.

A repository also makes dependency handling simple. A dependency is a program that the program you want to install needs to run, such as a library. Instead of bundling everything in the package and ending up with multiple copies of the same library on your computer (which is what Windows does), a package simply lists its dependencies so that your package manager can check whether they are already installed, and grab them from the repo if not.

In addition to the default repositories provided by the distro, there are several third-party ones that can be added to your package manager. These are not guaranteed to be tested to the same standards as the official repos, but many of them are very good, and if you stick to the popularly recommended repos for your distro, you won't go far wrong.

Ubuntu has also introduced the concept of the PPA, or Personal Package Archive, which are small repositories for individual projects. These may each be added individually to your package manager, but be careful about adding any untrusted sources.

Package management

We have used the term 'package manager' a few times now but what is it? Basically, this is a program that enables you to install, update and remove software, including taking care of dependencies. It also enables you to search for programs of interest, as well as performing other functions.

All distros will have command line package management tools. You can access them either by using your system's search and looking for terminal or using [Ctrl]+[Alt]+[T] in Linux desktops such as Unity, Gnome or Xfce, even if they also provide a fancy graphical front end. The main Linux commands are:

apt-get: Installs, upgrades and uninstalls packages.

apt-cache: This works with the repository index files, such as searching for packages.

add-apt-repository: Adds extra repositories to the system.

dpkg: A lower level package manipulation command.

These commands generally require root (superuser) access, so should be run at the root user or with sudo – we will stick with the sudo approach here. We've already mentioned that repos are indexed, so the first thing to do is update your index files to match the current contents of the repositories with:

sudo apt-get update

Then you probably want to make sure that your system is up to date:

sudo apt-get upgrade

This will list the packages it wants to install, tell you how much space it needs for the download, and then get on with it when you tell it to. When you want to install some new software, unless you have been told the exact name to install, you may want to search for it first, like this:

apt-cache search gimp

This will spit out a long list of packages, because it searches both name and description, and lists anything mentioning gimp, and there are a lot of them. To search only the names, use the -n or --names-only option:

apt-cache search -n gimp

This often gives a more manageable output, but still a lot in this case, perhaps too much to fit in your terminal window. The solution to this is to pipe the output from this command to the program less:

apt-cache search -n gimp | less

The less command is a pager – it lets you read text page by page and scroll through it. It can be used with any program that generates lots of terminal output to make it easier to read (see the 'Package management' walkthrough below for more details). Once you have found the package you want, installation is as simple as:

sudo apt-get install gimp

You can install multiple programs by giving them all to aptget at once:

sudo apt-get install program1 program2...

Not every program you try will be what you want, so you can tidy up your hard drive by uninstalling it with:

sudo apt-get remove program1

Or you can use:

sudo apt-get purge program1

Both commands remove the program, but remove leaves its configuration files in place while purge deletes those, too.

There are a number of extra options you can use with aptget, the man page lists them all (type man apt-get in the terminal), but one of the most useful is --dry-run. This has apt-get show you what it would do without actually doing it, a useful chance to check that you are giving the right command. Remember, computers do what you tell them to, not what you want them to do!

Finally, you don't normally need to use dpkg, but it is useful for listing everything you have installed with dpkg -L.

Package management

Everything you need to know about Linux Commands

1. Install

Using apt-get install will check the dependencies of the packages you want and install any that are needed. Adding --dry-run to apt-get install enables you to see what would be done, without actually writing anything to your hard drive. If you are happy, run the command again without --dry-run.

Everything you need to know about Linux Commands

2. Search

Use apt-cache search to find what's available. The --names-only option can give a more manageable set of results if you know the program's name. Otherwise let apt-cache search go through the descriptions, too, and view the results in less. You don't need to use sudo as search doesn't write to your drive.

Everything you need to know about Linux Commands

3. Update

Run apt-get update to update all your package lists, followed by apt-get upgrade to update all your installed software to the latest versions. In our case, it's well overdue. Then apt will show you what needs to be updated, and how much needs to be downloaded, before asking whether you want to proceed.