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

Shell介紹

  • shell是一個命令解釋器,提供用戶和機器之間的交互
  • 支持特定語法,好比邏輯判斷、循環(if for where)
  • 每一個用戶均可以有本身特定的shell
  • CentOS7默認shell爲bash(Bourne Agin Shell)
  • 還有zsh、ksh等

命令歷史

  • history命令
  • bash_history
  • 最大1000條
  • 變量HISTSIZE
  • /etc/profile中修改
  • HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
  • 永久保存 chattr +a ~/.bash_history
  • !!
  • !n
  • !word

1 .history 命令

顯示歷史命令 後10個shell

[root@yong-02 ~]# history |tail
  327  ls
  328  cd apr-util-1.6.1
  329  ls /usr/local/apache2/
  330  cd
  331  ls
  332  zsh
  333  eix
  334  yum list | grep zsh
  335  history
  336  history |tail

2. echo $HISTSIZE 顯示存放最大多少條歷史記錄

[root@yong-02 ~]# echo $HISTSIZE
1000

3. .bash_history 文件

歷史命令保存在用戶家目錄的.bash_history文件中apache

4. history –c

清空內存中的歷史記錄;不會清除.bash_history文件中的記錄。vim

[root@yong-02 ~]# history -c
[root@yong-02 ~]# history
    1  history
[root@yong-02 ~]# head -10 .bash_history 
ifconfig
ls
ls /tmp
df -h
ls -toot
ls /root
dhclient
ifconfig
ls
ifconfig
  • 只有當用戶正常退出當前shell時,在當前shell中運行的命令纔會保存至.bash_history文件中。

5. 定義HISTSIZE值,在配置文件/etc/profile中修改

[root@yong-02 ~]# vim /etc/profile
搜索HISTSIZE 默認值是1000,能夠修改爲5000條,而後按esc,:wq 保存退出。
而後運行命令:source /etc/profile
[root@yong-02 ~]# source /etc/profile
[root@yong-02 ~]# echo $HISTSIZE
5000

6. 定義格式:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

在配置文件/etc/profile中修改,在HISTSIZE=5000下面,添加一行HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
輸入圖片說明bash

[root@yong-02 ~]# vim /etc/profile
[root@yong-02 ~]# source /etc/profile
[root@yong-02 ~]# history
    1  2018/04/19 22:37:56 history
    2  2018/04/19 22:38:18 head -10 .bash_profile 
    3  2018/04/19 22:38:28 head -10 .bash_history 
    4  2018/04/19 22:39:24 vim /etc/profie
    5  2018/04/19 22:39:38 vim /etc/profile
    6  2018/04/19 22:41:23 source /etc/profile
    7  2018/04/19 22:41:37 echo $HISTSIZE
    8  2018/04/19 22:42:15 vim /etc/profile
    9  2018/04/19 22:42:53 source /etc/profile
   10  2018/04/19 22:43:07 history

6. 永久保存 chattr +a ~/.bash_history

  • 給.bash_history加一個a權限,不能刪除,能夠追加
[root@yong-02 ~]# chattr +a .bash_history 
[root@yong-02 ~]# lsattr .bash_history 
-----a---------- .bash_history

7. !!:連續兩個!表示執行上一條命令。

[root@yong-02 ~]# lsattr .bash_history 
-----a---------- .bash_history
[root@yong-02 ~]# !!
lsattr .bash_history 
-----a---------- .bash_history

8. !n:這裏的n是數字,表示執行命令歷史中的第n條命令。

[root@yong-02 ~]# history
    1  2018/04/19 22:37:56 history
    2  2018/04/19 22:38:18 head -10 .bash_profile 
    3  2018/04/19 22:38:28 head -10 .bash_history 
    4  2018/04/19 22:39:24 vim /etc/profie
    5  2018/04/19 22:39:38 vim /etc/profile
    6  2018/04/19 22:41:23 source /etc/profile
    7  2018/04/19 22:41:37 echo $HISTSIZE
    8  2018/04/19 22:42:15 vim /etc/profile
    9  2018/04/19 22:42:53 source /etc/profile
   10  2018/04/19 22:43:07 history
   11  2018/04/19 22:43:56 chattr +a .bash_history 
   12  2018/04/19 22:44:12 lsattr .bash_history 
   13  2018/04/19 22:45:07 history
   14  2018/04/19 22:45:25 ls
   15  2018/04/19 22:45:28 pws
   16  2018/04/19 22:45:31 pwd
   17  2018/04/19 22:45:48 history
[root@yong-02 ~]# !14
ls
anaconda-ks.cfg         a.txt  c.txt             multi-user.target  reboot.target
apr-util-1.6.1.tar.bz2  b.txt  graphical.target  poweroff.target    rescue.target

9. !字符串: !pw表示執行命令歷史中最近一次以pw開頭的命令。

[root@yong-02 ~]# !pw
pwd
/root

