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. Image
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. Image
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. Image
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. Image
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. Image
▶️ 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. Image
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. Image
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: Image
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. Image
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. Image
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^ Image
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. Image
The above option gives you only the last argument of the previous command. If you want all the arguments, use this: Image
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 Image
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
 

Keep Current with Xtreme Pentesting

Xtreme Pentesting Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @xtremepentest

26 Nov
A thread of my Linux threads🧵 Image
1. Useful Linux🐧 Terminal Keyboard⌨️ Shortcut Keys To Boost🚀 Your Productivity📈

2. Basic Linux 🐧Commands📜 For Text Manipulation.

Read 20 tweets
21 Nov
Useful Linux🐧 Terminal Keyboard⌨️ Shortcut Keys To Boost🚀 Your Productivity📈

A thread🧵

Hello everyone👋, Today I'll be doing another quick, easy to follow thread🧵on a few Linux terminal keyboard shortcuts keys that will help you to complete a few tasks real quick.
1. CTRL + A⌨️
2. CTRL + E⌨️
Read 14 tweets
19 Nov
Useful Linux🐧 Networking🌐 Commands📜 for Sysadmins/Regular Users🧑‍💻

A thread🧵
Hello everyone👋, Today I'll be doing another quick, easy to follow thread🧵 on some of the most used command-line tools and utilities for network management in Linux

#infosec #CyberSecurity #Linux
1. Ifconfig🌐
Ifconfig stands for Interface Configurator, it is one of the most used commands for finding network details, nitialize an interface, assign IP address, enable or disable an interface. It also display route and network interface.
2. Ip🌐
ip command is the latest version of ifconfig. It is more powerful than ifconfig command as it can perform several other tasks that the ifconfig cannot do. The utility is used for displaying and manipulating routing, network devices, interfaces.
Read 21 tweets
17 Nov
Basic Linux 🐧Commands📜 For Text Manipulation

A thread🧵

Hello everyone👋, Today I'll be doing a quick, easy to follow thread🧵 on basic Linux commands for text manipulation.
#infosec #cybersecurity #Linux
1. Echo🐧
The echo command is used to display line of text to the standard output(stdout).
2. Cat🐧
The cat command is used concatenate files and print their contents on the standard output. In other words it's just used to display the contents of a file.
Read 27 tweets
16 Nov
OSCP (Offensive Security Certified Professional) Pass and Preparation - Tips and Tricks💡

A thread🧵

#oscp #CyberSecurity #infosec
2. [0x4D31/awesome-oscp: A curated list of awesome OSCP resources (github.com)](github.com/0x4D31/awesome…)
Read 16 tweets
15 Nov
BEGINNER👦 LINUX PRIVILEGE 🔐ESCALATION⬆️
Abusing SUDO rights and popping r00t shells

A thread🧵
#infosec #CyberSecurity #bugbountytips Image
In this thread🧵, I will be discussing how you can pop a root shell by abusing SUDO rights misconfiguration and will be demonstrating this using this box: tryhackme.com/room/linuxpriv…
First things first, let’s start with theoretical concepts!!😄
What is SUDO👑?
Sudo (Superuser Do) - is a Unix and Linux based utility that gives users permissions to run commands at the `root` level (most powerful user).
Read 21 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Thank you for your support!

Follow Us on Twitter!

:(