8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向
8.1?shell介紹
8.2?命令歷史
8.3?命令補(bǔ)全和別名
8.4?通配符
8.5?輸入輸出重定向
8.1 shell介紹
Linux Shell基礎(chǔ)
介紹shell的特性,用法。
shell是一個(gè)命令解釋器,提供用戶和機(jī)器之間的交互。
支持特定語(yǔ)法,比如邏輯判斷、循環(huán)。
每個(gè)用戶都可以有自己特定的shell
CentOS7默認(rèn)shell為bash(Bourne Agin Shell)
Bourne是一個(gè)用戶,他開(kāi)發(fā)的shell是sh.
后來(lái)CentOS7默認(rèn)使用的shell是bash,基于sh優(yōu)化而來(lái)。
shell還有zsh、ksh等
8.2 命令歷史
history命令
.bash_historyhistory文件,存放命令
# cat /root/.bash_history?
歷史命令最大可以存1000條 (要修改最大值,可以修改/etc/profile文件)
查看變量HISTSIZE值,?
# echo $HISTSIZE?
1000
有時(shí)候敲命令,可以敲到1000+ ,這是因?yàn)檫@些命令還沒(méi)有真正寫到文件里,因?yàn)楝F(xiàn)在運(yùn)行的命令是暫時(shí)存在內(nèi)存中。
#history -c?
清空當(dāng)前命令歷史,不能清空相關(guān)歷史配置文件的,對(duì)配置文件無(wú)任何影響。只有退出終端,命令歷史記錄才會(huì)被寫入文件中。
要改變$HISTSIZE,需在/etc/profile中修改
修改profile中的history參數(shù)之后,需要讓修改生效,利用#source /etc/profile?
[root@centos7 ~]# vi /etc/profile
[root@centos7 ~]# echo $HISTSIZE
1000
[root@centos7 ~]# source /etc/profile
[root@centos7 ~]# echo $HISTSIZE?
5000
用時(shí)間顯示使用過(guò)的命令歷史記錄。
使用參數(shù):HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@centos7 ~]# echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "Y年m月d日H時(shí)M分鐘S秒
HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S ?
[root@centos7 ~]# vi /etc/profile修改profile文件
[root@centos7 ~]# source /etc/profile使參數(shù)修改生效
[root@centos7 ~]# history?
? ? 6 ?2018/03/07 15:08:26 history?
? ? 7 ?2018/03/07 15:08:32 ls
? ? 8 ?2018/03/07 15:08:33 history?
? ? 9 ?2018/03/07 15:09:16 cd /etc/
? ?10 ?2018/03/07 15:09:19 cd -
? ?11 ?2018/03/07 15:09:23 history?
?
用久保存 chattr +a ~/.bash_history
如果不正常退出,保存的命令會(huì)不全。
!! 上一條命令,則history的最后一條命令。
!n ?!接n條命令。例如!6 執(zhí)行history的第6條命令
!word 從history倒退網(wǎng)上找最近一條相關(guān)word命令。例如!echo,從history里找最后一條echo的命令。
# history?
? ? ? 26 ?2018/03/07 16:02:29 history?
? ?27 ?2018/03/07 16:04:17 echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "?
? ?28 ?2018/03/07 16:04:29 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
? ?29 ?2018/03/07 16:06:06 history?
# !echo
echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "?
HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S?
8.3 命令補(bǔ)全和別名
命令補(bǔ)全及別名
tab鍵,敲一下,敲兩下
敲一下補(bǔ)全效果,
敲兩下,列出相關(guān)補(bǔ)全文件或目錄名
系統(tǒng)參數(shù)命令在未安裝bash-completion之前,是不能補(bǔ)全的,例如敲入:
#systemctl restart network.service 會(huì)發(fā)現(xiàn)未安裝之前是不能全部敲入的。
參數(shù)補(bǔ)全,安裝bash-completion,安裝完成后重啟。
#yum install -y bash-completion
# rpm -qa bash-completion安裝成功,
bash-completion-2.1-6.el7.noarch
alias別名給命令重新起個(gè)名字
給systemctl restart network.service做一個(gè)alias,名為:restartnet
[root@centos7 ~]# alias restartnet='systemctl restart network.service'
[root@centos7 ~]# restartnet?
[root@centos7 ~]# alias查看系統(tǒng)有哪些別名?
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'
alias存放目錄?
各用戶都有自己配置別名的文件 ~/.bashrc
ls /etc/profile.d/
自定義的alias放到 ~/.bashrc
~/.bashrc 部分在這
/etc/profile.d/ ?grep,colors
#unalias restartnet取消restartnet的alias
8.4 通配符
介紹
*用法
[root@centos7 ~]# ls
1.txt ?1.xtx ?2.txt ?a_(2).txt ?anaconda-ks.cfg ?anaconda-ks.cfg.1 ?a.txt ?temp.1
[root@centos7 ~]# ls *txt目錄下所有TXT文件
1.txt ?2.txt ?a_(2).txt ?a.txt
[root@centos7 ~]# ls 1*txt目錄下1開(kāi)頭的TXT文件
1.txt
[root@centos7 ~]# ls *2*.txt目錄下名稱含有2的TXT文件
2.txt ?a_(2).txt
?用法
[root@centos7 ~]# touch 3.txt 4.txt a.txt bb.txt
[root@centos7 ~]# ls
1.txt ?1.xtx ?2.txt ?3.txt ?4.txt ?a_(2).txt ?anaconda-ks.cfg ?anaconda-ks.cfg.1 ?a.txt ?bb.txt ?temp.1
[root@centos7 ~]# ls ?.txt文件名只有一個(gè)字的TXT文件名
1.txt ?2.txt ?3.txt ?4.txt ?a.txt
[root@centos7 ~]# ls ??.txt文件名有兩個(gè)字的TXT文件名
bb.txt
[]數(shù)字范圍用法,支持特定數(shù)字,也支持特定范圍。
[root@centos7 ~]# ls [0-4].txt
1.txt ?2.txt ?3.txt ?4.txt
[root@centos7 ~]# ls [14].txt
1.txt ?4.txt
[root@centos7 ~]# ls [1234].txt
1.txt ?2.txt ?3.txt ?4.txt
英文和數(shù)字用法
[root@centos7 ~]# touch A.txt
[root@centos7 ~]# ls [0-9a-zA-Z].txt
1.txt ?2.txt ?3.txt ?4.txt ?a.txt ?A.txt
{}用法,例如{1,2,3}=[1-3]
[root@centos7 ~]# ls {1,2,3}.txt
1.txt ?2.txt ?3.txt
[root@centos7 ~]# ls [1-3].txt
1.txt ?2.txt ?3.txt
8.5 輸入輸出重定向
輸出重定向>
cat 1.txt >2.txt把1.txt輸出的內(nèi)容結(jié)果重定向輸入到2.txt的文件里。
cat 1.txt >>2.txt>>和>差不多,只是>>是追加,>>不把1.txt的文件內(nèi)容刪掉,會(huì)保留著,并且輸入到2.txt,這個(gè)動(dòng)作叫做追加。
>把前面輸出的內(nèi)容結(jié)果,重定向輸入到后面的文件里。(前面被重定向的內(nèi)容會(huì)被刪掉)
ls aaa.txt 2>4.txt lsaaa.txt 產(chǎn)生的錯(cuò)誤信息指定輸出到4.txt里
[root@centos7 ~]# ls aaa.txt 2> 4.txt
[root@centos7 ~]# cat 4.txt
ls: 無(wú)法訪問(wèn)aaa.txt: 沒(méi)有那個(gè)文件或目錄
ls aaa.txt 2>>4.txtlsaaa.txt 產(chǎn)生的錯(cuò)誤信息指定輸出追加到4.txt里
2>產(chǎn)生的錯(cuò)誤信息指定輸出到指定文件里
2>>產(chǎn)生的錯(cuò)誤信息指定輸出追加到指定文件里
>正確的信息,2>錯(cuò)誤的信息
>+2>=&> &>=>正確信息 2>的結(jié)合
&>舉例演示
[root@centos7 ~]# ls [1-2].txt aaa.txt &>a.txt
[root@centos7 ~]# cat a.txt?
ls: 無(wú)法訪問(wèn)aaa.txt: 沒(méi)有那個(gè)文件或目錄
1.txt
2.txt
&>>演示
[root@centos7 ~]# ls [1-2].txt aaa.txt &>>a.txt
[root@centos7 ~]# cat a.txt?
ls: 無(wú)法訪問(wèn)aaa.txt: 沒(méi)有那個(gè)文件或目錄
1.txt
2.txt
ls: 無(wú)法訪問(wèn)aaa.txt: 沒(méi)有那個(gè)文件或目錄
1.txt
2.txt
用2個(gè)文件區(qū)分 正確與錯(cuò)誤輸出結(jié)果
[root@centos7 ~]# ls [1-2].txt aaaa.txt >1.txt 2>a.txt
[root@centos7 ~]# cat 1.txt?
1.txt
2.txt
[root@centos7 ~]# cat a.txt?
ls: 無(wú)法訪問(wèn)aaaa.txt: 沒(méi)有那個(gè)文件或目錄
輸入重定向<,格式一般是:命令 < 文件 (這個(gè)比較少用)
[root@centos7 ~]# wc -l < 1.txt
2
轉(zhuǎn)載于:https://blog.51cto.com/13578154/2083929
總結(jié)
以上是生活随笔為你收集整理的8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 轴对称 Navier-Stokes 方程
- 下一篇: shell变量,管道符,作业控制,she