8.1 shell介紹css
8.2 命令歷史 html
8.3 命令補全和別名 linux
8.4 通配符 shell
8.5 輸入輸出重定向vim
8.1 shell介紹bash
本章只介紹shell的基礎,不涉及shell的腳本,腳本之後再講。基礎涉及shell的特性、一些用法。less
shell腳本在平常的linux系統管理工做中是必不可少的,若是不會寫shell腳本,你就不算是一個合格的管理員。ide
shell是系統跟計算機硬件交互時使用的中間介質,它只是系統的一個工具。實際上,在shell和計算機硬件之間還有一層東西——系統內核。用戶直接面對的不是計算機硬件而是shell,用戶把指令告訴shell,而後shell再傳輸給系統內核,接着內核再去支配計算機硬件去執行各類操做。工具
Red Hat/CentOS發行版默認安裝的shell版本是bash(Bourne Again Shell),它是sh(Bourne Shell)的加強版本。spa
什麼是shell?
• shell是一個命令解釋器,提供用戶和機器之間的交互
• 支持特定語法,好比邏輯判斷、循環
• 每一個用戶均可以有本身特定的shell
• CentOS7默認shell爲bash(Bourne Agin Shell)
• 還有zsh、ksh等
登錄用戶名出來的一個界面就是一個shell,用戶配置文件的最後一段就是shell,每一個用戶均可以有本身的shell。
能夠搜一下zsh和ksh。
[root@localhost ~]# yum list | grep zsh
zsh.x86_64 5.0.2-28.el7 base
zsh-html.x86_64 5.0.2-28.el7 base
[root@localhost ~]# yum list | grep ksh
ksh.x86_64 20120801-34.el7 base
mksh.x86_64 46-5.el7 base
[root@localhost ~]
8.2 命令歷史
• history命令
• .bash_history
• 最大1000條
• 變量HISTSIZE
• /etc/profile中修改
• HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
• 永久保存 chattr +a ~/.bash_history
• !!
• !n
• !word
咱們執行過的命令linux都會記錄,預設能夠記錄1000條命令。這些命令保存在用戶的家目錄的.bash_history文件中。但須要注意,只有當用戶正常退出當前shell時,在當前shell中運行的命令纔會保存至.bash_history文件中。
使用向上的方向鍵能夠查看歷史命令。
!是與命令歷史有關的一個特殊字符,經常使用應用有3個。
!!:連續兩個!表示執行上一條命令
!n:表示執行歷史命令中的第幾條命令
!字符串(字符串大於等於1):表示執行歷史命令中最近一次以字符串開頭的命令
[root@localhost ~]# ls /root/.bash_history
/root/.bash_history
[root@localhost ~]#
可使用cat命令查看該文件,也可使用history命令查看歷史命令。
[root@localhost ~]# cat /root/.bash_history^C
[root@localhost ~]# history^C
最大歷史命令數目在環境變量HISTSIZE中配置。
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]#
有時候敲命令會出現1004,1009這樣的數字,這是由於尚未真正的寫到這個文件裏去,由於敲過的命令暫時存在內存中。
history -c能夠清空當前的歷史命令,可是沒法清空配置文件,這個文件也不會被刪除,只是把內存的命令清空了。
[root@localhost ~]# history -c
[root@localhost ~]# history
1 history
[root@localhost ~]# cat /root/.bash_history
du -sh 3.txt
ls
xz -d 2.txt.xz
xz 2.txt
unxz 2.txt.xz
ls
xz -c 2.txt > /tmp/2.txt.xz
xz -d -c /tmp/2.txt/xz > ./4.txt
xz -d -c /tmp/2.txt.xz > ./4.txt
[root@localhost ~]# ll /root/.bash_history
-rw-------. 1 root root 2584 Jan 5 23:18 /root/.bash_history
環境變量HISTSIZE在文件/etc/profile中配置,能夠更改HISTSIZE的數值。
[root@localhost ~]# vim /etc/profile
HISTSIZE=5000
保存,退出。
若是想要這一步設置生效,須要運行以下命令。
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000
[root@localhost ~]# history
1 history
2 cat /root/.bash_history
3 ll /root/.bash_history
4 vim /dttc/profile
5 vim /etc/profile
6 echo $HISTSIZE
7 source /etc/profile
8 echo $HISTSIZE
9 history
[root@localhost ~]#
怎麼讓歷史命令什麼時候生效的時間顯示出來呢?
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@localhost ~]#
複製一個終端,發現HISTTIMEFORMAT是空的,由於它只在上一個終端中生效,系統默認這個環境變量是不存在的。
[root@localhost ~]# echo $HISTTIMEFORMAT
回到原來的終端,運行history命令。
[root@localhost ~]# history
1 2018/01/06 07:06:26 history
2 2018/01/06 07:07:59 cat /root/.bash_history
3 2018/01/06 07:09:55 ll /root/.bash_history
4 2018/01/06 07:13:28 vim /dttc/profile
5 2018/01/06 07:14:04 vim /etc/profile
6 2018/01/06 07:16:50 echo $HISTSIZE
7 2018/01/06 07:17:03 source /etc/profile
8 2018/01/06 07:17:06 echo $HISTSIZE
9 2018/01/06 07:18:00 history
10 2018/01/06 07:20:08 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
11 2018/01/06 07:20:23 echo $HISTTIMEFORMAT
12 2018/01/06 07:23:58 history
[root@localhost ~]#
若是想讓它在全部終端都生效,須要更改配置文件。
[root@localhost ~]# vim /etc/profile
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S ",保存退出。
[root@localhost ~]# source !$
source /etc/profile
[root@localhost ~]#
此時,從新複製一個終端,發現剛纔的配置已然生效。
[root@localhost ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@localhost ~]#
[root@localhost ~]# history
1 2018/01/06 07:28:41 cd /home
2 2018/01/06 07:28:41 ls
3 2018/01/06 07:28:41 yum install -y vim
4 2018/01/06 07:28:41 dhclient
5 2018/01/06 07:28:41 ip addr
6 2018/01/06 07:28:41 c
7 2018/01/06 07:28:41 cd
8 2018/01/06 07:28:41 dhclient -r
9 2018/01/06 07:28:41 dhclient
10 2018/01/06 07:28:41 ip addr
11 2018/01/06 07:28:41 alias='vi /etc/sysconfig/network-scripts/ifcfg-ens33'
12 2018/01/06 07:28:41 alias vieth='vi /etc/sysconfig/network-scripts/ifcfg-ens33'
13 2018/01/06 07:28:41 vieth
14 2018/01/06 07:28:41 ping www.baidu.com
15 2018/01/06 07:28:41 ip addr
16 2018/01/06 07:28:41 ifconfig
17 2018/01/06 07:28:41 yum install -y gcc
18 2018/01/06 07:28:41 lsb_release -a
19 2018/01/06 07:28:41 poweroff
20 2018/01/06 07:28:41 ls2
21 2018/01/06 07:28:41 cd /
22 2018/01/06 07:28:41 ls
23 2018/01/06 07:28:41 mkdir
24 2018/01/06 07:28:41 mkdir 456
25 2018/01/06 07:28:41 cd 456
26 2018/01/06 07:28:41 cp /etc/passwd ./
27 2018/01/06 07:28:41 ln -s passwd passwd-soft
28 2018/01/06 07:28:41 ll
29 2018/01/06 07:28:41 head -n1 passwd-soft
30 2018/01/06 07:28:41 head -n1 passwd
31 2018/01/06 07:28:41 rm -f passwd
32 2018/01/06 07:28:41 head -n1 passwd-soft
33 2018/01/06 07:28:41 cd ..
34 2018/01/06 07:28:41 ln -s 456 789
35 2018/01/06 07:28:41 ls -ld 456 789
36 2018/01/06 07:28:41 ls
37 2018/01/06 07:28:41 mkdir 123
38 2018/01/06 07:28:41 cp 123 456
39 2018/01/06 07:28:41 cp -r 123 456
40 2018/01/06 07:28:41 ls -ld 123 456
41 2018/01/06 07:28:41 which cp
42 2018/01/06 07:28:41 ls
43 2018/01/06 07:28:41 cd 123
44 2018/01/06 07:28:41 ls
45 2018/01/06 07:28:41 touch 111 222
46 2018/01/06 07:28:41 cp -i 111 222
47 2018/01/06 07:28:41 echo 'abc' > 111
48 2018/01/06 07:28:41 echo 'def' > 222
49 2018/01/06 07:28:41 cat 111 222
50 2018/01/06 07:28:41 /bin/cp 111 222
51 2018/01/06 07:28:41 cat 111
52 2018/01/06 07:28:41 cat 222
53 2018/01/06 07:28:41 mkdir /tmp/test_mv
54 2018/01/06 07:28:41 cd /tmp/test_mv
55 2018/01/06 07:28:41 mkdir dira dirb
56 2018/01/06 07:28:41 ls
57 2018/01/06 07:28:41 mv dira dirc
58 2018/01/06 07:28:41 ls
59 2018/01/06 07:28:41 mv dirc dirb
60 2018/01/06 07:28:41 ls
61 2018/01/06 07:28:41 ls dirb
62 2018/01/06 07:28:41 touch filed
63 2018/01/06 07:28:41 ls
64 2018/01/06 07:28:41 mv filed dirb
65 2018/01/06 07:28:41 mv dirb/filed .
66 2018/01/06 07:28:41 ls
67 2018/01/06 07:28:41 mv filed filee
68 2018/01/06 07:28:41 ls
69 2018/01/06 07:28:41 mv filee dirb
70 2018/01/06 07:28:41 ls
71 2018/01/06 07:28:41 ls dirb
72 2018/01/06 07:28:41 fdisk
73 2018/01/06 07:28:41 fdisk -l
74 2018/01/06 07:28:41 reboot
75 2018/01/06 07:28:41 fdisk -l
76 2018/01/06 07:28:41 fdisk -l /dev/sdb
77 2018/01/06 07:28:41 fdisk /dev/sdb
78 2018/01/06 07:28:41 p
79 2018/01/06 07:28:41 fdisk /dev/sdb
80 2018/01/06 07:28:41 fdisk -l
81 2018/01/06 07:28:41 fdisk /dev/sdb
82 2018/01/06 07:28:41 fdisk -l
83 2018/01/06 07:28:41 fdisk /dev/sdb
84 2018/01/06 07:28:41 ls
85 2018/01/06 07:28:41 cp /etc/dnsmasq.conf /tmp/1.txt
86 2018/01/06 07:28:41 vim /tmp/1.txt
87 2018/01/06 07:28:41 cp /etc/dnsmasq.conf /tmp/1.txt
88 2018/01/06 07:28:41 vim /tmp/1.txt
89 2018/01/06 07:28:41 cd /tmp
90 2018/01/06 07:28:41 ls
91 2018/01/06 07:28:41 mkdir d6z
92 2018/01/06 07:28:41 cd d6z
93 2018/01/06 07:28:41 ls
94 2018/01/06 07:28:41 find /etc -type f -name "*.conf"
95 2018/01/06 07:28:41 find /etc -type f -name "*.conf" -exec cat {} >> 1.txt
96 2018/01/06 07:28:41 find /etc -type f -name "*.conf" -exec cat {} >> 1.txt \;
97 2018/01/06 07:28:41 ls
98 2018/01/06 07:28:41 du -sh 1.txt
99 2018/01/06 07:28:41 find /etc -type f -name "*.conf" -exec cat {} >> 1.txt \;
100 2018/01/06 07:28:41 du -sh 1.txt
101 2018/01/06 07:28:41 wc -l 1.txt
102 2018/01/06 07:28:41 gzip 1.txt
103 2018/01/06 07:28:41 ls
104 2018/01/06 07:28:41 du -sh 1.txt.gz
105 2018/01/06 07:28:41 gzip -d 1.txt.gz
106 2018/01/06 07:28:41 ls
107 2018/01/06 07:28:41 du -sh 1.txt
108 2018/01/06 07:28:41 wc -l 1.txt
109 2018/01/06 07:28:41 gzip -1 1.txt
110 2018/01/06 07:28:41 ls
111 2018/01/06 07:28:41 du -sh 1.txt.gz
112 2018/01/06 07:28:41 gunzip 1.txt.gz
113 2018/01/06 07:28:41 ls
114 2018/01/06 07:28:41 gzip -9 1.txt
115 2018/01/06 07:28:41 ls
116 2018/01/06 07:28:41 du -sh 1.txt.gz
117 2018/01/06 07:28:41 file 1.txt.gz
118 2018/01/06 07:28:41 zcat 1.txt.gz
119 2018/01/06 07:28:41 ls
120 2018/01/06 07:28:41 gzip -d 1.txt.gz
121 2018/01/06 07:28:41 ls
122 2018/01/06 07:28:41 gzip -c 1.txt
123 2018/01/06 07:28:41 ls
124 2018/01/06 07:28:41 gzip -c 1.txt > /root/1.txt.gz
125 2018/01/06 07:28:41 ls
126 2018/01/06 07:28:41 cd /root
127 2018/01/06 07:28:41 ls
128 2018/01/06 07:28:41 rm -rf 1.txt.gz
129 2018/01/06 07:28:41 cd
130 2018/01/06 07:28:41 ls
131 2018/01/06 07:28:41 cd /
132 2018/01/06 07:28:41 ls
133 2018/01/06 07:28:41 cd tmp
134 2018/01/06 07:28:41 ls
135 2018/01/06 07:28:41 cd /tmp/d6z
136 2018/01/06 07:28:41 ls
137 2018/01/06 07:28:41 gzip -c 1.txt > /tmp/1.txt.gz
138 2018/01/06 07:28:41 ls /tmp/1.txt.gz
139 2018/01/06 07:28:41 file /tmp/1.txt.gz
140 2018/01/06 07:28:41 gzip -d -c /tmp/1.txt.gz /tmp/d6z/2.txt
141 2018/01/06 07:28:41 ls
142 2018/01/06 07:28:41 gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt
143 2018/01/06 07:28:41 ls
144 2018/01/06 07:28:41 wc -l *.txt
145 2018/01/06 07:28:41 du -sh *.txt
146 2018/01/06 07:28:41 yum install -y bzip2
147 2018/01/06 07:28:41 ls
148 2018/01/06 07:28:41 bzip2 1.txt
149 2018/01/06 07:28:41 ls
150 2018/01/06 07:28:41 du -sh 1.txt.bz2
151 2018/01/06 07:28:41 du -sh 2.txt
152 2018/01/06 07:28:41 bzip2 -d 1.txt.bz2
153 2018/01/06 07:28:41 bzip2 1.txt
154 2018/01/06 07:28:41 bunzip2 1.txt.bz2
155 2018/01/06 07:28:41 ls
156 2018/01/06 07:28:41 bzip2 -c 1.txt > /tmp/1.txt.bz2
157 2018/01/06 07:28:41 du -sh /tmp/1.txt.bz2
158 2018/01/06 07:28:41 bzip2 -d -c /tmp/1.txt.bz2 > 3.txt
159 2018/01/06 07:28:41 ls
160 2018/01/06 07:28:41 du -sh 3.txt
161 2018/01/06 07:28:41 ls
162 2018/01/06 07:28:41 bzip2 -9 1.txt
163 2018/01/06 07:28:41 du -sh 1.txt.bz2
164 2018/01/06 07:28:41 file 1.txt.bz2
165 2018/01/06 07:28:41 mv 1.txt.bz2 1.txt
166 2018/01/06 07:28:41 ls
167 2018/01/06 07:28:41 less 1.txt
168 2018/01/06 07:28:41 file 1.txt
169 2018/01/06 07:28:41 file 2.txt
170 2018/01/06 07:28:41 mv 1.txt 1.txt.bz2
171 2018/01/06 07:28:41 bzcat 1.txt.bz2
172 2018/01/06 07:28:41 ls
173 2018/01/06 07:28:41 xz 2.txt
174 2018/01/06 07:28:41 ls
175 2018/01/06 07:28:41 du -sh 2.txt.xz
176 2018/01/06 07:28:41 [root@localhost d6z]# ls
177 2018/01/06 07:28:41 1.txt.bz2 2.txt 3.txt
178 2018/01/06 07:28:41 [root@localhost d6z]# xz 2.txt
179 2018/01/06 07:28:41 [root@localhost d6z]# ls
180 2018/01/06 07:28:41 1.txt.bz2 2.txt.xz 3.txt
181 2018/01/06 07:28:41 [root@localhost d6z]# du -sh 2.txt.xz
182 2018/01/06 07:28:41 204K2.txt.xz
183 2018/01/06 07:28:41 du -sh 3.txt
184 2018/01/06 07:28:41 ls
185 2018/01/06 07:28:41 xz -d 2.txt.xz
186 2018/01/06 07:28:41 xz 2.txt
187 2018/01/06 07:28:41 unxz 2.txt.xz
188 2018/01/06 07:28:41 ls
189 2018/01/06 07:28:41 xz -c 2.txt > /tmp/2.txt.xz
190 2018/01/06 07:28:41 xz -d -c /tmp/2.txt/xz > ./4.txt
191 2018/01/06 07:28:41 xz -d -c /tmp/2.txt.xz > ./4.txt
192 2018/01/06 07:28:41 echo $HISTTIMEFORMAT
193 2018/01/06 07:30:03 history
[root@localhost ~]#
如何讓命令歷史永久保存,不讓別人刪除呢?能夠給配置文件增長一個隱藏權限a,表示只能追加不能刪除了。即便不設置最大權限1000條,寫過的命令仍是會繼續保存,是沒有權限刪除的。
[root@localhost ~]# chattr +a ~/.bash_history
[root@localhost ~]#
若是遇到非正常退出的狀況,一些了歷史命令就不會被保存在配置文件中。
[root@localhost ~]# w
07:41:01 up 51 min, 2 users, load average: 0.00, 0.06, 0.07
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.231.1 06:58 5.00s 0.24s 0.02s w
root pts/2 192.168.231.1 07:28 11:01 0.07s 0.07s -bash
[root@localhost ~]#
!!表示最後一條歷史命令。
[root@localhost ~]# !!
w
07:42:04 up 52 min, 2 users, load average: 0.00, 0.05, 0.06
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.231.1 06:58 4.00s 0.23s 0.01s w
root pts/2 192.168.231.1 07:28 12:04 0.07s 0.07s -bash
[root@localhost ~]#
!n會執行歷史命令中第n條命令。
!echo會倒着往前找,第一條以echo開頭的命令。
[root@localhost ~]# !echo
echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@localhost ~]#
8.3 命令補全和別名
Tab鍵能夠補全一個命令、路徑、或者一個文件名。連續按兩次tab鍵,系統則會把全部的命令或者文件名都列出來。
• tab鍵,敲一下,敲兩下
• 參數補全,安裝bash-completion
使用命令yum install -y bash-completion,而後重啓才能實現參數補全。
CentOS6只能補全命令,CentOS7能夠補全命令和參數。
• alias別名給命令從新起個名字
alias是bash特有的功能之一,咱們能夠經過alias把一個經常使用的而且很長的指令另外取一個簡單易記的名字。若是不想用了,可使用unalias命令解除別名功能。直接執行alias命令,會看到目前系統預設的別名。
自定義別名的格式:
alias [命令別名]=[‘具體的命令’]
[root@localhost ~]# alias restartnet='systemctl restart network.service'
[root@localhost ~]# restartnet
[root@localhost ~]# 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 perlll='eval `perl -Mlocal::lib`'
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@localhost ~]#
• 各用戶都有本身配置別名的文件 ~/.bashrc
[root@localhost ~]# vim .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
這個配置文件的別名不多,剩下的在/etc/profile.d/下定義。
[root@localhost ~]# cd /etc/profile.d/
[root@localhost profile.d]# ls
256term.csh bash_completion.sh colorls.csh kde.csh lang.sh PackageKit.sh qt.csh qt.sh vte.sh
256term.sh colorgrep.csh colorls.sh kde.sh less.csh perl-homedir.csh qt-graphicssystem.csh vim.csh which2.csh
abrt-console-notification.sh colorgrep.sh flatpak.sh lang.csh less.sh perl-homedir.sh qt-graphicssystem.sh vim.sh which2.sh
[root@localhost profile.d]# vim colorls.sh
# color-ls initialization
# Skip all for noninteractive shells.
[ ! -t 0 ] && return
#when USER_LS_COLORS defined do not override user LS_COLORS, but use them.
if [ -z "$USER_LS_COLORS" ]; then
alias ll='ls -l' 2>/dev/null
alias l.='ls -d .*' 2>/dev/null
INCLUDE=
COLORS=
for colors in "$HOME/.dir_colors.$TERM" "$HOME/.dircolors.$TERM" \
"$HOME/.dir_colors" "$HOME/.dircolors"; do
[ -e "$colors" ] && COLORS="$colors" && \
INCLUDE="`/usr/bin/cat "$COLORS" | /usr/bin/grep '^INCLUDE' | /usr/bin/cut -d ' ' -f2-`" && \
break
done
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.$TERM" ] && \
COLORS="/etc/DIR_COLORS.$TERM"
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS.256color" ] && \
[ "x`/usr/bin/tty -s && /usr/bin/tput colors 2>/dev/null`" = "x256" ] && \
COLORS="/etc/DIR_COLORS.256color"
[ -z "$COLORS" ] && [ -e "/etc/DIR_COLORS" ] && \
COLORS="/etc/DIR_COLORS"
# Existence of $COLORS already checked above.
[ -n "$COLORS" ] || return
if [ -e "$INCLUDE" ];
then
TMP="`/usr/bin/mktemp .colorlsXXX -q --tmpdir=/tmp`"
/alias
[root@localhost profile.d]# vim colorgrep.sh
# color-grep initialization
/usr/libexec/grepconf.sh -c || return
alias grep='grep --color=auto' 2>/dev/null
alias egrep='egrep --color=auto' 2>/dev/null
alias fgrep='fgrep --color=auto' 2>/dev/null
別名就放在.bashrc和/etc/profile.d
• 自定義的alias放到~/.bashrc
unalias取消自定義的命令。
[root@localhost profile.d]# unalias restartnet
[root@localhost profile.d]# restartnet
bash: restartnet: command not found...
[root@localhost profile.d]#
8.4 通配符
在bash下,可使用*匹配零個或多個字符,用?匹配一個字符。
[root@localhost ~]# ls
123 2.txt 456 789 anaconda-ks.cfg.1 initial-setup-ks.cfg perl5
[root@localhost ~]# ls *.txt
2.txt
[root@localhost ~]# ls *txt
2.txt
[root@localhost ~]# ls *txt*
2.txt
[root@localhost ~]# ls 2*
2.txt
[root@localhost ~]#
*不論是什麼字符,也不論是幾個字符,不論是文件仍是目錄,能夠通通列出來。
[root@localhost ~]# ls
123 2.txt 456 789 anaconda-ks.cfg.1 initial-setup-ks.cfg perl5
[root@localhost ~]# ls ?.txt
2.txt
[root@localhost ~]# touch 1.txt
[root@localhost ~]# touch 3.txt
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt
[root@localhost ~]# touch a.txt
[root@localhost ~]# touch bb.txt
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt a.txt
[root@localhost ~]#
?表明任意一個字符。
[]裏面能夠寫一個範圍,也能夠所有一一列出,可是隻選擇[]裏面的一個字符。
[root@localhost ~]# ls [1-3].txt
1.txt 2.txt 3.txt
[root@localhost ~]# ls [123].txt
1.txt 2.txt 3.txt
[root@localhost ~]# ls [23].txt
2.txt 3.txt
[root@localhost ~]# ls [13].txt
1.txt 3.txt
[root@localhost ~]# ls [0-9].txt
1.txt 2.txt 3.txt
[root@localhost ~]# ls [0-9a-z].txt
1.txt 2.txt 3.txt a.txt
[root@localhost ~]# ls [0-9a-zA-Z].txt
1.txt 2.txt 3.txt a.txt
{}和[]相似,可是{}裏面須要加,,也是或的意思,選取其中一個。
[root@localhost ~]# ls {1,2}.txt
1.txt 2.txt
[root@localhost ~]# ls {1,2,3,a}.txt
1.txt 2.txt 3.txt a.txt
[root@localhost ~]#
8.5 輸入輸出重定向
輸入重定向用於改變命令的輸入,輸出重定向用於改變命令的輸出。輸出重定向更經常使用,它常常用於將命令的結果輸入到文件中,而不是屏幕上。輸入重定向的命令是<,輸出重定向的命令是>,錯誤重定向的命令是2>以及追加劇定向的命令是>>。
[root@localhost ~]# lsaaa
bash: lsaaa: command not found...
[root@localhost ~]# lsaaa 2> a.txt
[root@localhost ~]# cat !$
cat a.txt
bash: lsaaa: command not found...
[root@localhost ~]# lsaaa 2>> a.txt
[root@localhost ~]# cat a.txt
bash: lsaaa: command not found...
bash: lsaaa: command not found...
>和2>合在一塊兒能夠用&>表示。
[root@localhost ~]# ls [12].txt aaa.txt &> a.txt
[root@localhost ~]# cat !$
cat a.txt
ls: cannot access aaa.txt: No such file or directory
1.txt
2.txt
上面這個符號一樣支持追加。
[root@localhost ~]# ls [12].txt aaa.txt &>> a.txt
[root@localhost ~]# cat !$
cat a.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
能夠把正確和錯誤的輸出分開,在寫shell腳本時,能夠正確和錯誤的分別輸出到不一樣的文件中。
[root@localhost ~]# ls [12].txt aaa.txt > 1.txt 2>a.txt
[root@localhost ~]# cat 1.txt
1.txt
2.txt
[root@localhost ~]# cat a.txt
ls: cannot access aaa.txt: No such file or directory
<叫輸入重定向,就是把右邊的內容做爲輸入給到左邊的命令。例如,查看1.txt的行數。
[root@localhost ~]# wc -l < 1.txt
2
輸入重定向只支持文件到命令,不支持文件到文件。
[root@localhost ~]# 2.txt < 1.txt
bash: 2.txt: command not found...
[root@localhost ~]#
既能夠寫做wc -l < 1.txt,又能夠寫做wc -l > 1.txt,可是結果不一樣。輸入重定向幾乎不會用,瞭解便可。
友情連接:阿銘Linux