How to Use Man Pages to Enhance Terminal Skills

What is the hardest part of using the Linux terminal?

For me, it is the switches and arguments.  I hate pure memorization, and recoil at remembering even the most basic ones.

Fortunately for people like me, there is help.  Man pages, short for manual pages, are documentation for many of the terminal commands available.

 

Help Is Only a ‘man’ Away

Use the terminal command ‘man’, with the command you want to learn about as an argument, to access that command’s man page.

For example, for the ‘ls’ command, I would type: ‘man ls’ into the terminal.

Results of the man ls terminal command.

 

Man pages are usually lengthy and don’t fit on one “page” of the terminal window.  The first part of the man page appears upon execution of the command.  To see the rest of the man page, you must advance the output using the Enter key or the space bar.  The Enter key advances the output by a single line, the space bar by an entire “page.”

To close a man page, press the ‘q’ key.

Here is a copy of the entire output of ‘man ls’.

 

Deciphering a “man page”

Unfortunately, not all man pages conform to the same style guidelines.  There is no central authority producing man pages, it is up to each command’s developer(s) to create and style them.  However, most are very similar to the example above.  So let’s use that man page to find out how to deal with arguments and switches.

 

Arguments & Order of Operations

Often times, what you will want to know is what arguments you need, and what order they go in.  For that, you want to look under the synopsis section.

The synopsis lists all the possible arguments for a command, as well as the order to place them in.  In the case of ‘ls’, our argument is “FILE”.  “OPTION” is where you place switches.

In a man page’s synopsis, when something is in square brackets, it means that it is an optional addition.  Since both “FILE” and “OPTION” are in square brackets, the ‘ls’ command does not need any switches or arguments to work; adding them is optional.

When the argument or switch is followed by ellipses (“…”), it means that you can put more than one.  In other words, ‘ls -l -a /home/john /home/bob’ is perfectly acceptable.  We have more than one switch, and more than one argument even though the man page only shows a place for one.  Ellipses means multiple are accepted.

 

Now let’s look at the SYNOPSIS of another command, ‘cp’.

As shown by the three entries under synopsis, ‘cp’ has more than one possible arrangement of arguments.  The first one you are familiar with if you’ve read our guide to the terminal.  The brackets and ellipses surrounding OPTION tell us that we can use multiple switches, but none are needed.  There are then two arguments, SOURCE and DEST.  SOURCE is the source file or directory, DEST is the destination file or directory.

But if we look at the second entry, there are some changes.  First, SOURCE now has ellipses indicating that multiple sources are acceptable.  Second, DEST has changed to DIRECTORY indicating that we can no longer specify a specific file as the destination, it must be a directory.  ‘cp file1.txt file2.txt file3.txt /home/john/myfiles/’ is a valid example of this.  We have 3 source files all going to the myfiles directory.

And the third entry gives us yet another option.  The third choice can still use multiple switches; however, one switch is required.  This long-form switch, ‘target-directory’, indicates the destination for what you are copying.  Then you must have one or more source files.  An example of this form is ‘cp –target-directory=/home/john/myfiles/ file4.txt file5.txt file6.txt. ‘.  This copies those three files to the myfiles directory.

 

Rebellion!

Despite what the man pages say, switches do not need to go before arguments.  You can move the switches to the end, or, if you have multiple switches, mix them in with the arguments.  Occasionally, rearranging them helps with readability, particularly for long commands.   However, this is optional.

 

It’s Raining Switches

Now let’s talk about finding available switches.  If you scroll down a man page, there is usually a list of switches for the command.  I am going to extract a small segment of the ‘ls’ command’s list here so we can talk about it.  You can see the whole list up above.

Let’s begin by looking at the most complex entry, ‘-I, –ignore=PATTERN’.  This switch can be expressed two ways, either the short form of ‘-I’, or the long form of ‘–ignore’.  Both do the same thing, you can choose your favorite.

This entry indicates that the switch requires an argument with the “=PATTERN.”  Recall that with short switches, the argument is provided with a space, then the argument.  And the long-form switch is an equal sign and then the argument.  When a switch has both a short-form and a long-form, the man page shows the argument on the long-form only.  If there is only a short-form switch, the argument is shown with the proper space, then argument notation.  It would look like this:

 

After each switch and their arguments, there is a short description of what the switch does.  Sometimes these descriptions are helpful, sometimes they aren’t.  If they aren’t helping, you can either experiment with the switch until you find what works, or ask Google for help.

man page Limitations

There are some Linux users who believe man pages are fantastic just as they are.  I am not one of them.  They are difficult to browse and searching, while possible, is limited.  If you actually wish to read a man page, as opposed to quickly skimming to refresh your memory, I recommend using the Internet.

There are many websites with copies of most man pages.  An appropriately phrased Google search will yield pages of results.  Or, you can simply visit http://linux.die.net/man/ and use their search feature.

 

 

Whether you are a veteran of the terminal, or a newcomer, you will probably need to consult man pages on occasion.  They aren’t perfect, and you need a bit of skill to get the most out of them, but they are quickly and easily accessible.  So add the ‘man’ command to your terminal toolbox, and try it out by running the command ‘man man’ in your terminal!

If you have any other tips for browsing man pages, or have questions, make sure to leave a comment!

 

Written By

John is a sailing instructor and mechanical engineering student who happens to be a computer geek. To find more information about John, visit his website or find him on social media by clicking on the icons below.

Facebook Twitter Google+ Website

Tags: , ,

Join the Conversation!