目录
3、移动除了 dir1 目录以及 file1 文件到 backup 目录下
4、一个日志目录,里头存着很多 00, 01, 02 … 31 的日期目录,如何列示出00到12号之间的所有目录?也就是说,如何在文件名上面,使用正则?
最简单的方法是
# shopt -s extglob (打开extglob模式)
如果是单个排除的,可以这样:
# rm -fr !(file1)
如果是多个要排除的,可以这样:
# rm -rf !(file1|file2)
扩充知识点:
一、shopt 命令可以设置 shell 的可选参数
shopt [-psu] [optname …]
-s 开启某个选项.
-u 关闭某个选项.
-p 列出所有选项的当前生效命令. (不带-p表示列出所有选项的当前状态)
#查看 extglob 选项是否开启(默认是off)
[root@alihost-1 ~]# shopt extglob
extglob off
#开启 extglob 选项
[root@alihost-1 ~]# shopt -s extglob
[root@alihost-1 ~]# shopt extglob
extglob on
#关闭 extglob 选项
[root@alihost-1 ~]# shopt -u extglob
[root@alihost-1 ~]# shopt extglob
extglob off
[root@alihost-1 test]# shopt
autocd off
cdable_vars off
cdspell off
checkhash off
checkjobs off
checkwinsize on
cmdhist on
compat31 off
compat32 off
compat40 off
compat41 off
direxpand off
dirspell off
dotglob off
execfail off
expand_aliases on
extdebug off
extglob on
extquote on
failglob off
force_fignore on
globstar off
gnu_errfmt off
histappend on
histreedit off
histverify off
hostcomplete off
huponexit off
interactive_comments on
lastpipe off
lithist off
login_shell on
mailwarn off
no_empty_cmd_completion off
nocaseglob off
nocasematch off
nullglob off
progcomp on
promptvars on
restricted_shell off
shift_verbose off
sourcepath on
xpg_echo off
[root@alihost-1 test]#
[root@alihost-1 ~]# shopt -p
shopt -u autocd
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -u checkjobs
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u compat32
shopt -u compat40
shopt -u compat41
shopt -u direxpand
shopt -u dirspell
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -u extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u globstar
shopt -u gnu_errfmt
shopt -s histappend
shopt -u histreedit
shopt -u histverify
shopt -u hostcomplete
shopt -u huponexit
shopt -s interactive_comments
shopt -u lastpipe
shopt -u lithist
shopt -s login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo
[root@alihost-1 ~]#
二、extglob选项【识别正则】
?(pattern-list) #所给模式匹配0次或1次
*(pattern-list) #所给模式匹配0次以上包括0次
+(pattern-list) #所给模式匹配1次以上包括1次
@(pattern-list) #所给模式仅仅匹配1次
!(pattern-list) #不匹配括号内的所给模式
实例
1、将目录下所有的文件和目录移动到 backup 目录中.
[root@alihost-1 test]# shopt extglob
extglob off
[root@alihost-1 test]# shopt -s extglob
[root@alihost-1 test]# shopt extglob
extglob on
#当前目录下3个文件以及3个目录
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 13:57 file1.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file3.txt
#新建一个 backup 目录
[root@alihost-1 test]# mkdir backup
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 backup
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 13:57 file1.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file3.txt
#将除了 backup 目录的其他文件和目录移动到 backup 目录下
[root@alihost-1 test]# mv !(backup) backup/
#查看结果
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 5 root root 93 Jan 24 14:00 backup
[root@alihost-1 test]# ll backup/
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 13:57 file1.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 13:57 file3.txt
[root@alihost-1 test]#
2、删除当前目录下所有file开头的文件或目录
[root@alihost-1 test]# cd backup/
[root@alihost-1 backup]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 14:06 file1.txt
-rw-r--r-- 1 root root 0 Jan 24 14:06 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 14:06 file3.txt
drwxr-xr-x 2 root root 6 Jan 24 14:07 file4.txt
[root@alihost-1 backup]# rm -rf file[1-4].txt
[root@alihost-1 backup]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
[root@alihost-1 backup]#
3、移动除了 dir1 目录以及 file1 文件到 backup 目录下
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 14:09 backup
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 14:08 file1.txt
-rw-r--r-- 1 root root 0 Jan 24 14:08 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 14:08 file3.txt
drwxr-xr-x 2 root root 6 Jan 24 14:09 file4.txt
[root@alihost-1 test]# mv !(dir1|file1.txt|backup) backup/
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 5 root root 81 Jan 24 14:10 backup
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir1
-rw-r--r-- 1 root root 0 Jan 24 14:08 file1.txt
[root@alihost-1 test]# ll backup/
total 0
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir2
drwxr-xr-x 2 root root 6 Jan 24 13:57 dir3
-rw-r--r-- 1 root root 0 Jan 24 14:08 file2.txt
-rw-r--r-- 1 root root 0 Jan 24 14:08 file3.txt
drwxr-xr-x 2 root root 6 Jan 24 14:09 file4.txt
[root@alihost-1 test]#
4、一个日志目录,里头存着很多 00, 01, 02 … 31 的日期目录,如何列示出00到12号之间的所有目录?也就是说,如何在文件名上面,使用正则?
问题解决:如何列示出00到12号之间的所有目录?
#模拟文件和目录
[root@alihost-1 test]# mkdir 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
[root@alihost-1 test]# ll
total 0
drwxr-xr-x 2 root root 6 Jan 24 14:26 00
drwxr-xr-x 2 root root 6 Jan 24 14:26 01
drwxr-xr-x 2 root root 6 Jan 24 14:26 02
drwxr-xr-x 2 root root 6 Jan 24 14:26 03
drwxr-xr-x 2 root root 6 Jan 24 14:26 04
drwxr-xr-x 2 root root 6 Jan 24 14:26 05
drwxr-xr-x 2 root root 6 Jan 24 14:26 06
drwxr-xr-x 2 root root 6 Jan 24 14:26 07
drwxr-xr-x 2 root root 6 Jan 24 14:26 08
drwxr-xr-x 2 root root 6 Jan 24 14:26 09
drwxr-xr-x 2 root root 6 Jan 24 14:26 10
drwxr-xr-x 2 root root 6 Jan 24 14:26 11
drwxr-xr-x 2 root root 6 Jan 24 14:26 12
drwxr-xr-x 2 root root 6 Jan 24 14:26 13
drwxr-xr-x 2 root root 6 Jan 24 14:26 14
drwxr-xr-x 2 root root 6 Jan 24 14:26 15
drwxr-xr-x 2 root root 6 Jan 24 14:26 16
drwxr-xr-x 2 root root 6 Jan 24 14:26 17
drwxr-xr-x 2 root root 6 Jan 24 14:26 18
drwxr-xr-x 2 root root 6 Jan 24 14:26 19
drwxr-xr-x 2 root root 6 Jan 24 14:26 20
drwxr-xr-x 2 root root 6 Jan 24 14:26 21
drwxr-xr-x 2 root root 6 Jan 24 14:26 22
drwxr-xr-x 2 root root 6 Jan 24 14:26 23
drwxr-xr-x 2 root root 6 Jan 24 14:26 24
drwxr-xr-x 2 root root 6 Jan 24 14:26 25
drwxr-xr-x 2 root root 6 Jan 24 14:26 26
drwxr-xr-x 2 root root 6 Jan 24 14:26 27
drwxr-xr-x 2 root root 6 Jan 24 14:26 28
drwxr-xr-x 2 root root 6 Jan 24 14:26 29
drwxr-xr-x 2 root root 6 Jan 24 14:26 30
drwxr-xr-x 2 root root 6 Jan 24 14:26 31
[root@alihost-1 test]# touch 00/20210100.txt
[root@alihost-1 test]# touch 01/20210101.txt
[root@alihost-1 test]# touch 02/20210102.txt
[root@alihost-1 test]# touch 03/20210103.txt
[root@alihost-1 test]# touch 04/20210104.txt
[root@alihost-1 test]# touch 05/20210105.txt
[root@alihost-1 test]# touch 06/20210106.txt
[root@alihost-1 test]# touch 07/20210107.txt
[root@alihost-1 test]# touch 08/20210108.txt
[root@alihost-1 test]# touch 09/20210109.txt
[root@alihost-1 test]# touch 10/20210110.txt
[root@alihost-1 test]# touch 11/20210111.txt
[root@alihost-1 test]# touch 12/20210112.txt
[root@alihost-1 test]# touch 02/20210102.txt
[root@alihost-1 test]# ls -l +(0[0-9]|1[0-2])
00:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:29 20210100.txt
01:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:29 20210101.txt
02:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:31 20210102.txt
03:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210103.txt
04:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210104.txt
05:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210105.txt
06:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210106.txt
07:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210107.txt
08:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210108.txt
09:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210109.txt
10:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210110.txt
11:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:30 20210111.txt
12:
total 0
-rw-r--r-- 1 root root 0 Jan 24 14:31 20210112.txt
[root@alihost-1 test]#
说明
平时shell的通配符,只是通配语义,不是正则语义
加上这个 extglob 之后,才能是正则语义
语法格式是 +正则