1. history作用 fP V n;
,T`,OZm
linux的history命令的作用是,记录执行过的命令。 EG;E !0
用法: Pal=I)
history [n] n为数字,列出最近的n条命令 XS">`9o!
-c 将目前shell中的所有history命令消除 -X~mW
history [-raw] histfiles i*Ldec^
-a 将目前新增的命令写入histfiles, 默认写入~/.bash_history P=5+I+
-r 将histfiles内容读入到目前shell的history记忆中 lU%}_!tp3/
-w 将目前history记忆的内容写入到histfiles L_NiU;cr%
[plain] view plain copy ylKK!vRHT
shell > history 8s@N NjV
34 14-10-28 16:19:24 ll DzQBWY]
)
35 14-10-28 16:19:26 vim test 0(D^NtB7
36 14-10-28 16:19:33 ll ba);f[>
37 14-10-28 16:19:34 ll \A"o[A2v
展示3行 ^ RA'E@"
[plain] view plain copy `t&{^ a&Y"
shell > history 3 Xq*^6*E-}
1032 14-11-02 16:10:41 history Ki\.w~Qs
1033 14-11-02 16:10:46 history |more /|1p7{km
1034 14-11-02 16:11:15 history 3 Nr4:Gih
使用! 执行历史命令。 h'):/}JPl
! number 执行第几条命令 \^c4v\s<o#
! command 从最近的命令查到以command开头的命令执行 GiV%Hcx
!! 执行上一条 =|S%Rzsk
[plain] view plain copy 8Xotly
shell > !1046 BBB@M
history 3 F4EAC|Y
1045 14-11-02 16:22:38 head ~/.bash_history tS'lJu
1046 14-11-02 16:35:37 history 3 Fb5U@X/vE
1047 14-11-02 16:35:48 history 3 EKwQ$?I
shell > !! =ejj@c
history 3 Ps!MpdcL3
1046 14-11-02 16:35:37 history 3 ]pax,|+$C
1047 14-11-02 16:35:48 history 3 :Ha/^cC/3
1048 14-11-02 16:35:52 history 3 "O&93#8
shell > !head ?+yr7_f3*
head ~/.bash_history Bt+^H6cb
#1414484377 `ONjEl
cd update/ *o6}>;
#1414484377 ;[v!#+yml
2. history配置修改 CdolZW-!"
{i5?R,a)
history记录的行数 9BCW2@Kp
[plain] view plain copy bjZJP\6
shell > echo $HISTSIZE "[ bkdL<
1000 88g3<&
shell > S*h52li
默认记录1000行 NVyBEAoh
配置文件在/etc/profile中修改 ]g_VPx"
[plain] view plain copy H|9t5
HISTSIZE=1000 h<9s&
p
export HISTSIZE wq)*bIv
历史命令文件记录在 ~/.bash_history中 48^-]};
iHa?b2=)
想要让linux的history命令显示时间,history是默认不带时间, 5E!C?dv(z
在/etc/profile 中增加 e~r/!B5X
[plain] view plain copy UhxM85M;x
export HISTTIMEFORMAT="%y-%m-%d %H:%M:%S " j-<]OOD
查看.bash_history _&<n'fK[
[plain] view plain copy 7v: XAU
shell > head ~/.bash_history +8Xjk\Hi
#1414484377 L2}\Ah"[
cd update/ ]\,?u /
#1414484377 v"*r %nCi
ll IH|zNg{\Y
#1414484388 5ggyk0
vim address VB |?S|<
#1414484439 3CCs_AO
ll v{u3[c
#1414484440 i~@gI5[k+
ll H.5
6
shell > wNB?3v{n
U2Ve @.
3. 同一账号同时多次登录写入history #S>N}<>
pF;.nt)
普通情况下, 当以bash登录系统时,系统会从~/.bash_history读取以前运行的命令 B'Yx/c&n
当注销时,把最新的1000(HISTSIZE)条命令更新到~/.bash_history文件中。 (,At5T
也可以使用history -w强制立刻写入,仅保留最新的。
f<o|5r
z)Rkd0/X
当同一账号,同时登录多个bash时,只有最后一个退出的会写入bash_history,其他的都被覆盖了。 d59rq<yI
x{H+fq,M
4. Ctrl+r 反向查询历史命令 e$LC
3.Fko<D4jD
使用Ctrl+r反向查询历史命令,将匹配的最新一条显示出来 {_7hX`p
如果还想继续向上查询,继续按Ctrl+r 1P)K@j
[plain] view plain copy G^'We6<
shell > history 3 6 (rm%c
1048 14-11-02 16:35:52 history 3 ZayJllaq^
1049 14-11-02 16:36:11 head ~/.bash_history xI,3(A.
1050 14-11-02 16:41:05 history 3 enk`I$Xx
(reverse-i-search)`his': head ~/.bash_history