Monday, 3 April 2017

Linux Tutorial : Getting Help On Linux Part 1

Introduction

If you ask users what feature of the Linux Operating System they most enjoy, many will answer "the power provided by the command line environment". This is because there are literally thousands of commands available with many options, making them powerful tools.
However, with this power comes complexity. Complexity, in turn, can create confusion. As a result, knowing how to find help while working in Linux is an essential skill for any user. Referring to help allows you to be reminded of how a command works, as well as being an information resource when learning new commands.

man Pages

As previously mentioned, UNIX was the operating system from which the Linux foundation was built. The developers of UNIX created help documents called man pages (man stands for manual).
Man pages are used to describe the features of commands. They will provide you with a basic description of the purpose of the command, as well as provide details regarding the options of the command.

1 Viewing man pages

To view a man page for a command, execute man command in a terminal window. For example, the command man cal will display the man page for the cal command:
CAL(1)                    BSD General Commands Manual             CAL(1) 

NAME                                                                
cal, ncal -- displays a calendar and the date of Easter               

SYNOPSIS                                                               
cal [-3hjy] [-A number] [-B number] [[month] year]                     
cal [-3hj] [-A number] [-B number] -m month [year]                     
ncal [-3bhjJpwySM] [-A number] [-B number] [-s country_code] [[month]  
year]                                                              
 ncal [-3bhJeoSM] [-A number] [-B number] [year]                      
 ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm]                                    

DESCRIPTION                                                            
    The cal utility displays a simple calendar in traditional format and    ncal offers an alternative layout, more options and the date of         Easter. The new format is a little cramped but it makes a year fit      on a 25x80 terminal.  If arguments are not specified, the current       month is displayed.      
                                                                
         The options are as follows:                                               
 -h      Turns off highlighting of today.                                   
                                                 
 Manual page cal(1) line 1 (press h for help or q to quit)

2 Controlling the man Page Display

The man command uses a "pager" to display documents. Normally this pager is the less command, but on some distributions it may be the more command. Both are very similar in how they perform and will be discussed in more detail in a later chapter.
If you want to view the various movement commands that are available, you can type the letter h while viewing a man page. This will display a help page:
Note: If you are working on a Linux distribution that uses the morecommand as a pager, your output will be different than the example shown here.
 SUMMARY OF LESS COMMANDS                                     
                                               
    Commands marked with * may be preceded by a number, N.     
    Notes in parentheses indicate the behavior if N is given.        
    A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.      
                                                                  
  h  H                 Display this help.                             
  q  :q  Q  :Q  ZZ     Exit.                                          
 -----------------------------------------------------------------------
                                                                    
                         MOVING                                         

e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).             
y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).              
f  ^F  ^V  SPACE  *  Forward  one window (or N lines).             
b  ^B  ESC-v      *  Backward one window (or N lines).
z                 *  Forward  one window (and set window to N).  
w                 *  Backward one window (and set window to N).      
ESC-SPACE         *  Forward  one window, but don't stop at end-of-file.
d  ^D             *  Forward  one half-window (and set half-window to N)
u  ^U             *  Backward one half-window (and set half-window to N)
ESC-)  RightArrow *  Left  one half screen width (or N positions).     
HELP -- Press RETURN for more, or q when done
If your distribution uses the less command, you might be a bit overwhelmed with the large number of "commands" that are available. The following table provides a summary of the more useful commands:
CommandFunction
Return (or Enter)Go down one line
SpaceGo down one page
/termSearch for term
nFind next search item
1GGo to beginning
GGo to end
hDisplay help
qQuit man page

3 Sections of the man Page

Man pages are broken into sections. Each section is designed to provide specific information about a command. While there are common sections that you will see in most man pages, some developers also create sections that you will only see in a specific man page.
The following table describes some of the more common sections that you will find in man pages:
Section namePurpose
NAMEProvides the name of the command and a very brief description.
SYNOPSISProvides examples of how the command is executed. See below for more information.
DESCRIPTIONProvides a more detailed description of the command.
OPTIONSLists the options for the command as well as a description of how they are used. Often this information will be found in the DESCRIPTIONsection and not in a separate OPTIONSsection.
FILESLists the files that are associated with the command as well as a description of how they are used. These files may be used to configure the command's more advanced features. Often this information will be found in the DESCRIPTIONsection and not in a separate OPTIONS section.
AUTHORThe name of the person who created the man page and (sometimes) how to contact the person.
REPORTING BUGSProvides details on how to report problems with the command.
COPYRIGHTProvides basic copyright information.
SEE ALSOProvides you with an idea of where you can find additional information. This also will often include other commands that are related to this command.

4 man Page SYNOPSIS Section

The SYNOPSIS section of a man page can be difficult to understand, but is very important because it provides a concise example of how to use the command. For example, consider the SYNOPSIS of the man page for the cal command:
SYNOPSIS                                                              
     cal [-3hjy] [-A number] [-B number] [[[day] month] year]
