There are several ways of working with command line history in a bash shell. The history command helps you check what commands have been typed previously. The bash command line history is usually stored in the file .bash_history in a user's home directory (that is; if you use bash as your user shell-which is true for Linux (I run the 3.0.0.12 kernel). You can check which shell is your default from the /etc/passwd file. It is usually the last value after the last colon on your username line e.g: user_name:x:1000:1000:FirstName LastName,,,:/home/my_user_name:/bin/bash The command line history is controlled by HISTSIZE and HISTFILE environment variables. The default HISTSIZE is 1000 and the default HISTFILE is ~/bash_history. This is where a history of your bash activity is stored. The HISTFILESIZE variable is set to 2000 (default). You can modify the defaults by editing the file ~/.bashrc (in your home directory) or /etc/skel/.bashrc for new subsequent users. In the .bashrc file, the line shopt -s histappend explicitly requests bash to append to the HISTFILE deleting this line will likely result in the contents of bash_history being overwritten every other time bash tries to save to history the commands that you have entered. An important thing to note about the bash history is that a command is written to history before bash actually expands the command. For example, the command ls Do* will be saved in ~/.bash_history as typed by the user before the bash shell expands it to list the contents of directories with names starting with the two letters “Do”. Also, if a user logs multiple times into a host, the last shell to be closed on that host will be the one whose commands are written to history. As with Linux/UNIX, there are many ways in which one can navigate the bash command line history. These include; Assuming that after typing the history command, we get this:
1688
sudo dhclient eth0
1689
ifconfig
1690
clear
1691
pgrep mysql
1692
kill 3071
1693
pgrep mysql
1694
ssh-keygen -R 192.168.1.1
1695 ssh
192.168.1.1 -l userxyz
1696
sudo ifconfig vboxnet0 192.168.1.20
1697
clear
1698 ssh
192.168.1.1 -l userxyz
1699
ssh-keygen -R 192.168.1.1
1700 ssh
192.168.1.1 -l user
1701
ifconfig
1702
ifconfig vboxnet0 down
1703
sudo ifconfig vboxnet0 down
1704 ssh
192.168.1.1 -l user
1705
pgrep mysql
1706
pgrep mysql | xargs kill
1707
pgrep mysql
1708
clear
1709
htop
1710
clear
1711
info whohas
1712 man
dpkg
1713
dpkg half-installed
1714 man
dpkg
1715
sudo apt-get purge mysql-query-browser
1716
sudo apt-get purge mysql-admin
1717
kill `pgrep mysql`
1718
sudo apt-get purge mysql-workbench-gpl
1719
sudo dpkg-reconfigure mysql-workbench-gpl
1720
sudo apt-get remove mysql-workbench-gpl
1721
clear
1722
VBoxManage startvm "Test"
1723
sudo bin/vm-manager start Dev
1724
ifconfig
1725
ifconfig -a
1726
VBoxManage controlvm Dev poweroff
1727
ifconfig
1728
clear
1729
ping 192.168.56.101
1730
VBoxManage list vms
1731
VBoxManage list runningvms
1732
clear
1733 env
| grep -i hist
1734
export
1735
clear
1736 ls
/etc/
1737 cat
/etc/environment
1738 cat
/etc/bash_completion
- Arrow keys: pressing the up arrow key navigates through previously entered commands. A similar action is accomplished by pressing Ctrl + P. To navigate forward, pressing the down arrow key moves through the next entered commands. This only works is one was previously navigating through earlier entered commands. Ctrl + N should work the same.
- !!: immediately
executes the previously/last entered command.
Using our history output above, !! would result in line 1738 being executed. - !n: executes
command number n. Example: Typing
!1735
would result in the clear command being executed.
- !-n:
executes command number: current command minus n command. If your
last command was history,
then !-1
executes that last command history.
Example: Typing !-3 would result in 1736 being executed (listing of /etc directory-if you have read privileges to the /etc directory)
- history: a list of previously entered commands is displayed. The number of commands displayed depends on the configured HISTSIZE value.
- !string: execute
the most recent command starting with string.
Example: Typing !VBox would execute the line 1731 which displays a list of currently running VirtualBox virtual machines (if you have VirtualBox installed) - !?string: execute
the most recent command containing string.
Example: Typing !?mysql would execute line 1720 (which removes mysql-workbench package)
- ^string1^string2:
repeat the last command replacing the first occurrence of the
string1
with string2.
Example: Typing ^mysql^postgres
would search the most recent command containing the word mysql
and replace it with the word postgres,
in our sample history file, this would replace line 1720: sudo
apt-get remove mysql-workbench-gpl
with this sudo
apt-get remove postgres-workbench-gpl
(if
there is any such package)
- Ctrl+R, string: This
command does a reverse search through history to find the most
recent command that contains string. The command does not have to
start with string. This brings a prompt (reverse-i-search:)
that requests you to enter the characters of the command that you
would like to execute. These characters do not have to be the entire
command or starting letters of the command. Any words in the command
are good enough. The more words of the command you type, the
narrower the search becomes making it easier to retrieve the command
that you want. Typing Ctrl +
R
repeats the search
further backwards through history until you get to the command you
want or reach the top of the bash_history
file.
No comments:
Post a Comment