Linux中的通配符的使用

洼地云 tuoyidashi.png

当我们要同时对多个文件进行操作的时候,我们就可以使用通配符,这些符号不是作为普通的文字来使用,而是来代替文键名称的字符串。我们来看看Linux下的几种通配符和他们的含义:

  • *:任何字符和字符串;
  • ?:一个任意字符;
  • [abc…] :[]内的任意一个字符,如[abc]表示a、b、c任一个字符;有时候也表示范围,如[a-x],表示a到x的任一个字符;[1-9]表示1到9的任一数字;
  • [!abc…]:和上面的相反,表示除[]内的字符外的任意一个字符。

我们来分别看看这些符号的实际操作中的实例:

[root@Linux two]# ll
总计 3
-rw-r--r-- 1 root root 0 03-20 20:08 111.txt
-rw-r--r-- 1 root root 0 03-20 20:08 112.txt
-rw-r--r-- 1 root root 0 03-20 20:08 113.txt
[root@Linux two]# rm 1*.txt
rm:是否删除一般空文件 "111.txt"? y
rm:是否删除一般空文件 "112.txt"? y
rm:是否删除一般空文件 "113.txt"? y
[root@Linux two]# ll
总计 0

这样只要是以“1”开头的文件全部删除。我们在来看下面的操作:

[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 20:09 111.txt
-rw-r--r-- 1 root root 0 03-20 20:09 112.txt
-rw-r--r-- 1 root root 0 03-20 20:09 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 11?.txt
[root@Linux two]# ll
总计 1
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt

这样就删除了以“11”开头的文件,我们还是以上面的例子再看:

[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 20:11 111.txt
-rw-r--r-- 1 root root 0 03-20 20:11 112.txt
-rw-r--r-- 1 root root 0 03-20 20:11 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 11[1-3].txt
[root@Linux two]# ll
总计 1
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt

这样就删除了以“11”开头,后面是以1、2、3结尾的文件。最后我们来看:

[root@Linux two]# ll
总计 4
-rw-r--r-- 1 root root 0 03-20 20:13 111.txt
-rw-r--r-- 1 root root 0 03-20 20:13 112.txt
-rw-r--r-- 1 root root 0 03-20 20:13 113.txt
-rw-r--r-- 1 root root 0 03-20 20:10 121.txt
[root@Linux two]# rm -f 1[!1]*.txt
[root@Linux two]# ll
总计 3
-rw-r--r-- 1 root root 0 03-20 20:13 111.txt
-rw-r--r-- 1 root root 0 03-20 20:13 112.txt
-rw-r--r-- 1 root root 0 03-20 20:13 113.txt

这样就删除了第二个字符不是为“1”的任何文件。
有了通配符,我们就可以针对多个文件一起进行操作。

赞(1)
未经允许禁止转载:优米格 » Linux中的通配符的使用

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册