文件目录操作

文章目录

小试牛刀

通配符说明
*匹配零个或多个字符,但不匹配“.”开头的文件,即隐藏文件
匹配任何单个字符,一个汉字也算一个字符
~当前用户目录
~用户名当前用户用户名目录
[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 ~]# ls -d /etc/i*[0-9]*[a-z]?
/etc/i123dft.txt  /etc/i1txt  /etc/i4546f.txt  /etc/i987tst  /etc/isdf234ts

显示/etc/目录下以任意一位数字开头,且以非数字结尾的文件或目录列表

[root@Rocky ~]# ls -d /etc/[0-9]*[^0-9]*
/etc/123abc  /etc/456abc.txt

显示/etc/目录下以非字母开头,后面跟了一个字母及其它任意长度任意字符的文件或目录列表

[root@Rocky ~]# ls -d /etc/[^a-z^A-Z]*
/etc/123abc  /etc/456abc.txt

显示/etc/目录下所有以rc开头,并且后面是0-6之间的数字,其它为任意字符的文件或目录列表

[root@Rocky ~]# ls -d /etc/rc[0-6]*
/etc/rc123dsfir  /etc/rc15.txt  /etc/rc235abc  /etc/rc36sfe.tst

显示/etc/目录下所有.conf结尾且以m,n,r,p开头的文件或目录列表

[root@Rocky ~]# ls -d /etc/[mnrp]*.conf
/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 ~]# ls -al /root/ | grep "^d"
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 ~]# ls -l /etc/ | grep "^d"
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 ~]# touch /home/student/backup.sh
[root@Rocky ~]# chmod +x /home/student/backup.sh 
[root@Rocky ~]# vim /home/student/backup.sh 
[root@Rocky ~]# ll -d /home/student/backup.sh
[root@Rocky ~]# cat /home/student/backup.sh 
#!/bin/bash
#
# 每天将/etc/目录下所有文件,备份到/data独立的子目录下,并要求子目录格式为backup_YYYY-mm-dd
# 创建/data/rootdir目录,并复制/root下所有文件到该目录内,要求保留原有权限

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 ~]# crontab -e 
[root@Rocky ~]# crontab -l
58    16    *    * *    /home/student/backup.sh 

********************验证**************************
[root@Rocky ~]# ll -ld /etc/
drwxr-xr-x. 136 root root 8192 Mar 11 14:42 /etc/
[root@Rocky ~]# ll -ld /root/
dr-xr-x---. 2 root root 158 Mar 11 16:58 /root/ 

[root@Rocky ~]# ls -ll /data/
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 ~]# ls -ll /data/rootdir/
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 ~]# mkdir -p /testdir/dir1/{x,y}/{a,b}

[root@Rocky ~]# tree -f /testdir/dir1/{x,y}
/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 ~]# mkdir -p /testdir/dir2/{x,y}/{a,b}

[root@Rocky ~]# tree -f /testdir/dir2/
/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 ~]# mkdir -p /testdir/dir{3..5}
[root@Rocky ~]# mkdir -p /testdir/dir5/dir{6,7} 

[root@Rocky ~]# tree -f /testdir/dir{3..5}
/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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值