記錄歷史命令,history,命令補全和別名、通配符、輸入輸出重定向
shell是一個命令解釋器,提供用戶與機器之間的交互,支持特定的語法(邏輯判斷、循環等);
每個用戶都可以有自己特定的shell;
centos7默認shell為bash,其他shell還有zsh、ksh等;
命令歷史
history命令:
可以查看歷史命令;
在用戶的家目錄下的.bash_history文件中保存著之前敲過的命令,
默認最大存儲1000條;
history命令可以查詢;
更改存儲數:
更改變量HISTSIZE來達到更改存儲數;
編輯文件vim /etc/profile
vim /etc/profile
修改HISTSIZE值,將HISTSIZE=1000改為5000
HISTSIZE=5000
更新緩存文件
source /etc/profile
查看變量值
[root@shu-test ~]# echo $HISTSIZE
5000
[root@shu-test ~]#
給history命令加上時間與日期
臨時生效:
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@shu-test ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [root@shu-test ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [root@shu-test ~]# history | grep 2000 2000 2018/01/10 18:34:46 tar -zxvf httpd-2.2.34.tar.gz 2041 2018/01/10 19:31:53 history | grep 2000 [root@shu-test ~]#
永久生效方法:
vim /etc/profile
增加變量定義
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
更新緩存
[root@shu-test ~]# source /etc/profile
[root@shu-test ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@shu-test ~]#
特殊命令
-
!!:表示執行上一條命令;
[root@shu-test ~]# pwd /root [root@shu-test ~]# !! pwd /root [root@shu-test ~]#
-
!n:這裏的n表示數值,,表示執行命令歷史中的第n條指令;
[root@shu-test ~]# history | grep 1002 1002 2018/01/10 18:34:46 rmdir 123 2023 2018/01/10 18:45:15 history |grep 1002 2048 2018/01/10 19:44:16 history | grep 1002 2050 2018/01/10 19:45:21 history | grep 1002 [root@shu-test ~]# !2050 history | grep 1002 1002 2018/01/10 18:34:46 rmdir 123 2023 2018/01/10 18:45:15 history |grep 1002 2048 2018/01/10 19:44:16 history | grep 1002 2050 2018/01/10 19:45:21 history | grep 1002 [root@shu-test ~]#
- !字符串:表示執行歷史命令中最近一次一字符串開頭的命令,必須兩個以上;
[root@shu-test ~]# !pw pwd /root [root@shu-test ~]#
命令補全和別名
命令補全
centos7支持命令參數補全;
centos6只支持命令補全,不支持參數補全;
安裝bash-completion包:
yum install -y bash-completion
安裝後,重啟生效;
別名
當常用的命令與參數過長,我們可以定義一個別名來實現;
格式:
alias [別名] = ‘[源命令]‘
systemctl restart network.service
將這條重啟網卡服務的命令定義一個新的命令restartnet
[root@shu-test ~]# alias restartnet=‘systemctl restart network.service‘
[root@shu-test ~]# alias
alias cp=‘cp -i‘
alias egrep=‘egrep --color=auto‘
alias fgrep=‘fgrep --color=auto‘
alias grep=‘grep --color=auto‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias restartnet=‘systemctl restart network.service‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@shu-test ~]#
[root@shu-test ~]# rest
restartnet restorecon
取消別名
格式:
unalias [自定義別名]
[root@shu-test ~]# unalias restartnet
[root@shu-test ~]# alias
alias cp=‘cp -i‘
alias egrep=‘egrep --color=auto‘
alias fgrep=‘fgrep --color=auto‘
alias grep=‘grep --color=auto‘
alias l.=‘ls -d .* --color=auto‘
alias ll=‘ls -l --color=auto‘
alias ls=‘ls --color=auto‘
alias mv=‘mv -i‘
alias rm=‘rm -i‘
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
[root@shu-test profile.d]# restartnet
-bash: restartnet: 未找到命令
[root@shu-test profile.d]#
別名存放目錄:
用戶家目錄下:~/.bashrc文件;
其他命令目錄:/etc/profile.d/目錄下的
通配符
- :在bash下,可以用來匹配零個或多個字符;
- ?:在bash下,?號表示匹配一個字符;
- [n-n]:n表示數值,n-n表示範圍,例如:[0-3]表示範圍0到3;
- *實驗1:使用來查詢;**
[root@shu-test abc]# ls *txt 1.txt 2.txt 3.txt [root@shu-test abc]# ls *.txt 1.txt 2.txt 3.txt [root@shu-test abc]#
實驗2:使用?來查詢;
[root@shu-test abc]# ls ?.txt
1.txt 2.txt 3.txt
[root@shu-test abc]# ls 1?txt
1.txt
[root@shu-test abc]#
實驗3:使用【n-n】方括號範圍來查詢;
[root@shu-test abc]# ls
1.txt 2.txt 3.txt a
[root@shu-test abc]# ls [0-2].txt
1.txt 2.txt
[root@shu-test abc]#
實驗4:查詢範圍數字0-9的.txt 小寫字母 大寫字母.txt;
[root@shu-test abc]# ls [0-9a-zA-Z].txt
1.txt 2.txt 3.txt a.txt B.txt
[root@shu-test abc]#
實驗5:“與”語句查詢;
[root@shu-test abc]# ls [a].txt
a.txt
[root@shu-test abc]# ls [a3].txt
3.txt a.txt
[root@shu-test abc]#
實驗6:花括號
[root@shu-test abc]# ls {1,2,a}.txt
1.txt 2.txt a.txt
[root@shu-test abc]#
輸入輸出重定向
- >:輸出重定向,將一個字符串輸出到一個文本中;輸入兩次後只計算後面一次;
- >>:追加重定向,將一個字符串追加輸入到一個文本中;
- <:輸入重定向;
- 2>:錯誤重定向,將錯誤信息重定向到某個文本中;
- 2>>:錯誤追加重定向,將錯誤信息追加到某個文本中;
實驗1:輸出重定向;
[root@shu-test abc]# echo "123" > x.txt
[root@shu-test abc]# echo "456" > x.txt
[root@shu-test abc]# cat x.txt
456
[root@shu-test abc]#
實驗2:追加重定向;
[root@shu-test abc]# cat x.txt
123
[root@shu-test abc]# echo "234" >> x.txt
[root@shu-test abc]# cat x.txt
123
234
[root@shu-test abc]# echo "345" >> x.txt
[root@shu-test abc]# cat x.txt
123
234
345
[root@shu-test abc]#
實驗3:錯誤重定向;
[root@shu-test abc]# ls [12].txt aaa.txt >x.txt 2>y.txt
[root@shu-test abc]# cat x.txt
1.txt
2.txt
[root@shu-test abc]# cat y.txt
ls: 無法訪問aaa.txt: 沒有那個文件或目錄
[root@shu-test abc]#
記錄歷史命令,history,命令補全和別名、通配符、輸入輸出重定向