Last modified: September 27, 2024

This article is written in: πŸ‡ΊπŸ‡Έ

Information and Navigation Commands

This comprehensive guide covers essential commands and techniques for retrieving information about command-line utilities and effectively navigating the terminal. It includes detailed explanations of the history, man, and apropos commands, along with valuable tips for command-line navigation and efficiency.

Command History and Navigation

Efficient navigation and command recall are crucial skills for any command-line user. The shell provides several features to help you manage your command history and navigate through previous commands with ease.

The history Command

The history command displays a list of commands you've executed in the current shell session. This allows you to review, re-run, or modify previous commands without retyping them entirely.

I. Display Command History:

history

This outputs a numbered list of your recent commands.

II. Limit the Number of Entries Displayed:

history 20

Shows the last 20 commands.

III. Search Command History:

You can pipe the output to grep to search for specific commands:

history | grep "search_term"

Command Recall and Editing

I. Navigating Command History:

II. Reverse Search with Ctrl+R:

Press Ctrl+R to initiate a reverse incremental search. Start typing, and the shell will dynamically search for matching commands in your history.

(reverse-i-search)`keyword': matched_command

III. Executing Commands from History:

By Line Number:

!<number>

Executes the command corresponding to the given history line number.

Example:

!42

Runs command number 42 from the history list.

By Command Prefix:

!<prefix>

Repeats the most recent command starting with the specified prefix.

Example:

!git

Repeats the last command that started with git.

IV. Modifying Previous Commands:

Repeat Last Command with Substitution:

^old^new

Repeats the last command, replacing old with new.

Example:

^foo^bar

If the last command was echo foo, this would execute echo bar.

V. Suppressing Command History:

Space Prefix:

Starting a command with a space prevents it from being saved in the history (if HISTCONTROL includes ignorespace).

sensitive_command

Note: Ensure HISTCONTROL is set appropriately:

export HISTCONTROL=ignorespace

Suppressing and Clearing Command History

I. Clear Current Session History:

history -c

Removes all commands from the current session's history.

II. Write Current Session History to File:

history -w

Writes the current session's history to the history file (e.g., ~/.bash_history), overwriting it.

III. Append Current Session History to File:

history -a

Appends the session's commands to the history file without overwriting.

Auto-Completion

I. Tab Completion:

Press the Tab key to auto-complete commands, file names, directories, and more.

II. Enhanced Auto-Completion:

Install and configure tools like bash-completion to improve auto-completion capabilities.

sudo apt install bash-completion

The Manual (man) Pages

The man command accesses the manual pages, which provide detailed documentation for command-line utilities, configuration files, and system calls.

Understanding man Pages

I. Basic Usage:

man <command/>

Example:

man ls

Displays the manual for the ls command.

II. Viewing a Specific Section:

man <section_number> <command/>

Example:

man 5 passwd

Views the man page for the passwd file format (section 5), not the passwd command.

Man pages use the less pager for navigation:

Action Command
Scroll Down (line by line) Enter
Scroll Down (page by page) Space
Scroll Up b (back one page)
Go to End G
Go to Beginning g
Search Forward /search_term, then Enter
Search Backward ?search_term, then Enter
Next Match n
Previous Match N
Exit the Man Page q

Man Page Sections

Manual pages are organized into numbered sections:

Section Description
1 Executable programs or shell commands
2 System calls (kernel routines)
3 Library calls (functions within libraries)
4 Special files (usually in /dev)
5 File formats and conventions
6 Games and screensavers
7 Miscellaneous (macro packages, conventions)
8 System administration commands
9 Kernel routines [Non-standard]

Viewing All Sections:

man -a <command/>

Example:

man -a intro

Displays the intro man page for each section.

Searching Within man Pages

I. Keyword Search with -k:

man -k <keyword>

Equivalent to apropos.

Example:

man -k print

II. Limiting Search to a Section:

man -s <section_number> -k <keyword>

Example:

man -s 2 -k open

The apropos Command

The apropos command searches the man page descriptions for instances of a keyword, helping you find commands related to a particular topic.

Using apropos Effectively

I. Basic Usage:

apropos <keyword>

Example:

apropos network

Lists all commands and functions related to networking.

II. Exact Match Search:

apropos -e <exact_keyword>

Example:

apropos -e zip

Finds entries where the keyword is exactly zip.

III. Using Regular Expressions:

apropos -r <regex>

Example:

apropos -r '^git.*'

Searches for entries starting with git.

IV. Limiting Results to a Section:

apropos -s <section_number> <keyword>

Example:

apropos -s 2 open

Updating the Man Database

I. Rebuilding the Man Database:

If apropos returns incomplete or outdated results, update the man database:

sudo mandb

II. Specifying Man Path:

sudo mandb -c /usr/share/man

Additional Commands and Tips

Enhance your command-line proficiency with these additional tools and techniques.

The whatis Command

Provides a brief description of a command, similar to a dictionary definition.

I. Usage:

whatis <command/>

Example:

whatis ls

II. Multiple Commands:

whatis ls pwd cd

The type Command

Displays how the shell interprets a given command, indicating if it's a built-in, alias, function, or external executable.

I. Usage:

type <command/>

Examples:

type cd
type ls
type ll

II. Verbose Output:

type -a <command/>

Lists all instances found in the PATH.

The which Command

Shows the full path of the command executable that the shell would run.

I. Usage:

which <command/>

Example:

which python

II. All Matches in PATH:

which -a <command/>

Aliases in the Shell

Aliases allow you to define custom shortcuts for commands.

I. Creating an Alias:

alias <name>='<command/>'

Example:

alias ll='ls -alF'

II. Viewing All Aliases:

alias

III. Removing an Alias:

unalias <name>

IV. Temporary vs. Permanent Aliases:

echo "alias ll='ls -alF'" >> ~/.bashrc

Apply Changes:

source ~/.bashrc

Clearing the Terminal Screen

clear

Alternative Methods:

Advanced Topics

Configuring Command History

Fine-tune how your shell handles command history with environment variables.

Action Command
In-Memory History Size export HISTSIZE=1000
History File Size export HISTFILESIZE=2000
Ignore Duplicate Commands export HISTCONTROL=ignoredups
Ignore Commands Starting with Space export HISTCONTROL=ignorespace
Combine Options (Ignore both above) export HISTCONTROL=ignoreboth
Time Stamps in History export HISTTIMEFORMAT="%F %T "

Command-Line Shortcuts

Enhance efficiency with keyboard shortcuts.

Here are the details formatted into three separate markdown tables:

Movement

Action Command
Move to the beginning of the line Ctrl+A
Move to the end of the line Ctrl+E
Move backward one word Alt+B
Move forward one word Alt+F

Editing

Action Command
Delete from cursor to end of line Ctrl+K
Delete from cursor to beginning of line Ctrl+U
Delete word before the cursor Ctrl+W
Yank (paste) the last killed text Ctrl+Y

Process Control

Action Command
Cancel the current command Ctrl+C
Suspend the current process Ctrl+Z
Resume a suspended process in the foreground fg

Challenges

  1. Investigate if there's a way to determine the command used to create a file. Hint: you may want to look into shell history and command audit tools. Consider exploring tools like auditd or bash's built-in command history to trace the creation commands.
  2. Display the description of the cat command using the man command. Execute man cat to read the manual page for cat, which describes its usage and options.
  3. Increase the number of commands your command history "remembers" to 3000. This involves changing the HISTSIZE environment variable. You can set this in your shell's configuration file (e.g., ~/.bashrc) by adding HISTSIZE=3000 and then source the file or restart the terminal.
  4. Show the last five commands you typed using the history command. You can use history | tail -5 to display the last five commands from your command history.
  5. When you close the shell, where do the history commands go? They're typically saved in a history file, such as ~/.bash_history for bash. The history is saved automatically when the shell session is closed, preserving the commands for future sessions.
  6. Investigate what happens to the history when you have multiple terminal sessions open. How are commands saved in the history and what happens when sessions are closed in different orders? Look into how different shells handle command history across multiple sessions. Consider how HISTFILE, HISTCONTROL, and related settings affect the saving and merging of history entries when sessions close. Note that newer commands may overwrite older history entries, and closing sessions in different orders can affect which commands are ultimately saved. Additionally, explore settings like shopt -s histappend to append history rather than overwriting it.
  7. Explore how to search your command history efficiently. Use Ctrl+R to initiate a reverse incremental search and find commands quickly by typing part of the command or a keyword. Additionally, investigate how history | grep <keyword> can be used to filter commands by a specific term.
  8. Set up your shell to ignore duplicate commands in the history This can be achieved by setting the HISTCONTROL environment variable to ignoredups or ignoreboth in your shell configuration file (e.g., ~/.bashrc). This helps in keeping the history clean and concise.
  9. Find a way to prevent specific commands from being saved in your history. Commands prefixed with a space are not saved to the history. Alternatively, you can set HISTIGNORE to a pattern in your shell configuration file to exclude certain commands automatically. For example, HISTIGNORE="ls:cd:exit" will prevent these commands from being recorded.
  10. Investigate how to share command history between multiple terminal sessions. Look into the PROMPT_COMMAND environment variable and the history -a command to append each command to the history file as it is executed. This setup can help synchronize history across open sessions. Additionally, using shopt -s histappend ensures that history is appended rather than overwritten when sessions end. Explore tools or shell options that facilitate history sharing across sessions, such as history-substring-search or shared history management in zsh.

Table of Contents

    Information and Navigation Commands
    1. Command History and Navigation
      1. The history Command
      2. Command Recall and Editing
      3. Suppressing and Clearing Command History
      4. Auto-Completion
    2. The Manual (man) Pages
      1. Understanding man Pages
      2. Navigating man Pages
      3. Man Page Sections
      4. Searching Within man Pages
    3. The apropos Command
      1. Using apropos Effectively
      2. Updating the Man Database
    4. Additional Commands and Tips
      1. The whatis Command
      2. The type Command
      3. The which Command
      4. Aliases in the Shell
      5. Clearing the Terminal Screen
    5. Advanced Topics
      1. Configuring Command History
      2. Command-Line Shortcuts
    6. Challenges