文章目录
- 小试牛刀
- 显示/etc/目录下所有以i开头,以一个小写字符结尾,且中间出现至少一位数字的文件或目录列表
- 显示/etc/目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
- 显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
- 显示/etc/目录下所有以rc开头,并且后面是0-6之间的数字,其它为任意字符的文件或目录列表
- 显示/etc/目录下所有.conf结尾且以m,n,r,p开头的文件或目录列表
- 只显示/root/下的隐藏文件和目录列表
- 只显示/etc下的非隐藏目录列表
- 每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求目录格式为backup_YYYY-mm-dd
- 创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b
- 创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b
- 创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7
- 案例
-
小试牛刀
通配符 | 说明 |
---|
* | 匹配零个或多个字符,但不匹配“.”开头的文件,即隐藏文件 |
? | 匹配任何单个字符,一个汉字也算一个字符 |
~ | 当前用户目录 |
~用户名 | 当前用户用户名目录 |
[0-9] | 匹配数字范围 |
[a-z] | 一个字母 |
[A-Z] | 一个字母 |
[wang] | 匹配列表中的任何的一个字符 |
[^wang] | 匹配列表中的所有字符以外的字符 |
[^a-z] | 匹配列表中的所有字符意外的字符 |
.和~+ | 当前工作目录 |
~- | 前一个工作目录 |
字符类 | 说明 |
---|
[:digit:] | 任意数字,相当于0-9 |
[:lower:] | 任意小写字母,表示a-z |
[:upper:] | 任意大写字母,表示A-Z |
[:alpha:] | 任意大小写字母 |
[:alnum:] | 任意数字或字母 |
[:blank:] | 水平空白字母 |
[:space:] | 水平或垂直空白字符 |
[:punct:] | 标点符号 |
[:print:] | 可打印字符 |
[:cntrl:] | 控制(非打印)字符 |
[:graph:] | 图形字符 |
[:xdigit:] | 十六进制字符 |
显示/etc/目录下所有以i开头,以一个小写字符结尾,且中间出现至少一位数字的文件或目录列表
[root@Rocky ~]
/etc/i123dft.txt /etc/i1txt /etc/i4546f.txt /etc/i987tst /etc/isdf234ts
显示/etc/目录下以任意一位数字开头,且以非数字结尾的文件或目录列表
[root@Rocky ~]
/etc/123abc /etc/456abc.txt
显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表
[root@Rocky ~]
/etc/123abc /etc/456abc.txt
显示/etc/目录下所有以rc开头,并且后面是0-6之间的数字,其它为任意字符的文件或目录列表
[root@Rocky ~]
/etc/rc123dsfir /etc/rc15.txt /etc/rc235abc /etc/rc36sfe.tst
显示/etc/目录下所有.conf结尾且以m,n,r,p开头的文件或目录列表
[root@Rocky ~]
/etc/man_db.conf /etc/nsswitch.conf /etc/request-key.conf /etc/rsyncd.conf
/etc/mke2fs.conf /etc/perpar.conf /etc/resolv.conf /etc/rsyslog.conf
只显示/root/下的隐藏文件和目录列表
[root@Rocky ~]
dr-xr-x---. 2 root root 170 Mar 10 15:33 .
dr-xr-xr-x. 18 root root 256 Mar 4 17:35 ..
只显示/etc下的非隐藏目录列表
[root@Rocky ~]
drwxr-xr-x. 2 root root 6 Mar 10 14:38 123abc
drwxr-xr-x. 3 root root 20 Mar 4 17:15 alsa
drwxr-xr-x. 2 root root 4096 Mar 4 17:17 alternatives
drwxr-xr-x. 4 root root 4096 Mar 4 17:15 asciidoc
drwxr-x---. 4 root root 100 Mar 4 17:14 audit
drwxr-xr-x. 3 root root 4096 Mar 4 17:18 authselect
drwxr-xr-x. 2 root root 163 Mar 4 17:15 bash_completion.d
每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求目录格式为backup_YYYY-mm-dd
[root@Rocky ~]
[root@Rocky ~]
[root@Rocky ~]
[root@Rocky ~]
[root@Rocky ~]
cp -av /etc/ /data/backup_`date +\%F`
cp -av /etc/ /data/etc_`date +%\F_%\H:%\M:%\S`
mkdir /data/rootdir/
cp -a /root/ /data/rootdir/
[root@Rocky ~]
[root@Rocky ~]
58 16 * * * /home/student/backup.sh
********************验证**************************
[root@Rocky ~]
drwxr-xr-x. 136 root root 8192 Mar 11 14:42 /etc/
[root@Rocky ~]
dr-xr-x---. 2 root root 158 Mar 11 16:58 /root/
[root@Rocky ~]
total 24
drwxr-xr-x. 136 root root 8192 Mar 11 14:42 backup_2023-03-11
drwxr-xr-x. 136 root root 8192 Mar 11 14:42 etc_2023-03-11_16:58:01
drwxr-xr-x. 3 root root 18 Mar 11 16:58 rootdir
[root@Rocky ~]
total 0
dr-xr-x---. 2 root root 158 Mar 11 16:57 root
创建/testdir/dir1/x, /testdir/dir1/y, /testdir/dir1/x/a, /testdir/dir1/x/b, /testdir/dir1/y/a, /testdir/dir1/y/b
[root@Rocky ~]
[root@Rocky ~]
/testdir/dir1/x
├── /testdir/dir1/x/a
└── /testdir/dir1/x/b
/testdir/dir1/y
├── /testdir/dir1/y/a
└── /testdir/dir1/y/b
创建/testdir/dir2/x, /testdir/dir2/y, /testdir/dir2/x/a, /testdir/dir2/x/b
[root@Rocky ~]
[root@Rocky ~]
/testdir/dir2
├── /testdir/dir2/x
│ ├── /testdir/dir2/x/a
│ └── /testdir/dir2/x/b
└── /testdir/dir2/y
├── /testdir/dir2/y/a
└── /testdir/dir2/y/b
创建/testdir/dir3, /testdir/dir4, /testdir/dir5, /testdir/dir5/dir6, /testdir/dir5/dir7
[root@Rocky ~]
[root@Rocky ~]
[root@Rocky ~]
/testdir/dir3
/testdir/dir4
/testdir/dir5
├── /testdir/dir5/dir6
└── /testdir/dir5/dir7
案例
1、在/home/student/Documents/project_plans目录中创建season1_project_plan.odf和season2_project_plan.odf 空文件,若~Documents/project_plans目录不存在则创建
[student@rocky ~]$ mkdir -p Documents/project_plans
[student@rocky ~]$ touch /home/student/Documents/project_plans/season{1,2}_project_plan.odf
[student@rocky ~]$ ls -lR Documents/
Documents/:
total 0
drwxr-xr-x. 2 student student 70 Mar 27 12:55 project_plans
Documents/project_plans:
total 0
-rw-r--r--. 1 student student 0 Mar 27 12:55 season1_project_plan.odf
-rw-r--r--. 1 student student 0 Mar 27 12:55 season2_project_plan.odf
2、创建名为tv_seasonX_episodeY.ogg的文件,一共12个。将X替换为季数,Y替换为该季中的集数,共两季,每季六集
[student@rocky ~]$ touch tv_season{1,2}_episonde{1..6}.ogg
[student@rocky ~]$ ls -ll tv*
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde1.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde2.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde3.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde4.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde5.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde6.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde1.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde2.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde3.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde4.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde5.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde6.ogg
3、创建名为mystery_chapterX.odf的文件,一共8个,将X替换为数字1到8
[student@rocky ~]$ touch mystery_chapter{1..8}.odf
[student@rocky ~]$ ls -ll mystery*
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter1.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter2.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter3.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter4.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter5.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter6.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter7.odf
-rw-r--r--. 1 student student 0 Mar 27 13:16 mystery_chapter8.odf
4、使用一个命令在Videos目录下创建两个子目录,名称为season1和season2
[student@rocky ~]$ mkdir -p Vidoes/season{1,2}
[student@rocky ~]$ ls Vidoes/
season1 season2
5、将tv_season1移动到Vidoes/season1目录中,将tv_season2移动到Vidoes/season2目录中
[student@rocky ~]$ mv tv_season1_episonde* Vidoes/season1/
[student@rocky ~]$ mv tv_season2_episonde* Vidoes/season2/
[student@rocky ~]$ ls -lR Vidoes/
Vidoes/:
total 8
drwxr-xr-x. 2 student student 4096 Mar 27 14:12 season1
drwxr-xr-x. 2 student student 4096 Mar 27 14:12 season2
Vidoes/season1:
total 0
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde1.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde2.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde3.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde4.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde5.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season1_episonde6.ogg
Vidoes/season2:
total 0
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde1.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde2.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde3.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde4.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde5.ogg
-rw-r--r--. 1 student student 0 Mar 27 13:13 tv_season2_episonde6.ogg
6、在Documents目录下创建my_bestseller,并在新的my_bestseller目录下创建chapters
[student@rocky ~]$ mkdir -p Documents/my_bestseller/chapters
[student@rocky ~]$ ls -lR Documents/
Documents/:
total 0
drwxr-xr-x. 3 student student 22 Mar 27 13:28 my_bestseller
7、在my_bestseller目录下再创建3个子目录。将这些子目录命名为editor、changes和vacation,不需要使用-p选项,因为父级已经存在
[student@rocky ~]$ mkdir Documents/my_bestseller/{editor,changes,vacation}
[student@rocky ~]$ ls Documents/my_bestseller/{editor,changes,vacation}
Documents/my_bestseller/changes:
Documents/my_bestseller/editor:
Documents/my_bestseller/vacation:
8、更改chapters目录,使用波浪符快捷键指定源文件,将所有mystery_chapterX.odf移动到chapters目录中,即现在的当前目录,指定目标目录的最简单语法
[student@rocky ~]$ cd Documents/my_bestseller/chapters/
[student@rocky chapters]$ mv ~/mystery_chapter* .
[student@rocky chapters]$ ls
mystery_chapter1.odf mystery_chapter3.odf mystery_chapter5.odf mystery_chapter7.odf
mystery_chapter2.odf mystery_chapter4.odf mystery_chapter6.odf mystery_chapter8.odf
9、将前两章节移动到editor目录中,要求从章节子目录中使用大括号移动
[student@rocky chapters]$ mv mystery_chapter{1,2}.odf ~/Documents/my_bestseller/editor/
[student@rocky chapters]$ ls ~/Documents/my_bestseller/editor/
mystery_chapter1.odf mystery_chapter2.odf
10、使用一个命令将mystery_chapter7,mystery_chapter8章节从chapters目录移动到vacation目录
[student@rocky chapters]$ mv mystery_chapter{7,8}.odf ~/Documents/my_bestseller/vacation/
[student@rocky chapters]$ ls ~/Documents/my_bestseller/vacation/
mystery_chapter7.odf mystery_chapter8.odf
11、将工作目录更改为~/videos/season2,然后将tv_season2_episonde1.ogg复制到vacation目录
[student@rocky vacation]$ cd ~/Vidoes/season2/
[student@rocky season2]$ ls
tv_season2_episonde1.ogg tv_season2_episonde3.ogg tv_season2_episonde5.ogg
tv_season2_episonde2.ogg tv_season2_episonde4.ogg tv_season2_episonde6.ogg
[student@rocky season2]$ mv tv_season2_episonde1.ogg ~/Documents/my_bestseller/vacation/
13、使用一个cd命令,从工作目录更改为~/Document/my_bestseller/vacation目录,列出其文件,使用cd - 命令,返回到season2目录(如果上次执行命令是用一个cd来完成,这才会成功),从season2目录,将tv_season2_episonde2.ogg文件复制到vacation目录,再次使用快捷键返回到vacation目录
[student@rocky season2]$ cd ~/Documents/my_bestseller/vacation/
[student@rocky vacation]$ ls
mystery_chapter7.odf mystery_chapter8.odf tv_season2_episonde1.ogg
[student@rocky vacation]$ cd -
/home/student/Vidoes/season2
[student@rocky season2]$ cp tv_season2_episonde2.ogg ~/Documents/my_bestseller/vacation/
[student@rocky season2]$ cd -
/home/student/Documents/my_bestseller/vacation
[student@rocky vacation]$ ls
mystery_chapter7.odf mystery_chapter8.odf tv_season2_episonde1.ogg tv_season2_episonde2.ogg
14、将当前工作目录更改为changes目录,通过date +%F命令将mystery_chapter5.odf复制到包含完整日期的新文件。名称应该采用mystery_chapter5_YYYY-MM-DD.odf形式,并制作mystery_chapter5.odf的另一个副本,并且附上当前的时间戳,确保文件名独一无二,使用命令date +%s来实现这一目标
[student@rocky changes]$ cp ../chapters/mystery_chapter5.odf ./mystery_chapter5_`date +%F`.odf
[student@rocky changes]$ cp mystery_chapter5_2023-03-27.odf mystery_chapter5_`date +%s`.odf
[student@rocky changes]$ ls
mystery_chapter5_1679899884.odf mystery_chapter5_2023-03-27.odf
15、创建/Documents/project_plans/season2_project_plan.odf文件的硬链接,取名为/Documents/project_plans/season2_project_plan.odf.back。硬链接将防止意外删除原始文件,并在更改原始文件时使备份文件保持更新
[student@rocky ~]$ ln ~/Documents/project_plans/season2_project_plan.odf ~/Documents/backups/season2_project_plan.odf.back
[student@rocky ~]$ ls -lR ~/Documents/
/home/student/Documents/:
/home/student/Documents/backups:
total 0
-rw-r--r--. 2 student student 0 Mar 27 12:55 season2_project_plan.odf.back