In Linux and UNIX-like operating systems, the command-line history is a record of the commands that have been entered at the command prompt. The command-line history is stored in a file called .bash_history
in the user's home directory.
To view the command-line history, you can use the history
command. This command will display a list of the previously entered commands, along with their associated history number and command. For example:
$ history 1 ls 2 cd Documents 3 pwd 4 cat file.txt 5 history
You can also use the history
command with the -c
option to clear the command-line history.
$ history -c
To search for a specific command in the history, you can use the grep
command. For example, to search for all commands that contain the word "file":
$ history | grep file 4 cat file.txt
You can also use the !
symbol followed by the history number to execute a specific command from the history. For example, to execute the fourth command in the history:
$ !4 cat file.txt
It's worth noting that the command-line history is limited to a certain number of commands (usually 1000 by default). When the limit is reached, the oldest commands are deleted to make room for new commands. You can change the size of the command-line history by modifying the HISTSIZE
environment variable in the .bashrc
file in the user's home directory.
$ echo "HISTSIZE=2000" >> ~/.bashrc
This will increase the size of the command-line history to 2000 commands.