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:
- Up Arrow (
β
): Scroll backward through previous commands. - Down Arrow (
β
): Scroll forward through the 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
- Press
Ctrl+R
repeatedly to cycle through matches. - Press
Enter
to execute the found command. - Press
Esc
orCtrl+G
to exit the search without running a 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.
- Press
Tab
twice to see all possible completions. - Works for command options if shell completion scripts are installed.
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.
Navigating man
Pages
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:
- Temporary is defined in the current session.
- To make it permanent add alias definitions to your shell configuration file (e.g.,
~/.bashrc
).
echo "alias ll='ls -alF'" >> ~/.bashrc
Apply Changes:
source ~/.bashrc
Clearing the Terminal Screen
clear
Alternative Methods:
Ctrl+L
reset
(useful if the terminal gets garbled)
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
- 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
orbash
's built-in command history to trace the creation commands. - Display the description of the
cat
command using theman
command. Executeman cat
to read the manual page forcat
, which describes its usage and options. - 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 addingHISTSIZE=3000
and then source the file or restart the terminal. - Show the last five commands you typed using the
history
command. You can usehistory | tail -5
to display the last five commands from your command history. - 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. - 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 likeshopt -s histappend
to append history rather than overwriting it. - 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 howhistory | grep <keyword>
can be used to filter commands by a specific term. - Set up your shell to ignore duplicate commands in the history This can be achieved by setting the
HISTCONTROL
environment variable toignoredups
orignoreboth
in your shell configuration file (e.g.,~/.bashrc
). This helps in keeping the history clean and concise. - 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. - Investigate how to share command history between multiple terminal sessions. Look into the
PROMPT_COMMAND
environment variable and thehistory -a
command to append each command to the history file as it is executed. This setup can help synchronize history across open sessions. Additionally, usingshopt -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 ashistory-substring-search
or shared history management inzsh
.