TRΓ„W🀟 Profile picture
Nov 24, 2021 β€’ 22 tweets β€’ 7 min read β€’ Read on X
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 TRΓ„W🀟

TRΓ„W🀟 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 @thatstraw

Jul 11
Linux filesystem and directory structure🐧

πŸ“ /
β”œβ”€β”€ πŸ“ bin
β”‚ β”œβ”€β”€ βš™οΈ ls
β”‚ └── βš™οΈ bash
β”œβ”€β”€ πŸ“ boot
β”‚ β”œβ”€β”€ πŸ’Ύ vmlinuz
β”‚ └── πŸ’Ύ initrd.img
β”œβ”€β”€ πŸ“ dev
β”‚ β”œβ”€β”€ πŸ’Ώ sda1
β”‚ └── πŸ’Ώ sdb
β”œβ”€β”€ πŸ“ etc
β”‚ β”œβ”€β”€ πŸ› οΈ passwd
β”‚ └── πŸ› οΈ fstab
β”œβ”€β”€ πŸ“ home
β”‚ β”œβ”€β”€ πŸ“ james
β”‚ β”‚ β”œβ”€β”€ πŸ› οΈ .bashrc
β”‚ β”‚ β”œβ”€β”€ πŸ“ documents
β”‚ β”‚ └── πŸ“ .ssh
β”‚ β”‚ β”œβ”€β”€ πŸ” id_rsa
β”‚ β”‚ └── πŸ”‘ authorized_keys
β”‚ └── πŸ“ traw
β”‚ β”œβ”€β”€ πŸ› οΈ .profile
β”‚ β”œβ”€β”€ πŸ“ pictures
β”‚ └── πŸ“ .ssh
β”‚ β”œβ”€β”€ πŸ” id_rsa
β”‚ └── πŸ”‘ authorized_keys
β”œβ”€β”€ πŸ“ lib
β”‚ β”œβ”€β”€ βš™οΈ
β”‚ └── βš™οΈ
β”œβ”€β”€ πŸ“ media
β”‚ β”œβ”€β”€ πŸ“ cdrom
β”‚ └── πŸ“ usb
β”œβ”€β”€ πŸ“ mnt
β”‚ └── πŸ“ mymount
β”œβ”€β”€ πŸ“ opt
β”‚ └── πŸ“ myapp
β”œβ”€β”€ πŸ“ proc
β”‚ β”œβ”€β”€ πŸ“„ cpuinfo
β”‚ └── πŸ“„ meminfo
β”œβ”€β”€ πŸ“ root
β”‚ β”œβ”€β”€ πŸ› οΈ .bash_history
β”‚ └── πŸ“ .ssh
β”‚ β”œβ”€β”€ πŸ” id_rsa
β”‚ └── πŸ”‘ authorized_keys
β”œβ”€β”€ πŸ“ run
β”‚ β”œβ”€β”€ πŸ“„ utmp
β”‚ └── πŸ“„ systemd
β”œβ”€β”€ πŸ“ sbin
β”‚ β”œβ”€β”€ βš™οΈ init
β”‚ └── βš™οΈ reboot
β”œβ”€β”€ πŸ“ srv
β”‚ └── πŸ“ myservice
β”œβ”€β”€ πŸ“ sys
β”‚ β”œβ”€β”€ πŸ“„ devices
β”‚ └── πŸ“„ firmware
β”œβ”€β”€ πŸ“ tmp
β”‚ β”œβ”€β”€ πŸ“„ temp1
β”‚ └── πŸ“„ temp2
β”œβ”€β”€ πŸ“ usr
β”‚ β”œβ”€β”€ πŸ“ bin
β”‚ β”‚ β”œβ”€β”€ βš™οΈ perl
β”‚ β”‚ └── βš™οΈ python
β”‚ β”œβ”€β”€ πŸ“ lib
β”‚ β”‚ β”œβ”€β”€ βš™οΈ
β”‚ β”‚ └── βš™οΈ
β”‚ └── πŸ“ local
β”‚ β”œβ”€β”€ πŸ“ bin
β”‚ β”œβ”€β”€ πŸ“ lib
β”‚ └── πŸ“ share
└── πŸ“ var
β”œβ”€β”€ πŸ“ log
β”‚ β”œβ”€β”€ πŸ“„ syslog
└── πŸ“„ auth.log
β”œβ”€β”€ πŸ“ mail
β”œβ”€β”€ πŸ“ run
β”œβ”€β”€ πŸ“ spool
└── πŸ“ wwwlibc.so
libm.so
libssl.so
libcrypto.so
πŸ“ / (Root Directory)
β€’ The top-level directory of the filesystem. All other directories and files are contained within this directory.

πŸ“ /bin
β€’ Contains essential binary executables (commands like ls, bash). These are the basic commands needed for system operation.
πŸ“ /boot
β€’ Contains the boot loader files and kernel images (e.g., vmlinuz, initrd.img). These files are necessary to start the operating system.

πŸ“ /dev
β€’ Contains device files representing hardware devices (e.g., sda1, tty). These special files allow software to interact with hardware devices.
Read 10 tweets
Jul 9
REST API Design Image
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.

You can check out and try Apidog using this link:
apidog.com/?utm_source=th…Image
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.Image
Read 8 tweets
Jun 4
If you want to learn Linux bash scripting for FREE, open this: Image
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…
Read 19 tweets
Dec 29, 2023
Linux hand-written notes:🧡 ↓ Image
[2] Image
[3] Image
Read 80 tweets
Oct 24, 2023
13 Linux tr command practical examples you should know as a system administrator: Image
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.
Read 29 tweets
Jul 2, 2023
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:

$ useradd -D
Read 18 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(