The command-line is often a daunting place for beginners. With nothing but a blinking cursor and an unfriendly dark screen staring back at you, despair sets in.
Here are a few things to do if you are stuck:
Many times, you want to do a particular task but you do not remember the name of the program which does that. Many shell utilities are not easy to remember at first with their cryptic 2 letter names.
Worry not, there is a command called
apropos to help you out!
For example, you want to show a file with line numbers. You know that there is a program but have forgotten its name. You can just type
apropos -a line number to get a list of programs. We use
-a flag so that
apropos will only return those programs which have the words
number in their description.
On my computer, it gives the following output:
apropos -a line number
addr2line (1) - convert addresses into file names and line numbers.
nl (1) - number lines of files
x86_64-linux-gnu-addr2line (1) - convert addresses into file names and line numbers.
There are 3 programs which have something to do with numbering lines. Let's find out what each of these programs does.
Let's have a look at our first candidate
--help after the command is one way of finding out what a program does. It is a convention for command-line programs to print a small help message when you call them with
--help. Some programs also allow do the same on calling with
Usage: addr2line [option(s)] [addr(s)]
Convert addresses into line number/file name pairs.
If no addresses are specified on the command line, they will be read from stdin
Ok. This does program does something with
addresses, not what we are looking for. Moving on to the next in our list:
Usage: nl [OPTION]... [FILE]...
Write each FILE to standard output, with line numbers added.
Looks like we found our program. Let's try it out. This is our file
It works ok but why is it not numbering blank lines?
--help messages are very concise. If we want to know in detail what a program does, we need to consult the manual or
man pages. These pages are very detailed documentation of what a program does, all its possible options and arguments. Type
man nl and have a look at the information...
Woah! The terminal screen is filled with information!. You can navigate up and down using the arrow keys and press
q to quit.
We can see the following in the output:
use STYLE for numbering body lines
STYLE is one of:
a number all lines
t number only nonempty lines
n number no lines
nl -b a foo.txt will number all lines in foo.txt which is what we want.
nl -b a foo.txt
That seems to solve our problem.
There is also
info command which gives even more detailed usage information. You can try it out using
But what if we just quickly want to get our command to work without having to read a huge wall of text?
Turns out, there is a nifty utility you can install called
tldr. If you use
python you can install using
npm install -g tldr or
pip install tldr respectively. The tldr page has a list of other installation options. After installing it, just try out
tldr nl in your terminal
A utility for numbering lines, either from a file, or from standard input.
- Number non-blank lines in a file:
- Number all lines including blank lines:
nl -b a file
There! We can see the example with easy to understand description.
tldr is community-driven! People contribute examples for various commands. If your favorite command does not have an entry, you can submit a pull request.
To recap, we can:
- Find out possible programs which do particular tasks using
- Get help information for
- Get examples using
In Section 1, we assume that we will be able to find programs for our task. But many times, we may not have it installed. In such cases, we can search our distribution's package manager e.g
apt-get for Ubuntu or
dnf for CentOS / Fedora.
In Ubuntu, we can search all available packages in the repositories using
apt-cache search $KEYWORD.