Use the linux shell for help before you search the Internet

Very often I find myself searching for information about a command or searching the various options available from a command. Well, guess what, most of the information that we need is inside our own Linux shell and we can use the command-line interface to read through them.

In this post, we will discuss some of the built-in basic commands that may be used for obtaining the meta-information about a command.

man

Reading detailed information about a command. You can use the man command to read about the manual pages of almost any command.

man ls
# Trimmed down output
LS(1)                                                              User Commands                                                             LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).  Sort entries alphabetically if none of -cftuvSUX nor --sort is speci‐
       fied.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --all
              do not ignore entries starting with .

       -A, --almost-all
              do not list implied . and ..

       --author
              with -l, print the author of each file

       -b, --escape
              print C-style escapes for nongraphic characters

       --block-size=SIZE
              scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE format below

       -B, --ignore-backups
              do not list implied entries ending with ~

       -c     with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime  and  sort  by  name;
              otherwise: sort by ctime, newest first
...

info

Works pretty similar to the man command. But in general displays less information by displaying the documentation in info format. e.g

info whoami
Next: groups invocation,  Prev: logname invocation,  Up: User information

20.3 ‘whoami’: Print effective user ID
======================================

‘whoami’ prints the user name associated with the current effective user
ID.  It is equivalent to the command ‘id -un’.

   The only options are ‘--help’ and ‘--version’.  *Note Common
options::.

   An exit status of zero indicates success, and a nonzero value
indicates failure.

whatis

This command displays a single line of information about the usage of another command. The output from the man command can be sometimes too long to read and hence this works like a charm. e.g

whatis grep
grep (1)             - print lines matching a pattern

It is actually equivalent to man command with the -f option.

--help

You can specify this option alongside almost any command to show the various options available. This also displays the proper usage of the command. e.g

mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

apropos

This is one of the most powerful commands that helps you find stuff in case you know what you want. It can be used to search through the names and descriptions in manual pages(basically works like a search engine like google, duckduckgo, etc for commands.). If you do not specify any options, the results are generally too long and hence difficult to use. When used with the option -e (used to specify searching each keyword for exact matching), the results are far less and easier to use. For example, if we want to search for a command that has the exact words "copy files" inside their manual pages, we can do: 

apropos -e "copy files"                                                                                  
cp (1)               - copy files and directories
cpio (1)             - copy files to and from archives
install (1)          - copy files and set attributes 

This is equivalent to using the man command with the -k option.


The irony of the matter is that you found this post on the internet. Nevertheless, we hope it helps you in finding out the relevant information about a command that helps you in your end goal. In case you are interested in learning about some other commands, you may visit this page.

Till, we meet again in another blog post, keep hacking !!!

0 Comments

Join discussion:

Login