命令和文件名補全

  • tab鍵,敲一下,敲兩下
  • 參數補全,安裝bash-completion
  • alias別名給命令從新起個名字
  • 各用戶都有本身配置別名的文件 ~/.bashrc
  • ls /etc/profile.d/
  • 自定義的alias放到~/.bashrc
  1. 按tab鍵能夠幫咱們補全一個指令,一個路徑或者一個文件名
  2. Centos6中tab鍵只能補全自己;Centos7中tab鍵支持命令參數補全,須要安裝一個包bash-completion  重啓才能生效
  3. 例如 systemctl restart network.service 
[root@yong-02 ~]# yum install -y bash-completion
重啓服務器 reboot

別名

  1. alias也是bash所特有的功能之一;直接執行alias命令會看到目前系統預設的全部別名。
[root@yong-02 ~]# 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'
  1. 用戶家目錄下.bashrc文件中只配置了幾個alias
[root@yong-02 ~]# cat .bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
  1. 其餘的不少別名在/etc/profile.d/目錄下,有不少.sh文件中定義;
[root@yong-02 ~]# ls /etc/profile.d/
256term.csh         colorgrep.csh  colorls.sh  less.csh  vim.sh
256term.sh          colorgrep.sh   lang.csh    less.sh   which2.csh
bash_completion.sh  colorls.csh    lang.sh     vim.csh   which2.sh

vim/etc/profile.d/colorls.sh 服務器

通配符

  • ls *.txt
  • ls ?.txt
  • ls [0-9].txt
  • ls {1,2}.txt

*用來匹配零個或多個任意字符

[root@yong-02 test]# ls *.txt
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt
[root@yong-02 test]# ls *txt
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt
[root@yong-02 test]# ls *txt*
11.txt  1.txt  23.txt  2.txt  3.txt  ab.txt  a.txt  b.txt  cc.txt.bak

?用來匹配一個字符

[root@yong-02 test]# ls ?.txt
1.txt  2.txt  3.txt  a.txt  b.txt

ls [0-3].txt [ ]裏面表示範圍,只要是在這個範圍內都列出來

[root@yong-02 test]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@yong-02 test]# ls [123].txt
1.txt  2.txt  3.txt
[root@yong-02 test]# ls [23].txt
2.txt  3.txt
[root@yong-02 test]# ls [a-z].txt
a.txt  b.txt
[root@yong-02 test]# ls [az].txt
a.txt
[root@yong-02 test]# ls [0-9a-z].txt
1.txt  2.txt  3.txt  a.txt  b.txt

ls {1,3}.txt { }花括號裏面要用「,」隔開,1.txt或者3.txt;

[root@yong-02 test]# ls {1,3}.txt
1.txt  3.txt
[root@yong-02 test]# ls {1,3,2,a}.txt
1.txt  2.txt  3.txt  a.txt

輸入輸出重定向

  • cat 1.txt >2.txt
  • cat 1.txt >> 2.txt
  • ls aaa.txt 2>err
  • ls aaa.txt 2>>err
  • wc -l < 1.txt
  • command >1.txt 2>&1

>輸出重定向

[root@yong-02 test]# echo "aabbcc123" >1.txt 
[root@yong-02 test]# cat 1.txt 
aabbcc123

>>追加劇定向

[root@yong-02 test]# echo "56789" >>1.txt 
[root@yong-02 test]# cat 1.txt 
aabbcc123
56789

2> 錯誤重定向

[root@yong-02 test]# ls ccc.txt 2>2.txt
[root@yong-02 test]# cat 2.txt 
ls: 沒法訪問ccc.txt: 沒有那個文件或目錄

2>>錯誤追加劇定向

[root@yong-02 test]# ls bbb.txt 2>>2.txt
[root@yong-02 test]# cat 2.txt 
ls: 沒法訪問ccc.txt: 沒有那個文件或目錄
ls: 沒法訪問bbb.txt: 沒有那個文件或目錄

&> == >+2>正確和錯誤重定向

[root@yong-02 test]# ls 1.txt aaa.txt &>3.txt
[root@yong-02 test]# cat 3.txt 
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt

&>>

[root@yong-02 test]# ls 1.txt aaa.txt &>>3.txt
[root@yong-02 test]# cat 3.txt 
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄
1.txt

能夠將正確輸出到一個文件中,錯誤輸入到另外一個文件中,也能夠同時輸出到一個文件中(用&>)。

[root@yong-02 test]# ls 23.txt aaa.txt > 1.txt 2>2.txt 
[root@yong-02 test]# cat 1.txt 
23.txt
[root@yong-02 test]# cat 2.txt 
ls: 沒法訪問aaa.txt: 沒有那個文件或目錄

< 輸入重定向

[root@yong-02 test]# wc -l </etc/passwd
25
[root@yong-02 test]# 2.txt < 3.txt
-bash: 2.txt: 未找到命令
左邊只能是命令,不能是文件。
相關文章
相關標籤/搜索