Home Linux Commands How To Effortlessly Retrieve Commands From Linux Command History Like a Pro

How To Effortlessly Retrieve Commands From Linux Command History Like a Pro

Linux Command Line Hacks: How to Easily Access Your Command History

By sk
Published: Updated: 1.8K views

In our daily work with Linux, we regularly execute both simple and complex commands. While short and simple commands are easy to recall, remembering long and complex ones with multiple flags can be challenging. In this brief guide, I'll outline some easy methods to effortlessly retrieve commands from history in Linux.

Retrieve Commands From History in Linux

Retrieving lengthy commands from the history file in Linux can be made easier with a few tips and tricks. Not just lengthy commands, you can track down all executed commands as described in the following sections.

1. Use the history Command:

Simply type history to list recent commands. You can then scroll through this list to find the command you're looking for.

Example:

$ history

2. Search History with Ctrl+R:

Press Ctrl+R and start typing a part of the command. This initiates a reverse search through your history. Keep pressing Ctrl+R to cycle through matches.

Example:

Press Ctrl+R and type ssh to find the last used ssh command.

Search History with Ctrl+R
Search History with Ctrl+R

3. History with Line Numbers:

Use history | nl to display your history with line numbers. You can then execute a command directly from history using !number, where number is the line number from the history.

Example:

Type !25 to execute the command listed as line number 25 in the history.

$ !25
cat /etc/fedora-release 
Fedora release 39 (Thirty Nine)
Run n-th Command from History in Linux
Run n-th Command from History in Linux

4. Use grep with History:

You can grep your history by using history | grep 'search-term'. This is useful for finding commands that contain a specific word or phrase.

Example:

$ history | grep 'ssh'

This finds all commands in your history that include 'ssh'.

6  sudo systemctl status sshd
7  sudo systemctl start sshd
8  sudo systemctl enable sshd
9  sudo systemctl status sshd
131  history | grep 'ssh'
Use grep with History
Use grep with History

As you see in the output above, all commands that include ssh are displayed along with their sequence number. This sequence number can be used in place of the command ID for execution using the ! bang symbol.

For example, running !6 would execute the sudo systemctl status sshd command.

Suggested Read: The Grep Command Tutorial With Examples For Beginners

5. Aliases for Long Commands:

If you regularly use a long command, consider creating an alias for it in your .bashrc or .bash_profile. This way, you can execute the command using a short, memorable name.

Example:

Add alias ll='ls -alF' to your .bashrc file. Typing ll now executes the ls -alF command.

Suggested Read: The alias And unalias Commands Explained With Examples

6. Scripts for Complex Commands:

For very complex commands, especially those with many arguments and flags, consider saving Linux commands as scripts. This way, you can execute them with a simple command.

Example:

Write a script called backup.sh with your complex backup command. Run the script using ./backup.sh instead of typing the whole command.

A while ago, we made a simple Rsync script to backup files from a remote a Remote system. Go to the following link to learn how to create a rsync script and how to backup files.

7. Bookmark Commands:

You can 'bookmark' a long command by appending a unique comment to it. For instance, your-long-command #uniqueTag. Later, you can easily find it by searching for the unique tag.

Example:

Visit the following link to know how to apply tags to commands and how to use the tags to retrieve the command associated with that tag.

8. Use fc Command:

The fc command opens the last command in your default text editor. You can also specify a range like fc 10 20 to open commands 10 to 20 in the editor.

Example:

$ fc -l
Use fc Command
Use fc Command

The output will display a list of commands with their IDs. You can then identify the ID for the command you want to execute.

Once you have identified the command ID, you can use the ! bang symbol followed by the ID to execute the command. For instance, if the ID for the command sudo dnf install vim is 10, you would use:

$ !10

To learn more about fc command, visit the following link.

9. History Expansion:

Use history expansion features like !! to refer to the last command, !-n for the nth last command, and !string to execute the last command starting with 'string'.

Execute Last Command
Execute Last Command

We have compiled a few methods to repeat a previously executed commands from the history. Refer the following guide for more details.

10. Extended History Control:

You can configure your .bashrc or .bash_profile to increase the size of your history file and control what gets saved. For example, HISTSIZE and HISTFILESIZE variables define the size of the history.

11. Use External Tools:

Many individuals have developed a wide range of applications and tools to save, bookmark, and manage commands and snippets in Linux and Unix-like systems.

We already have tried and reviewed some of them in the past. If you're interested to know about them, refer the following guides.

Conclusion

In this tutorial, we discussed a few useful tips to track down commands from the history file in Linux.

By using these tips, you can efficiently navigate through your command history and retrieve lengthy commands without needing to retype them.

It's your turn. What are your effective ways to track down commands from your shell history? Let us know via the comment section below.

Related Read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More