The square brackets [ ] are used to indicate that this feature is not required to run the command. For example, [-3hjy]means you can use the options -h-j-y1 or 3, but none of these options are required for the calcommand to function properly.
The second set of square brackets in the cal SYNOPSIS ([[[day] month] year]) demonstrates another feature; it means that you can specify a year by itself, but if you specify a month you must also specify a year. Additionally, if you specify a day then you also need to specify a month and a year.
Another component of the SYNOPSIS that might cause some confusion can be seen in the SYNOPSIS of the date command:
SYNOPSIS                                                              
       date [OPTION]... [+FORMAT]                                      
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
In this SYNOPSIS there are two syntaxes for the date command. The first one is used to display the date on the system while the second is used to set the date.
The ellipses following [OPTION],..., indicate that one or more of the items before it may be used.
Additionally the [-u|--utc|--universal] notation means that you can either use the -u option or the --utc option or the --universal option. Typically this means that all three options really do the same thing, but sometimes this format (use of the | character) is used to indicate that the options can't be used in combination, like a logical “or".

5 Searching Within a man Page

In order to search a man page for a term, press the / and type the term followed by the Enter key. The program will search from the current location down towards the bottom of the page to try to locate and highlight the term.
If the term is not found, or you have reached the end of the matches, then the program will report Pattern not found (press Return). If a match is found and you want to move to the next match of the term, press n. To return to a previous match of the term, press N.

6 man Pages Categorized by Sections

Until now, we have been displaying man pages for commands. However, sometimes configuration files also have man pages. Configuration files (sometimes called system files) contain information that is used to store information about the Operating System or services.
Additionally, there are several different types of commands (user commands, system commands, and administration commands) as well as other features that require documentation, such as libraries and Kernel components.
As a result, there are thousands of man pages on a typical Linux distribution. To organize all of these man pages, the pages are categorized by sections, much like each individual man page is broken into sections.
Consider This:
By default there are nine default sections of man pages:
  1. Executable programs or shell commands
  2. System calls (functions provided by the kernel)
  3. Library calls (functions within program libraries)
  4. Special files (usually found in /dev)
  5. File formats and conventions, e.g. /etc/passwd
  6. Games
  7. Miscellaneous (including macro packages and conventions), e.g. man(7)groff(7)
  8. System administration commands (usually only for root)
  9. Kernel routines [Non standard]
When you use the man command, it searches each of these sections in order until it finds the first "match". For example, if you execute the command man cal, the first section (Executable programs or shell commands) is searched for a man page called cal. If not found, then the second section is searched. If no man page is found after searching all sections, you will receive an error message:
sysadmin@localhost:~$ man zed                                          
No manual entry for zed                                                
sysadmin@localhost:~$

6.1 Determining Which Section

To determine which section a specific man page belongs to, look at the numeric value on the first line of the output of the man page. For example, if you execute the command man cal, you will see that the cal command belongs to the first section of man pages:
CAL(1)                    BSD General Commands Manual

6.2 Specifying a Section

In some cases you will need to specify the section in order to display the correct man page. This is necessary because sometimes there will be man pages with the same name in different sections.
For example, there is a command called passwdthat allows you to change your password. There is also a file called passwdthat stores account information. Both the command and the file have a man page.
The passwd command is a "user" command, so the command man passwd will display the man page for the passwd command by default:
PASSWD(1)                        User Commands                 PASSWD(1)
To specify a different section, provide the number of the section as the first argument of the man command. For example, the command man 5 passwdwill look for the passwd man page just in section 5:
PASSWD(5)                File Formats and Conversions

6.3 Searching Sections

Sometimes it isn't clear what section a man page is stored in. In cases like this, you can search for a man page by name.
The -f option to the man command will display man pages that match, or partially match, a specific name and provide a brief description of each man page:
sysadmin@localhost:~$ man -f passwd                                    
passwd (5)           - the password file                              
passwd (1)           - change user password                           
passwd (1ssl)        - compute password hashes                         
sysadmin@localhost:~$
Note that on most Linux distributions, the whatis command does the same thing as man -f. On those distributions, both will produce the same output.

7 Searching man Pages by Keyword

Unfortunately, you won't always remember the exact name of the man page that you want to view. In these cases you can search for man pages that match a keyword by using the -k option to the man command.
For example, what if you knew you wanted a man page that displays how to change your password, but you didn't remember the exact name? You could run the command man -k password:
sysadmin@localhost:~$ man -k passwd                                    
chgpasswd (8)        - update group passwords in batch mode            
chpasswd (8)         - update passwords in batch mode                 
fgetpwent_r (3)      - get passwd file entry reentrantly               
getpwent_r (3)       - get passwd file entry reentrantly               
gpasswd (1)          - administer /etc/group and /etc/gshadow         
pam_localuser (8)    - require users to be listed in /etc/passwd      
passwd (1)           - change user password                           
passwd (1ssl)        - compute password hashes                        
passwd (5)           - the password file                               
passwd2des (3)       - RFS password encryption                         
update-passwd (8)    - safely update /etc/passwd, /etc/shadow and /etc/group    
sysadmin@localhost:~$
When you use this option, you may end up with a large amount of output. The preceding command, for example, provided over 60 results.
Recall that there are thousands of man pages, so when you search for a keyword, be as specific as possible. Using a generic word, such as "the" could result in hundreds or even thousands of results.
Note that on most Linux distributions, the apropos command does the same thing as man -k. On those distributions, both will produce the same output.





































Load disqus comments

0 comments