Linux commands

Page: ()   1  2  3  4  5  6  ()

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:
With no options, display the command history list with line numbers. Lines listed with a * have been modified. An argument of n lists only the last nfilename is supplied, it is used as the name of the history file; if not, the value of lines. If HISTFILE is used. Options, if supplied, have the following meanings:

-c
Clear the history list by deleting all the entries.
-d offset
Delete the history entry at position offset.
-a
Append the ``new'' history lines (history lines entered since the beginning of the current bash session) to the history file.
-n
Read the history lines not already read from the history file into the current history list. These are lines appended to the history file since the beginning of the current bash session.
-r
Read the contents of the history file and use them as the current history.
-w
Write the current history to the history file, overwriting the history file's contents.
-p
Perform history substitution on the following args and display the result on the standard output. Does not store the results in the history list. Each arg must be quoted to disable normal history expansion.
-s
Store the args in the history list as a single entry. The last command in the history list is removed before the args are added.

The return value is 0 unless an invalid option is encountered, an error occurs while reading or writing the history file, an invalid offset is supplied as an argument to -d, or the history expansion supplied as an argument to -p fails.



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

Word designators are used to select desired words from the event. A `:' separates the event specification from the word designator. It may be omitted if the word designator begins with a `^', `$', `*', `-', or `%'. Words are numbered from the beginning of the line, with the first word being denoted by 0 (zero). Words are inserted into the current line separated by single spaces.

For example,

!! designates the preceding command. When you type this, the
preceding command is repeated in toto.

!!:$ designates the last argument of the preceding command.
This may be shortened to !$.

!fi:2 designates the second argument of the most recent command
starting with the letters fi.

Here are the word designators:

0 (zero) The 0th word. For many applications, this is the command word.

n The nth word.

^ The first argument; that is, word 1.

$ The last argument.

% The word matched by the most recent `?string?' search.

x-y A range of words; `-y' abbreviates `0-y'.

* All of the words, except the 0th. This is a synonym for `1-$'.
It is not an error to use `*' if there is just one word in the event;
the empty string is returned in that case.

x* Abbreviates `x-$'

x- Abbreviates `x-$' like `x*', but omits the last word.

If a word designator is supplied without an event specification,
the previous command is used as the event.

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.

t Remove all leading pathname components, leaving the tail.

r Remove a trailing suffix of the form `.suffix', leaving the basename.

e Remove all but the trailing suffix.

p Print the new command but do not execute it.

q Quote the substituted words, escaping further substitutions.

x Quote the substituted words as with `q', but break into words at
spaces, tabs, and newlines.

s/old/new/
Substitute new for the first occurrence of old in the event line.
Any delimiter may be used in place of `/'. The delimiter may be
quoted in old and new with a single backslash.
If `&' appears in new, it is replaced by old.
A single backslash will quote the `&'.
The final delimiter is optional if it is the last character on the input line.

& Repeat the previous substitution.

g Cause changes to be applied over the entire event line.
Used in conjunction with `s', as in gs/old/new/, or with `&'.





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
% make
% ./foo -f foo.conf
% vi foo.c bar.c

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.

last
...
mysurface tty7 :0 Mon Oct 6 20:07 - down (00:00)
reboot system boot 2.6.24.4-64.fc8 Mon Oct 6 20:06 (00:00)
mysurface pts/8 10.168.28.44 Mon Oct 6 17:42 - down (01:58)
mysurface pts/7 :0.0 Mon Oct 6 17:41 - 19:40 (01:59)
mysurface pts/6 :0.0 Mon Oct 6 17:27 - 19:40 (02:13)
mysurface pts/5 :0.0 Mon Oct 6 17:27 - 19:40 (02:13)
mysurface pts/5 :0.0 Mon Oct 6 15:52 - 15:59 (00:07)
...

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 “.

export HISTTIMEFORMAT="%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.

...
 994 2008-10-16 02:27:40 exit
 995 2008-10-16 01:12:20 iptables -nL
 996 2008-10-16 01:47:46 vi .bash_profile
 997 2008-10-16 01:47:55 history
 998 2008-10-16 01:48:03 . .bash_profile
 999 2008-10-16 01:48:04 history
 1000 2008-10-16 01:48:09 exit
 1001 2008-10-16 02:27:43 history
...

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

Picture of Dennis Daniels
by Dennis Daniels - Saturday, 5 September 2009, 10:27 PM
 

Nice to have a select all in the distribution question as well as an option, I don't know.


Page: ()   1  2  3  4  5  6  ()