4-17 Linux中的通配符

2021-12-30  本文已影响0人  捌千里路雲和月

1、通配符:它由 shell 解析,一般用于搜索时匹配文件名或目录名。

2、常用通配符:

3、实操练习:

[root@localhost ~]# ll /tmp/    ## /tmp/ 目录下的文件
total 4
-rw-------. 1 root root 27 Oct 19 09:06 crontab.615m7n
drwx------. 2 root root  6 Oct 19 08:59 vmware-root_582-2722173592
drwx------. 2 root root  6 Oct 20 08:53 vmware-root_583-3988162886
drwx------. 2 root root  6 Oct 18 16:26 vmware-root_589-4013264586
drwx------. 2 root root  6 Oct 19 11:28 vmware-root_595-4013788883
drwx------. 2 root root  6 Oct 22 11:47 vmware-root_598-2689143944
[root@localhost ~]# 
[root@localhost ~]# rm -rf /tmp/*  ## * 号匹配任意多个字符,删除当前目录下所有内容
[root@localhost ~]# 
[root@localhost ~]# ll /tmp/    ## 删除后的 /tmp/ 目录已经没有文件
total 0
[root@localhost ~]# 

[root@localhost ~]# ll /etc/*.txt
-rw-r--r--. 1 root root 165 Mar 17  2021 /etc/passwd.txt
-rw-r--r--. 1 root root 460 Mar 17  2021 /etc/user.txt
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# ll /etc/y*
-rw-r--r--. 1 root root 970 Oct  2  2020 /etc/yum.conf    ## /etc/ 目录下 y 开头的文件

/etc/yum:    ## /etc/ 目录下 y 开头的目录及目录下的文件,此处是 yum 目录及内容。
total 4
drwxr-xr-x. 2 root root   6 Oct  2  2020 fssnap.d
drwxr-xr-x. 2 root root  54 Oct  2  2020 pluginconf.d
drwxr-xr-x. 2 root root  26 Mar 22  2021 protected.d
drwxr-xr-x. 2 root root  37 Oct  2  2020 vars
-rw-r--r--. 1 root root 444 Oct  2  2020 version-groups.conf

/etc/yum.repos.d:    ## /etc/ 目录下 y 开头的目录及目录下的文件,此处是 yum.repos.d 目录及内容。
total 8
-rw-r--r--. 1 root root 2523 Dec 26  2020 CentOS-Base.repo
-rw-r--r--. 1 root root   70 Apr  2  2021 first.repo
drwxr-xr-x. 2 root root  220 Apr  2  2021 repos
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# ll f*.txt
-rw-r--r--. 1 root root 21 Oct 21 11:14 file1.txt
-rw-r--r--. 1 root root 21 Oct 21 11:04 file.txt
[root@localhost ~]# 


[root@localhost ~]# ll /etc/pass??
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]#
[root@localhost ~]# 
[root@localhost ~]# ll /etc/pa??wd
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]# 

[root@localhost ~]# ll /etc/??????    ## 6个 ? 号表示占位为 6个字符的任意组合
-rw-r--r--. 1 root root 2853 Apr  1  2020 /etc/bashrc
-rw-r--r--. 1 root root  577 Jun  4 11:25 /etc/group-
lrwxrwxrwx. 1 root root   11 Mar 22  2021 /etc/init.d -> rc.d/init.d
-rw-r--r--. 1 root root  570 Sep 30  2020 /etc/my.cnf
-rw-r--r--. 1 root root  915 Jun  4 11:25 /etc/passwd
-r--------. 1 root root  964 Jun  4 11:26 /etc/shadow
-rw-r--r--. 1 root root   44 Apr  1  2020 /etc/shells
-rw-r--r--. 1 root root   54 Jun  4 11:25 /etc/subgid
-rw-r--r--. 1 root root   54 Jun  4 11:25 /etc/subuid
-rw-r--r--. 1 root root 4479 Aug  4  2017 /etc/wgetrc

/etc/audisp:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 audisp 目录及内容。
total 4
-rw-r-----. 1 root root 246 Aug  8  2019 audispd.conf
drwxr-x---. 2 root root  45 Mar 22  2021 plugins.d

/etc/cron.d:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 cron.d 目录及内容。
total 4
-rw-r--r--. 1 root root 128 Aug  9  2019 0hourly

