How to use Linux Commands

There are thousands of Linux commands, from the commonplace to the arcane, but you need only a handful of key commands to get started in Linux.

Here we will look at some of the core workhorse commands, giving a brief description of what each one is for. As always, the man pages give far more detail on how to use them.

Many of these produce more output than can fit in your terminal display, so consider piping them through Less (as described in our introduction to apt-get).

File handling

Central to any terminal activity is working with files, creating, removing, listing and otherwise examining them. Here are the main commands for this.

  • ls: lists the contents of the current or given directory.
  • ls -l: as for ls, but gives more information about each item. Add human-readable file sizes with -h
    ls -lh MyPhotos
  • rm: deletes a file. Use the -i option for confirmation before each removal or -f to blitz everything. With -rit deletes directories and their contents too.
  • rmdir: deletes a directory, which must be empty.
  • df: Shows free disk space on all filesystems or just those given on the command line.
  • du: Shows the amount of space used by individual files or directories.
    df -h /home
    du -sh /home/user/*
  • file: identifies the type of a file. Unlike Windows, which uses the file name extension, this command looks inside the file to see what it really contains.
  • find: searches the current or given directory for files matching certain criteria. For example you could find all LibreOffice spreadsheets with
    find Documents -name '*.ods'
  • locate: This also looks for files but using a much faster system. The locate database is automatically rebuilt each day by updatedb, and locate then searches this. It's fast, but doesn't know about very recent changes.

Text handling

How to master the Linux terminal with core commands

Stay out of the Vi vs Emacs arguments by using the lightweight Nano to edit config files. The instructions at the bottom of the window make it almost impossible to get lost.

Text files are all around us, from emails to configuration files, and there are plenty of commands to deal with them. If you want to edit a text file, there are a number of choices, with the two big ones beingEmacs and Vi. Both are overkill if you just want to tweak a configuration file; in this instance, try nanoinstead:

nano -w somefile.txt

The -w option turns off word wrapping, which you certainly don't want when editing configuration files. The status bar at the bottom shows the main commands – for example, press Ctrl + X to save your file and exit.

This assumes you know which file you want, but what if you know what you're looking for but not the name of the file? In that case, use grep. This searches text files for a string or regular expression.

grep sometext *.txt

This will search all .txt files in the current directory and show any lines containing the matching text from each file, along with the name of the file. You can even search an entire directory hierarchy with -r (or --recursive):

grep -r -I sometext somedir

Be careful when you're searching large directory trees: it can be slow and return strange results from any non-text files it searches. The -I option tells grep to skip such binary files.

Text is also the preferred way of passing data between many programs, using the pipes we looked at previously. Sometimes you want to pass data straight from one program to the next, but other times you may want to modify it first. You could send the text to a file, edit it and then send the new file to the next program, or you could pass it though a pipe and modify it on-the-fly.

Nano edits files interactively, grep searches them automatically, so we just need a program to edit automatically; it's called sed (Stream EDitor). Sed takes a stream of text, either from a file or a pipe, and makes the changes you tell it to. The most common uses are deletion and substitution. Normally, sedsends its output to stdout, but the -i option modifies files in place:

sed -i 's/oldtext/newtext/g' somefile.txt
sed -i '/oldtext/d' somefile.txt

The second example deletes all lines containing "oldtext".

Another useful program is awk, which can be used to print specific items from a text file or stream.

awk '{print $1}' somefile.txt
cat *.txt | awk '/^Hello/ {print $2}'

The first example prints the first word from each line of the file. The second takes the contents of all files ending in .txt, filters the lines starting with "Hello" (the string between the slashes is a pattern to match) and then prints the second word from each matching line.

Networking

We normally picture big graphical programs like Chromium and Thunderbird when we think of networked software, but there are many command line programs for setting up, testing and using your network or internet connection.

  • Ping sends a small packet to a remote server and times the response, which is useful if you want to check whether a site is available, or if your network is working.
    ping -c 5 www.google.com
  • wget: downloads files. The only argument it needs is a URL, although it has a huge range of options that you will not normally need.
  • hostname: shows your computer's host name, or its IP address with -i.
  • lynx: a text mode web browser. While not as intuitive as Chromium or Firefox, it is worth knowing about in case you ever suffer graphics problems.

How to master the Linux terminal with core commands

Lynx is a web browser than runs in a terminal window. It may not be the best choice for watching YouTube, but it's fast and always there, even if you have no desktop available.

Commands, options and arguments

You'll often see references to command arguments and options, but what exactly are they?

Options and arguments are the things that tell a program what to do. Put simply, arguments tell a command what to do, while options tell it how to do it – although the lines can get a little blurred.

Take the ls command as an example: this lists the contents of a directory. With no options or arguments, it lists the current directory using the standard format:

ls
Desktop Downloads Music Public Videos
Documents examples.desktop Pictures Templates

If you want to list a different directory, give that as an argument:

ls Pictures

or

ls Desktop Downloads

Arguments are given as just the names you want listed, but options are marked as such by starting with a dash. The standard convention among GNU programs, and used by most others, it to have long and short options.

A short option is a single dash and one letter, such as ls -l, which tells ls to list in its long format, giving more detail about each file. The long options are two dashes followed by a word, such as ls --reverse, which lists entries in reverse order, as is pretty apparent from the name. The form ls -r does the same thing but it is not so obvious what it does.

Many options, like this one, have long and short versions, but there are only 26 letters in the alphabet, so less popular options are often available only in the long version. The short options are easier to type but the long ones are more understandable. Compare:

ls -l --reverse --time

with

ls -l -r -t

or even

ls -lrt

Each gives a long listing in reverse time/date order. Notice how multiple short options can be combined with a single dash.

While we're talking about ls, this is a good time to mention so-called 'hidden' files. In Linux, any files or directories beginning with a dot are considered hidden and do not show up in the output from ls or in most file managers by default.

These are usually configuration files that would only clutter up your display – but if you want to see them, simply add the -A option to ls.

Linux core commands

By piping the output from du through sort, and adding extra options to both commands, we can see which directories use the most space.

Getting help

The command line may appear a little unfriendly at first, but there's plenty of help available if you know where to look. Most commands have a --help option that tells you what the options are. The man and info pages are the main source of information about anything. To learn all the options for a program and what they do, run the following:

man progname

The man pages are divided into numbered sections. The ones that are most applicable to using the system are:

  • 1 User commands
  • 5 File formats and conventions
  • 8 System Administration tools

If you don't specify the number, man will pick the first available, which usually works.

But man pages are not limited to programs; they also cover configuration files. As an example, passwords are managed by the passwd command, and information is stored in the /etc/passwd file, so you could use:

man passwd

man 1 passwd

man 5 passwd

The first two would tell you about the passwd command while the third would explain the content and format of the /etc/passwd file.

Man pages have all the information on a single page but info pages are a collection of hypertext linked pages contained in a single file.

How to master the Linux terminal with core commands

Info pages, when available, provide more detail than man pages, but you will need to read info's own info page first to learn to use it.

They often provide more detail but aren't intuitive to read – try info info to see how to use them. It's often easier to use a search engine to find the online version of info pages, which contain the same information in the more familiar HTML format.