Simple Bash/ZSH GOD-LIKE History Tricks Every Linux User Should Know!
A thread π§΅
Hello everyoneπ. For today's Linux threadπ§, I'll be showing some GOD-LIKE bash/zsh history tricks which will save you plenty of timeβ° and doing your day to day tasks on the Linux terminal.
Effectively using bash history will save you plenty of time in the Linux terminal. Not only this will save you plenty of time, but it will also make your life much easier when you are using the Linux Terminal to perfom day to day tasks as regular user, penterser, dev or sysadmin.
1β£View the Bash History
The simplest way to view commands you have used earlier is to use the history bash built in command.
The above command will show you a hundred or perhaps a thousand commands stored in your history. The size depends on HISTSIZE variable, which you can easily change if you wish to. Here is how you can change view and change your history size.
You can also limit the number of lines to display if you don't want your screen to be cluttered by using the history command together with -n/n option, where n is the number of lines you want to be displayed. For example, to show only the last 8 commands from the history.
Pro Tipπ‘: In BASH you use just n without -, in ZSH you use -n with a -.
2β£Searching Commands From the Bash History
There are two ways or several ways you can use to search through the bash history. Personally for me I prefer to use the history command, together with the grep command or a handy shortcut to do a reverse search through the history.
βΆοΈ Using Grep
Here issue the command history and pipe it's output as the input to the grep command and then specify an expression I'm searching for as a grep option.
βΆοΈ Using CTRL + R shortcut
A better and super handy way to search through history is to use the CTRL + R shortcut key to start the reverse search and type the string you are looking for.
This is a handy way as it allows us to execute the command once we found it unlike with grep where we have to retype the command once we found it.
For example, the example below searches for a command that had 'history' in it.
You can cycle through the choices by pressing CTRL + R repeatedly and when you find the command you were looking for, press CTRL + O or simply `enter` key to select and run that command. To come out of the reverse search simply press ESC or use CTRL + G.
3β£Run command from bash history
Another handy trick that bash offers is the use of the ! to run commands in the history. That means you can run a command by using !# where # is the position/number of the command in the history. The value of # can be either negative or positive.
You can rerun the last command with `!!` or `!-1`. A good use of this comes when you forget to use with a command and then quickly use it like this to run with sudo:
You may also run the last command starting with a certain text like this:
!text
Suppose you ran echo $HISTSIZE earlier and you want to run it again. You can use it like this:
The below command will run the last ran echo command from your history.
Pro Tipπ‘: If you have doubts with command, instead of running it, print it by adding `:p` to the end of it. So you use it like `!echo:p`, `!23:p` etc.
If you are using ZSH there's no need to do that as it will display the command before executing it. With BASH you should.
4β£Changing an executed command
You can also use history to rerun a command with different syntax.For example, if I want to change my previous commandΒ `history | grep ftp` to `history | grep ssh`, I can execute the following at the prompt:
$ ^ftp^ssh^
5β£Reuse arguments from previous commands in your history
Another God-Like Bash History trick is the use of !$ and !* this will substitutes/place in the last argument and arguments of the last command into your line respectively.
For example, say you were reading a file with the cat command and you decided you need to edit this file with vim or any editor of your choice. Instead of typing it entirely, you use the `!$` shortcut.
The above option gives you only the last argument of the previous command. If you want all the arguments, use this:
6β£Clearing your history
If you feel like you there are too many commands and you no longer want them or for some security reasons you don't people or other users to know the commands you have been using throughout your session you can simply clear with the option `-c` or -d.
This -c option will clear the present history of bash and start adding commands afresh from now onwards and the -d option will delete the specified command. The -d option works on BASH not ZSH
That's it! Thank you for reading today's threadπ. If you have other useful tricks I have missed, feel free to add them in the comments. Would love to know them as well. And be sure to rtπ, likeπ and πfollow me (@xtremepentest) for more future Linux & security content.
β’ β’ β’
Missing some Tweet in this thread? You can try to
force a refresh
Looking for a tool that simplifies API management? Introducing Apidog
Designing APIs can be complex, but tools like @ApidogHQ simplify the process. Apidog ( is a comprehensive API design-first development platform that offers numerous features to streamline API development. While many API design and management tools are available, Apidog stands out as a comprehensive solution.
Here's why Apidog is becoming the go-to platform for API design:
1. REAL API Design-first Development Platform
Apidog offers visual tools for API design, development, testing, and documentation. Its elegant UI and compatibility with OpenAPI Specification (OAS) eliminate the need for manual coding.
The platform also supports multi-branch API development, making it ideal for teams working on complex projects.
If you want to learn Linux bash scripting for FREE, open this:
1. Getting Started with Bash Scripting
Bash scripts are a series of commands written in a file that can be executed by the Bash shell. This article will cover the basics of creating and running your first Bash script. sysxplore.com/create-a-bash-β¦
2. Comments in Bash
Commenting your code is an essential practice in programming, as it helps you and others understand the purpose and functionality of each section.
In Bash scripting, comments are used to annotate your scripts, making them more readable and maintainable. This article will cover the different ways to add comments in your Bash scripts. sysxplore.com/comments-in-baβ¦
13 Linux tr command practical examples you should know as a system administrator:
The tr command short for translate, is one of the most useful command for manipulating text on the command line.
It allows you to perform useful operations such as converting lowercase characters to uppercase characters, uppercase characters to lowercase characters, character replacing, and deleting characters.
It is usually used in conjunction with other commands via piping.
Linux user management - how to add users in Linux π§΅β
The useradd command is the main tool for adding new users to a Linux system. This command allows you to quickly create a new user account and configure the user's $HOME directory structure.
The useradd command creates a user account by combining system default settings and command-line parameters.
To see the system default values on your Linux distribution, use the useradd command with the -D option: