目錄
一、shell介紹
二、命令歷史
三、命令補(bǔ)全和別名
四、通配符
五、輸入輸出重定向
一、shell介紹
在計(jì)算機(jī)科學(xué)中,Shell俗稱殼(用來區(qū)別于核),是指“為使用者提供操作界面”的軟件(命令解析器)。它類似于[DOS]下的command.com和后來的cmd.exe。它接收用戶命令,然后調(diào)用相應(yīng)的應(yīng)用程序。
用戶直接面對的不是計(jì)算機(jī)硬件而是shell,用戶把指令告訴shell,然后shell再傳輸給系統(tǒng)內(nèi)核,接著內(nèi)核再去支配計(jì)算機(jī)硬件去執(zhí)行各種操作。
- 每個(gè)用戶都可以有自己特定的shell。
- shell支持特定語法,比如邏輯判斷、循環(huán)。
- CentOS默認(rèn)shell為bash(Bourne Agin Shell)
二、命令歷史
我們執(zhí)行過的命令Linux都會(huì)記錄,預(yù)設(shè)可以記錄1000條歷史命令,該數(shù)量是由環(huán)境變量HISTSIZE進(jìn)行控制。這些命令保存在用戶的家目錄的.bash_history文件中。但需要注意的是,只有當(dāng)用戶正常退出當(dāng)前shell時(shí),在當(dāng)前shell中運(yùn)行的命令才會(huì)保存至.bash_history文件中。
[root@minglinux-01 ~]# echo $HISTSIZE
1000
[root@minglinux-01 ~]# ls /root/.bash_history
/root/.bash_history
環(huán)境變量HISTSIZE 可以在/etc/profile中修改,在文件中找到HISTSIZE=1000修改后面的數(shù)值,更改之后重啟終端或者source /etc/profile 才會(huì)生效。
- 給$HISTTIMEFORMAT變量賦值可以修改命令歷史的格式,示例命令如下:
[root@minglinux-01 ~]# history 10
994 reboot
995 history 10
996 ls /root/.bash_history
997 cat /root/.bash_history
998 echo $HISTSIZE
999 ls /root/.bash_history
1000 echo $HISTTIMEFORMATE
1001 echo $HISTTIMEFORMAT
1002 history
1003 history 10
[root@minglinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@minglinux-01 ~]# history 10
996 2018/10/11 22:24:19ls /root/.bash_history
997 2018/10/11 22:24:34cat /root/.bash_history
998 2018/10/11 22:31:30echo $HISTSIZE
999 2018/10/11 22:32:19ls /root/.bash_history
1000 2018/10/11 22:41:05echo $HISTTIMEFORMATE
1001 2018/10/11 22:41:10echo $HISTTIMEFORMAT
1002 2018/10/11 22:43:59history
1003 2018/10/11 22:44:11history 10
1004 2018/10/11 22:44:27HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
1005 2018/10/11 22:44:29history 10
這個(gè)變量賦值只在當(dāng)前終端生效,如果想讓此環(huán)境變量全局生效,需要編輯 /etc/profile把HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "加到里面。
如果我們想永久保存命令歷史,不讓別人刪除破壞,我們可以使用chattr +a ~/.bash_history指令設(shè)置了這個(gè)文件的權(quán)限只能追加不能刪除。
history命令用于顯示指定數(shù)目的指令命令。該命令單獨(dú)使用時(shí),顯示全部歷史命令。加上參數(shù)n則顯示最近的n條歷史命令。
[root@minglinux-01 ~]# history 10
986 yum install apr apr-util apr-devel
987 ./config --prefix=/usr/local/apace2
988 ./configure --prefix=/usr/local/apace2
989 yum install apr-util
990 yum install apr-devel
991 yum install apr
992 yum install -y gcc
993 ./configure --prefix=/usr/local/apace2
994 reboot
995 history 10
history命令的幾個(gè)選項(xiàng):
-c:清空當(dāng)前歷史命令,不會(huì)清空歷史命令文件。
-a:將歷史命令緩沖區(qū)中命令寫入歷史命令文件中。
-r:將歷史命令文件中的命令讀入當(dāng)前歷史命令緩沖區(qū)。
-w:將當(dāng)前歷史命令緩沖區(qū)命令寫入歷史命令文件中。
在命令行中,可以使用符號!執(zhí)行指定序號的歷史命令。!符號常用的應(yīng)用有以下3個(gè):
!!:連續(xù)兩個(gè)!表示執(zhí)行上一條指令。示例命令如下:
[root@minglinux /]# ls
bin data etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
[root@minglinux /]# !!
ls
bin data etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
!n:n是數(shù)字,表示執(zhí)行命令歷史中的第n條指令。示例命令如下:
[root@minglinux /]# history |grep 536
536 pwd
539 history |grep 536
[root@minglinux /]# !536
pwd
/
!字符串(字符串大于等于1):例如!pw表示執(zhí)行命令歷史中最近一次以pw開頭的命令。示例命令如下:
[root@minglinux /]# !pw
pwd
/
三、命令補(bǔ)全和別名
按tab鍵可以幫我們補(bǔ)全一個(gè)指令、一個(gè)路徑或者一個(gè)文件名。連續(xù)按兩次tab鍵,系統(tǒng)則會(huì)把所有的命令或者文件名都列出來。
Centos7 支持參數(shù)補(bǔ)全,默認(rèn)不可以,需要執(zhí)行yum install -y bash-completion 安裝相關(guān)軟件包,安裝完成后重啟系統(tǒng)才能生效。
alias命令用來設(shè)置指令的別名。我們可以使用該命令可以將一些較長的命令進(jìn)行簡化。使用alias時(shí),用戶必須使用單引號''將原來的命令引起來,防止特殊字符導(dǎo)致錯(cuò)誤。如果不想用了,還可以使用unalias命令解除別名功能。
直接執(zhí)行alias命令,會(huì)看到目前系統(tǒng)預(yù)設(shè)的別名,如下所示:
[root@minglinux /]# 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'
自定義別名的格式為alias [命令別名]=['具體的命令'],示例命令如下:
[root@minglinux /]# alias ming='pwd'
[root@minglinux /]# ming
/
[root@minglinux /]# unalias ming
[root@minglinux /]# ming
-bash: ming: 未找到命令
alias命令的作用只局限于該次登入的操作。若要每次登入都能夠使用這些命令別名,則可將相應(yīng)的alias命令存放到bash的初始化文件/etc/bashrc中。
四、通配符
在bash下,可以使用*來匹配零個(gè)或多個(gè)字符,用?匹配一個(gè)字符。示例命令如下:
[root@minglinux dir1]# ls
test1 test123 test2 test3
[root@minglinux dir1]# ls test*
test1 test123 test2 test3
[root@minglinux dir1]# ls test?
test1 test2 test3
[]匹配一個(gè)范圍的字符,如[123]匹配123其中任意一個(gè),[0-9]匹配1至9當(dāng)中任一個(gè)字符。示例命令如下:
[root@minglinux-01 ~]# ls
123 1_soft.txt anaconda-ks.cfg dir2 dir4
1_hard.txt 1.txt dir1 dir3 快捷鍵.txt
[root@minglinux-01 ~]# ls -d dir[123]
dir1 dir2 dir3
[root@minglinux-01 ~]# ls -d dir[1-9]
dir1 dir2 dir3 dir4
五、輸入輸出重定向
輸入重定向用于改變命令的輸入,輸出重定向用于改變命令的輸出。輸入重定向的命令是<,輸出重定向的命令是>,還有錯(cuò)誤重定向命令2>以及追加重定向命令>>,示例命令如下:
[root@minglinux-01 ~]# touch 2.txt
[root@minglinux-01 ~]# echo "123" > 2.txt
[root@minglinux-01 ~]# cat 2.txt
123
[root@minglinux-01 ~]# echo "123" >> 2.txt
[root@minglinux-01 ~]# cat 2.txt
123
123
- 錯(cuò)誤重定向和錯(cuò)誤追加重定向:
[root@minglinux-01 ~]# ming
-bash: ming: 未找到命令
[root@minglinux-01 ~]# ming 2> 3.txt
[root@minglinux-01 ~]# cat 3.txt
-bash: ming: 未找到命令
[root@minglinux-01 ~]# ming 2>> 3.txt
[root@minglinux-01 ~]# cat 3.txt
-bash: ming: 未找到命令
-bash: ming: 未找到命令
- &>是錯(cuò)誤和正確都有的輸出重定向,示例命令如下:
[root@minglinux-01 ~]# ls
123 1.txt anaconda-ks.cfg dir3
1_hard.txt 2.txt dir1 dir4
1_soft.txt 3.txt dir2 快捷鍵.txt
[root@minglinux-01 ~]# ls [123].txt abc.txt &> a.txt
[root@minglinux-01 ~]# cat a.txt
ls: 無法訪問abc.txt: 沒有那個(gè)文件或目錄
1.txt
2.txt
3.txt
- 我們還可以將正確輸出和錯(cuò)誤輸出分開保存到不同文件中:
[root@minglinux-01 ~]# ls [123].txt abc.txt > a.txt 2>b.txt
[root@minglinux-01 ~]# cat a.txt
1.txt
2.txt
3.txt
[root@minglinux-01 ~]# cat b.txt
ls: 無法訪問abc.txt: 沒有那個(gè)文件或目錄
- 輸入重定向:
[root@minglinux-01 ~]# wc -l 2.txt
2 2.txt
[root@minglinux-01 ~]# wc -l < 2.txt
2