
I a developer from India, Interested in learning any tech stack. Actively searching for opensource projects to contribute and involve in collaborative learning.
Once upon a time, a user asked Linux where can I find my bash command history. Linux replied cat .bash_history in your root directory.
User: On executing, saw so many commands and asked how many will you store.
Linux: It depends on the environment variable HISTFILESIZE
User: echo $HISTFILESIZE, it gave 2000. Getting curious and asking how can I run a specific command from my history?
Linux: Executing history will get you to command history with line numbers. You can execute a command at a specific line with the syntax !<line-number> example: !4
@user:~$ history
1 apt get update
2 apt-get update
3 sudo apt-get update
4 sudo apt install nmap
5 ifconfig
6 sudo apt install net-tools
7 ifconfig
8 sudo nmap -sp 172.22.52.*
9 sudo nmap -sP 172.22.52.*
10 ls
@user:~$ !10
ls
example.sh generated_files
The amount of commands stored in history is managed by the environment variable
HISTSIZEUse
!!to execute the last command!-2will execute the command which was executed before the last 2 command executions. You can change the number and play with it.!<command_name>will execute the last execution of the command provided eg:!catwill execute the last cat command that you ran. If you just want to print the command to the console and not execute it use!<command_name:p>eg:!cat:pwill log to the terminal the complete cat command that you executed last time.
User: Is there any way to search for a command in bash history?
Linux: ctrl+r brings up the search, start typing a command to search for it, once you get the command you are looking for press enter or ctrl+p to execute it. You can also leave the search by using ctrl+g
User: Hey I accidentally executed some commands which I shouldn't have, how to delete them from history?
Linux: I know you will do that! use history -d <line-number> to delete a specific command with the line number from history eg: history -d 10 will remove 10th command that you executed in history. You can also delete the entire history by using history -c.
User: That's a lot of info, let me practice a few.
Linux: As if you are going to.. with a sarcastic smile!
Linux will return.
