Last modified: October 10, 2024
This article is written in: 🇺🇸
Information and Navigation Commands
Let's explore important commands and techniques for efficiently retrieving information and navigating the command line. Understanding how to review past commands, access command documentation, and search for relevant tools are key skills for working effectively in the terminal.
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 whether it is possible to identify the exact command used to create a specific file. Explore tools such as
auditd
for auditing commands and the shell’s built-in history functions. Create a file and attempt to trace back the command that created it using your findings. - Use the
man
command to display the manual page for thegrep
command. Read through the available options, and then practice by usinggrep
to search for a specific term within a file on your system. Reflect on the value of the manual pages for command reference. - Increase the command history size in your shell to 5000 entries. Modify the
HISTSIZE
environment variable in your shell’s configuration file (such as~/.bashrc
), then reload the configuration file and confirm the new setting. Explore the benefits of having an extended command history for long-term use. - Display the last ten commands you’ve executed using the
history
command. Use the output to review your recent activity, and then clear the history. Verify that the commands are no longer accessible by re-checking the history log. - Examine where and how command history is saved when you close a shell session. Close a terminal, reopen it, and check the history file (e.g.,
~/.bash_history
) to confirm your previous commands were saved. Document how this process varies across different shells or configurations. - Open multiple terminal sessions and explore how command history is managed across them. Run several commands in each session and then close the terminals in various orders. Reopen a new session to see which commands have been saved and explore any settings that might influence this, such as
HISTFILE
andHISTCONTROL
. - Explore different methods to search through your command history. Practice using the
Ctrl+R
reverse search shortcut to quickly locate a past command by typing a keyword. Also, try usinghistory | grep <keyword>
to find specific commands from your history. Reflect on which method feels more efficient and why. - Configure your shell to ignore duplicate commands in the history to keep it clean and concise. Modify the
HISTCONTROL
variable by setting it toignoredups
orignoreboth
in your shell configuration file. Test this by entering duplicate commands and confirming that they are not saved in your history. - Find a way to exclude specific commands from being saved in your history. Experiment with prefacing a command with a space to prevent it from being recorded. Additionally, try setting the
HISTIGNORE
variable to filter out commands likels
,cd
, andexit
automatically. Check your history to verify the exclusions. - Set up a shared command history across multiple terminal sessions. Explore the use of
PROMPT_COMMAND
and thehistory -a
command to append each command to the history file as you execute it. Experiment withshopt -s histappend
to ensure history entries from all sessions are preserved when you close them, and reflect on how this might benefit your workflow.