Building your personal Linux cheat sheets


Linux man pages can be overwhelming to people who are just learning how to work on the command line, but here we’ll look at a way to quickly prepare a cheat sheet for a series of commands. These cheat sheets will tell new Linux users enough to get started and know what man page to read when they want to know more.

To get started, we’ll take a look at series of commands that any Linux newbie would need to learn:

alias   cmp     export  less    tail    whereis
apropos comm    grep    more    tar     who
cat     dd      head    passwd  top     whoami
chmod   df      kill    pwd     unzip   zip
chown   diff    killall sort    whatis

Next, we use a series of commands that will provide short descriptions of these commands. These are help -d, whatis, and a man command that selects only the command description from the man pages.

help -d

The help -d command will provide a one-line description for some of the bash built-ins.

$ help -d pwd
pwd - Print the name of the current working directory.

The man -f and whatis commands

The man -f command and the whatis command produce the same results. Here are some examples:

$ whatis cd
cd (1) - bash built-in commands, see bash(1)
$ man -f cd
cd (1) - bash built-in commands, see bash(1)

Getting the 4th line from the man page

The 4th line of each man page contains a short description of the command. The script below gets this by taking the top three lines (using the head command) and then only displaying the last of them (using the tail command).

Getting the description from the man page

To retrieve just the short description from a command’s man page, you can use a command like this:

$ man date | head -4 | tail -1
date - print or set the system date and time

Using a script

To use each of the methods described, you can run a script like the one shown below that runs through the series of commands you provide (listed in a file) and creates a cheat sheet using each of the commands described. This script calls the resultant files help1, help2, and help3. Note that it quiets any error message (e.g., when it can’t find a description for a particular command) and otherwise adds a line to one of the three files.

#!/bin/bash

# empty CheatSheet files if they exist
> help1; > help2; > help3
# get name of command list
echo -n "lisf of commands> "
read list
# sort command list
sort $list > $list$$
# run through command list and collect descriptions
for cmd in `cat $list$$
do
    help -d $cmd 2>/dev/null >> help1
    whatis $cmd 2>/dev/null | grep ^$cmd 2>/dev/null >> help2
    man $cmd | head -4 | tail -1 2>/dev/null >> help3
done
# remove sorted commands file
rm $list$$

Results

The results below show the top 10 lines from each of the files.

$ head -10 help1 help2 help3
==> help1 <==
alias - Define or display aliases.
command - Execute a simple command or display information about commands.
export - Set export attribute for shell variables.
kill - Send a signal to a job.
pwd - Print the name of the current working directory.

==> help2 <==
alias (1)            - bash built-in commands, see bash(1)
apropos (1)          - search the manual page names and descriptions
cat (1)              - concatenate files and print on the standard output
chmod (1)            - change file mode bits
chown (1)            - change file owner and group
cmp (1)              - compare two files byte by byte
comm (1)             - compare two sorted files line by line
dd (1)               - convert and copy a file
df (1)               - report file system disk space usage
diff (1)             - compare files line by line

==> help3 <==
       bash,  :,  .,  [, alias, bg, bind, break, builtin, caller, cd, command,
       apropos - search the manual page names and descriptions
       cat - concatenate files and print on the standard output
       chmod - change file mode bits
       chown - change file owner and group
       cmp - compare two files byte by byte
       comm - compare two sorted files line by line
       dd - convert and copy a file
       df - report file system disk space usage
       diff - compare files line by line

Wrap-Up

Using the commands described in this post, you can assemble a list of commands and quickly prepare a cheat sheet that contains a brief description of each of them. Select the resultant file that works best or join them into a single cheat sheet.

Copyright © 2023 IDG Communications, Inc.



Source link