Linux commands
| Command: history |
gives list of latest used commands (usually using up and down keys) http://linux.die.net/man/3/history |
|
Description of commmand / detailed explanation:
The Bourne Again Shell's history mechanism, a feature adapted from the C Shell, maintains a list of recently issued command lines, also called events, providing a quick way to reexecute any of the events in the list. This mechanism also enables you to execute variations of previous commands and to reuse arguments from them. You can replicate complicated commands and arguments that you used earlier in this login session or in a previous one and enter a series of commands that differ from one another in minor ways. The history list also serves as a record of what you have done. It can prove helpful when you have made a mistake and are not sure what you did, or when you want to keep a record of a procedure that involved a series of commands. |
|
|
Command options:
History Expansion History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly. History expansion takes place in two parts. The first is to determine which line from the history list should be used during substitution. The second is to select portions of that line for inclusion into the current one. The line selected from the history is called the event, and the portions of that line that are acted upon are called words. Various modifiers are available to manipulate the selected words. The line is broken into words (several words surrounded by quotes are considered one word). History expansions are introduced by the appearance of the history expansion character, which is `!' by default. Only `\' and `'' may be used to escape the history expansion character. Several shell options settable with the shopt builtin may be used to tailor the behavior of history expansion. The `-p' option to the history builtin command may be used to see what a history expansion will do before using it. The `-s' option to the history builtin may be used to add commands to the end of the history list without actually executing them, so that they are available for subsequent recall. Event Designators An event designator is a reference to a command line entry in the history list. ! Start a history substitution, except when followed by a space,
tab, the end of the line, `=' or `('.
!n Refer to command line n.
!-n Refer to the command n lines back.
!! Refer to the previous command. This is a synonym for `!-1'.
!string Refer to the most recent command starting with string.
!?string[?] Refer to the most recent command containing string.
The trailing `?' may be omitted if the string is followed
immediately by a newline.
^string1^string2^ Quick Substitution. Repeat the last command, replacing string1
with string2. Equivalent to !!:s/string1/string2/.
!# The entire command line typed so far.
Word Designators For example, Modifiers
After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a `:'. h Remove a trailing pathname component, leaving only the head. |
|
How to use it: history [n] || history -c || history -d offset || history -anrw [filename] || history -p arg [arg ...] || history -s arg [arg ...] |
|
|
Typical Usage Examples:
I think you all know that every time you input a command into a shell Linux places that command in a special system variable which is, surprise, called HISTORY. This “buffer” is used to hold in memory a finite, but configurable, number of commands the administrator or the user has used over time just to give you an idea this is the partial output of the history command on one of my lab machines : Lethe@ccielogs:$ [ccielogs@lethe ~]# history
... 110 ll 111 cat newpasswd 112 sed 's/Test/test/g' /etc/passwd > newpasswd 113 ll 114 cat newpasswd 115 w 116 who 117 ll ; w 118 w ; ll 119 history 120 history 114 121 cat newpasswd 122 rm newpasswd 123 cat /etc/passwd 124 vi /etc/passwd 125 cat /etc/passwd 126 clear 127 history ... [ccielogs@lethe ~]#
As you can see my Linux system registered a lot of the commands I’ve typed so far, this good both for reference and for taking advantage of its “do it quickly” approach as, for example, I was to launch again the command at line 112 all I have to do is to write the following : Lethe@ccielogs:$ [ccielogs@lethe ~]# !112
And automatically Linux would execute the command at line 112 (sed ‘s/Test/test/g’ /etc/passwd > newpasswd in this example), I think it’s clear how much time this can save you! Ok but what if I want to execute just the last command I’ve typed in? Yes I know you could use the up arrow to recall the last command, but an alternate way to do this is typing a double exclamation mark, that is !!, and Linux automatically will launch the last command,. But there is more, consider the following output from the history command : Lethe@ccielogs:$ [ccielogs@lethe ~]# history
128 pwd 129 pwd 130 pwd 131 history [root@lethe ~]# Do you notice anything weird here? Yes Linux, by default, won’t take care about duplicated commands into its HISTORY variable. This can be easily modified with a few simple steps. First you could simply launch the following command : Lethe@ccielogs:$ [ccielogs@lethe ~]# export HISTORY=ignoredups assume these are the last three commands you ran: % which firefox Getting stuff from the last command: Full line: % !! becomes: % vi foo.c bar.c Last arg : % svn ci !$ becomes: % svn ci bar.c All args : % svn ci !* becomes: % svn ci foo.c bar.c First arg: % svn ci !!:1 becomes: % svn ci foo.c Accessing commandlines by pattern: Full line: % !./f becomes: % ./foo -f foo.conf Full line: % vi `!whi` becomes: % vi `which firefox` Last arg : % vi !./f:$ becomes: % vi foo.conf All args : % ./bar !./f:* becomes: % ./bar -f foo.conf First arg: % svn ci !vi:1 becomes: % svn ci foo.c Ok now every time I input a command that is already in the shell’s history it’ll get ignored. If you want to make this change available to all users, or system wide if you prefer, just modify the .bashrc file so with the same line (expect the "export” command of course) but I’ll cover this in another post. History is a common command for shell to list out all the executed commands. It is very useful when it comes to investigation on what commands was executed that tear down the server. With the help of last command, you be able to track the login time of particular user as well as the the duration of the time he/she stays login.
If the command line history could provides the date time of the commands being executed, that may really narrow down the scope of the user actions that cause the server malfunction. By default, history do not append with timestamp, but it is easy to configure it to display timestamp, you just need to set one environment variable HISTTIMEFORMAT. HISTTIMEFORMAT takes format string of strftime. Check out the strftime manual to choose and construct the timestamp that suit your taste. My favorite is “%F %T “.
Execute history again and you will see the effect on the spot, bare in mind that the timestamp for command lines that executed at previous sessions may not valid, as the time was not tracked.
I would suggest you to put the export into ~/.bash_profile as well as /root/.bash_profile. In case you do not have .bash_profile, you can choose to put into ~/.bashrc. |
|
|
Available in: debian
fedora gentoo suse mandriva ubuntu |
Related commands: set -o history - Enable/Disable history
set history = 8 - Set the size of the history list. fc - Fix History Command hash - Remember the full pathname of a name argument time - Measure Program Resource Use |
|
Nice to have a select all in the distribution question as well as an option, I don't know. |