所有的linux命令歷史,保存在~/.bash_history文件中。
如果想清空:
rm ~/.bash_history && history -c
默認歷史保存500條??梢赃@樣修改:
- 僅對當(dāng)前有效:
執(zhí)行:
HISTSIZE=1000
2.永遠有效:
在~./bashrc中加入HISTSIZE=<number of entries, -1 for unlimited>
然后:. ~/.bashrc
在用history看到命令行,如果你想要再次執(zhí)行,只需要執(zhí)行 !行號就行了。
比如:
$history
1 ls
2 clear
3 mv abc.txt def
$ !3
就會再次執(zhí)行mv abc.txt def這個命令。
- 執(zhí)行上一條命令:
!-1或!!,或sudo !!(如果你需要root權(quán)限) - 執(zhí)行倒數(shù)第2條命令:
!-2
如果剛才的命令有輸入錯誤,想更正的話只需要替換就好了。這樣:
$ ls -hal cleann.sh
ls: cleann.sh: No such file or directory
//這里不小心多打了一個n,clean錯打到了cleann,所以提示找不到此文件。
$ ^nn^n //把nn替換成n,重新執(zhí)行剛才的命令,就對了
ls -hal clean.sh
-rwxr-xr-x@ 1 aidenfang staff 585B Oct 18 20:58 clean.sh
Ref: https://askubuntu.com/questions/624848/view-history-of-commands-ran-in-terminal