Shell扩展
一个?表示一个任意字符
[root@C7 tmp]# ls
dir1 file1.txt file2.txt file3.txt fileb.txt yangge.txt
file1 file2 file3 filea.txt yangge.log
[root@C7 tmp]# ll file1 file2 file3
-rw-r--r-- 1 root root 0 Nov 8 19:46 file1
-rw-r--r-- 1 root root 0 Nov 8 19:46 file2
-rw-r--r-- 1 root root 0 Nov 8 19:46 file3
[root@C7 tmp]# ll file?
-rw-r--r-- 1 root root 0 Nov 8 19:46 file1
-rw-r--r-- 1 root root 0 Nov 8 19:46 file2
-rw-r--r-- 1 root root 0 Nov 8 19:46 file3
[root@C7 tmp]# ll fil??
-rw-r--r-- 1 root root 0 Nov 8 19:46 file1
-rw-r--r-- 1 root root 0 Nov 8 19:46 file2
-rw-r--r-- 1 root root 0 Nov 8 19:46 file3
*表示任意多个字符
[root@C7 tmp]# ls
dir1 file1.txt file2.txt file3.txt fileb.txt yangge.txt
file1 file2 file3 filea.txt yangge.log
[root@C7 tmp]# ls file*
file1 file1.txt file2 file2.txt file3 file3.txt filea.txt fileb.txt
[root@C7 tmp]# ls *.txt
file1.txt file2.txt file3.txt filea.txt fileb.txt yangge.txt
[root@C7 tmp]# ls *a*
filea.txt yangge.log yangge.txt
[ ]匹配任意一个字符,反向^,!
[root@C7 tmp]# ls file[12]
file1 file2
[root@C7 tmp]# ls file[1234a]
file1 file2 file3
[root@C7 tmp]# ls file[^12] 取反意思
file3
[root@C7 tmp]# ls
dir1 file1.txt file2.txt file3.txt fileb.txt yangge.txt
file1 file2 file3 filea.txt yangge.log
[root@C7 tmp]# ls file[!12] 取反意思
file3
应用场景:对文件批量删除
[root@C7 tmp]# rm -rvf file[12]
removed ‘file1’
removed ‘file2’
---------------------------
[root@C7 tmp]# ls
dir1 file2.txt file3.txt fileb.txt yangge.txt
file1.txt file3 filea.txt yangge.log
[root@C7 tmp]# ls .
dir1 file2.txt file3.txt fileb.txt yangge.txt
file1.txt file3 filea.txt yangge.log
[root@C7 tmp]# ls * -d
dir1 file2.txt file3.txt fileb.txt yangge.txt
file1.txt file3 filea.txt yangge.log
在匹配过程中不想匹配到下一层
[root@C7 ~]# ll /etc/pa* -d
drwxr-xr-x. 2 root root 4096 Oct 17 13:15 /etc/pam.d
-rw-r--r--. 1 root root 3593 Nov 5 13:15 /etc/passwd
-rw-r--r--. 1 root root 3593 Nov 5 13:14 /etc/passwd-
[root@C7 ~]# ll -d /etc/pa*
drwxr-xr-x. 2 root root 4096 Oct 17 13:15 /etc/pam.d
-rw-r--r--. 1 root root 3593 Nov 5 13:15 /etc/passwd
-rw-r--r--. 1 root root 3593 Nov 5 13:14 /etc/passwd-
root@C7 ~]# ll /etc/pa??? -d
drwxr-xr-x. 2 root root 4096 Oct 17 13:15 /etc/pam.d
{ }里面支持两种写法
1.双点语法,指一个范围
[root@C7 tmp]# touch file{1..10}
[root@C7 tmp]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@C7 tmp]# touch file{a..z}
[root@C7 tmp]# ls
filea filed fileg filej filem filep files filev filey
fileb filee fileh filek filen fileq filet filew filez
filec filef filei filel fileo filer fileu filex
2.逗号隔开参数
[root@C7 tmp]# touch li{aa,bb,cc}
[root@C7 tmp]# ls
liaa libb licc
应用场景:
[root@C7 tmp]# cp -r /etc/sysconfig/network /etc/sysconfig/network.bak
[root@C7 tmp]# cp -rv /etc/sysconfig/network{,.bak}
‘/etc/sysconfig/network’ -> ‘/etc/sysconfig/network.bak’
[root@C7 tmp]# touch file{a,b}{1,2}.txt
[root@C7 tmp]# ls
filea1.txt filea2.txt fileb1.txt fileb2.txt
[root@C7 tmp]# touch file{a{1,2},b,c}
[root@C7 tmp]# ls
filea1 filea2 fileb filec
~ 指用户家目录
[root@C7 tmp]# cd ~root 进到用户的家目录
[root@C7 ~]# pwd
/root
[root@C7 ~]# cd ~li
[root@C7 li]# pwd
/home/li
[root@C7 li]#
[root@C7 ~]# cp -rvf /etc/services ~li/Pictures
‘/etc/services’ -> ‘/home/li/Pictures’
变量扩展
[root@C7 ~]# path1="/etc/sysconfig"
[root@C7 ~]# cd $path1
[root@C7 sysconfig]# pwd
/etc/sysconfig
[root@C7 sysconfig]# cp -rvf /etc/services $path1
‘/etc/services’ -> ‘/etc/sysconfig/services’
[root@C7 sysconfig]# echo $path1
/etc/sysconfig
系统自带变量=环境变量
命令替换:应用场景:创建以当天日期命名的文件
数据库备份的时候希望备份脚本按照当天日期生成脚本。
1.获取当前日期
[root@C7 ~]# date +%R 几点几分
12:13
[root@C7 ~]# date +%F 年月日
2024-11-09
[root@C7 ~]# touch $(date +%F)-mysql.bak
[root@C7 ~]# ls
2024-11-09-mysql.bak Desktop Downloads Music Public Videos
[root@C7 ~]# touch `date +%F`-file.bak
[root@C7 ~]# ls
2024-11-09-file.bak Desktop initial-setup-ks.cfg Public
\ 转义字符
[root@C7 ~]# echo ${HOSTNAME}
C7
[root@C7 ~]# echo \$HOSTNAME
$HOSTNAME
[root@C7 ~]# touch li\ si
[root@C7 ~]# ls
2024-11-09-file.bak Desktop initial-setup-ks.cfg Pictures Videos
2024-11-09-mysql.bak Documents li si Public
单引号-强引用(是啥就是啥)
双引号-弱引用
[root@C7 ~]# echo '** lili aiai $HOSTNAME **'
** lili aiai $HOSTNAME **
[root@C7 ~]# echo "** lili aiai $HOSTNAME **"
** lili aiai C7 **