/etc/dbus-1:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 dbus-1 目录及内容。
total 12
-rw-r--r--. 1 root root  838 Sep 30  2020 session.conf
drwxr-xr-x. 2 root root    6 Sep 30  2020 session.d
-rw-r--r--. 1 root root  833 Sep 30  2020 system.conf
drwxr-xr-x. 2 root root 4096 Mar 22  2021 system.d

/etc/gcrypt:    ## /etc/ 目录下 6 个字符命名的目录,此处是 gcrypt 目录。
total 0

/etc/grub.d:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 grub.d 目录及内容。
total 72
-rwxr-xr-x. 1 root root  8702 Mar 17  2021 00_header
-rwxr-xr-x. 1 root root  1043 Mar 22  2019 00_tuned
-rwxr-xr-x. 1 root root   232 Mar 17  2021 01_users
-rwxr-xr-x. 1 root root 10781 Mar 17  2021 10_linux
-rwxr-xr-x. 1 root root 10275 Mar 17  2021 20_linux_xen
-rwxr-xr-x. 1 root root  2559 Mar 17  2021 20_ppc_terminfo
-rwxr-xr-x. 1 root root 11169 Mar 17  2021 30_os-prober
-rwxr-xr-x. 1 root root   214 Mar 17  2021 40_custom
-rwxr-xr-x. 1 root root   216 Mar 17  2021 41_custom
-rw-r--r--. 1 root root   483 Mar 17  2021 README

/etc/kernel:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 kernel 目录及内容。
total 0
drwxr-xr-x. 2 root root 42 Mar 22  2021 postinst.d

/etc/pkcs11:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 pkcs11 目录及内容。
total 0
drwxr-xr-x. 2 root root 6 Aug  5  2017 modules

/etc/popt.d:    ## /etc/ 目录下 6 个字符命名的目录,此处是 popt.d 目录。
total 0

/etc/python:    ## /etc/ 目录下 6 个字符命名的目录及目录下的文件,此处是 python 目录及内容。
total 4
-rw-r--r--. 1 root root 381 Nov 17  2020 cert-verification.cfg
[root@localhost ~]# 

[root@localhost ~]# ll /etc/pa???????
ls: cannot access /etc/pa???????: No such file or directory
[root@localhost ~]# 


[root@localhost ~]# 

## 第 5 位字符从 abcdw 中匹配,此处 w 能匹配到 passwd 文件。
[root@localhost ~]# ll /etc/pass[abcdw]d    
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]# 

## 第 5 位字符从 abcd 中匹配,此处没有 passad、passbd、passcd 和 passdd 的文件或目录,所有提示没有找到文件和目录
[root@localhost ~]# ll /etc/pass[abcd]d    
ls: cannot access /etc/pass[abcd]d: No such file or directory
[root@localhost ~]# 

## 第 5 位字符从 a-z 中匹配,此处 w 能匹配到 passwd 文件。
[root@localhost ~]# ll /etc/pass[a-z]d    
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]# 


[root@localhost ~]# 
[root@localhost ~]# ll /etc/pass[!abcdw]d     ## 第 5 位字符除了 abcdw 不匹配,其余 21 个字母都进行匹配。
ls: cannot access /etc/pass[!abcdw]d: No such file or directory
[root@localhost ~]# 
[root@localhost ~]# ll /etc/pass[!abcd]d     ## 第 5 位字符除了 abcd 不匹配,其余 22 个字母都进行匹配。
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]# 
[root@localhost ~]# ll /etc/pass[!a-h]d     ## 第 5 位字符除了a-h 不匹配,其余字母都进行匹配。
-rw-r--r--. 1 root root 915 Jun  4 11:25 /etc/passwd
[root@localhost ~]# 


[root@localhost tmp]# touch {a..z}    ## 创建 a-z
[root@localhost tmp]# ls
a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z
[root@localhost tmp]# 
[root@localhost tmp]# touch {A..Z}     ## 创建 A-Z
[root@localhost tmp]# ls
A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
[root@localhost tmp]# 
[root@localhost tmp]# touch test{1..9}    ## 创建 test1-9
[root@localhost tmp]# ls
test1  test2  test3  test4  test5  test6  test7  test8  test9
[root@localhost tmp]#
[root@localhost tmp]# touch test{1..10}.txt     ## 创建 test1-10.txt
[root@localhost tmp]# ls
test10.txt  test2.txt  test4.txt  test6.txt  test8.txt
test1.txt   test3.txt  test5.txt  test7.txt  test9.txt
[root@localhost tmp]# 

上一篇 下一篇

猜你喜欢

热点阅读