1. 程式人生 > >shell介紹、命令歷史 、 命令補全和別名 、通配符、輸入輸出重定向

shell介紹、命令歷史 、 命令補全和別名 、通配符、輸入輸出重定向

shell

一、shell介紹
技術分享圖片
二、命令歷史
技術分享圖片
歷史命令存放路徑 /root/.bash_history ,默認可以存放1000條命令
#history //查看具體的歷史命令
[root@linux-01 ~]# echo $HISTSIZE //系統內置環境變量
1000

#history -c //清空內存中命令歷史,但是存放命令的文件不會被刪除
#vi /etc/profile // 修改HISTORY環境變量路徑/etc/profile,可以吧HISTSIZE=1000改為5000
[root@linux-01 ~]# source /etc/profile //修改完配置文件需要執行這條命令讓配置文件生效
[root@linux-01 ~]# echo $HISTSIZE

5000

[root@linux-01 ~]# HISTTIMEFORMAT="%Y/%M/%d %H:%M:%S" //需要查看每條命令的執行時間
[root@linux-01 ~]# echo $HISTTIMEFORMAT
%Y/%M/%d %H:%M:%S

如果需要讓查看每條命令的具體時間永久生效,需要編輯/etc/profile,在HISTSIZE=5000下面添加一行: HISTTIMEFORMAT="%Y/%M/%d %H:%M:%S" 保存退出就可以永久生效

[root@linux-01 ~]# chattr +a ~/.bash_history //給文件添加隱藏權限,此文件只能追加,不能被刪除

如果沒有正常退出終端,那麽命令在.bash_history文件中就不被保存

[root@linux-01 ~]# !! //兩個感嘆號表示執行的上一條命令
[root@linux-01 ~]# !11 //執行第命令歷史中的第11條命令
[root@linux-01 ~]# !echo //會在命令歷史中倒著找第一個以echo開頭的命令
[root@linux-01 ~]# !mkdir //會在命令歷史中倒著找第一個以mkdir開頭的命令

三、命令補全和別名
技術分享圖片
在Centos7中,如果需要按Teb鍵補全參數,如 systemctl restart network這條命令需要補全restart參數,需要安裝bash-completion,可以yum安裝 # yum install -y bash-completion ,安裝完之後需要重啟系統生效

#rpm -qa 包名 //查看包有沒有安裝

[root@linux-01 ~]# rpm -qa alias restartnet=‘systemctl restart network.service‘
//使用restartnet給‘systemctl restart network.service‘ 這條命令做別名,那麽重啟網絡服務的時候就可以執行restartnet命令來重合器網卡了

[root@linux-01 ~]# 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‘

.bashrc 和 /etc/profile.d 下都有定義alias
#unalias restartnet //取消自定義別名restartnet

四、通配符 、輸入輸出重定向
技術分享圖片
表示通配
[root@linux-01 ~]# ls
.txt //列出.txt所有文件可以使用.txt
11.txt 1.txt 2.txt
[root@linux-01 ~]# ls
txt //列出.txt所有文件也可以使用txt
11.txt 1.txt 2.txt
[root@linux-01 ~]# ls
txt //列出.txt所有文件也可以使用txt*
11.txt 1.txt 1.txt.zip 2.txt

[root@linux-01 ~]# ls 1 //列出以1開頭的所有文件可以使用1
11.txt 1.txt 1.txt.zip
123:
333 yum.log

[root@linux-01 ~]# ls ?.txt //?.txt表示一個任意一個字符的txt文件,?表示任意一個字符
1.txt 2.txt 3.txt 4.txt a.txt

[root@linux-01 ~]# ls [0-3].txt //查看開頭0-3之間的.txt文件,方括號中的0-3取其中一個數字
1.txt 2.txt 3.txt
[root@linux-01 ~]# ls [123].txt //也可以取方括號中的1 2 3開頭所有的.txt文件
1.txt 2.txt 3.txt
[root@linux-01 ~]# ls [23].txt //例如列出2 3開頭的.txt文件
2.txt 3.txt
[root@linux-01 ~]# ls [0-9a-zA-Z].txt //列出0-9 a-z A-Z的所有.txt文件
1.txt 2.txt 3.txt 4.txt

[root@linux-01 ~]# ls {1,2}.txt //列出1,2開頭的.txt文件,註意花括號中需要加逗號
1.txt 2.txt

#cat 1.txt > 2.xtx //把前面命令的輸出直接輸入到後面的文件裏面,一個大於號會把原來的內容刪除
#cat 1.txt >> 2.xtx //兩個大於號不會把大於號左邊原來的命令刪除

#laaaa 2> 1.txt //使用 2> 把一個命令的錯誤信息輸入到1.txt文件中
#laaaa 2>> 1.txt //使用2>>把錯誤信息追加重定向到1.txt文件中

是輸出重定向;>> 是輸出追加重定向;2> 是錯誤重定向;2>> 是錯誤追加重定向

  • 2> = &>
    例:
    [root@linux-01 ~]# ls [12].txt aaa.txt &> 3.txt //把[12].txt aaa.txt 的正確信息和錯誤信息都輸入到3.txt文件中
    [root@linux-01 ~]# cat 3.txt //查看3.txt文件信息
    ls: cannot access aaa.txt: No such file or directory
    1.txt
    2.txt

[root@linux-01 ~]# ls [12].txt aaa.txt &>> 3.txt //同樣支持追加
[root@linux-01 ~]# cat 3.txt
ls: cannot access aaa.txt: No such file or directory
1.txt
2.txt
ls: cannot access aaa.txt: No such file or directory
1.txt
2.txt

[root@linux-01 ~]# ls [12].txt aaa.txt > 3.txt 2>4.txt //可以把正確命令輸入到3.txt文件中,錯誤信息輸入到4.txt文件中,以後shell腳本中用的多,可以把正確和錯誤分別指定到不同文件
[root@linux-01 ~]# cat 3.txt
1.txt
2.txt
[root@linux-01 ~]# cat 4.txt
ls: cannot access aaa.txt: No such file or directory

< 是輸入重定向,小於號左邊必須是命令,右邊是文件
[root@linux-01 ~]# wc -l < 3.txt //查看3.txt文件行數
2

shell介紹、命令歷史 、 命令補全和別名 、通配符、輸入輸出重定向