关于目录及文件管理和简单正则表达式的两个实例

关于目录及文件管理和简单正则表达式的两个实例

1.用命令和正则表达式按照要求建立文件
*)用一条命令建立12个文件WESTOS_classX_linuxY(X的数值范围为1-2,Y的数值范围为1-6)
*)这些文件都包含在root用户桌面的study目录中
*)用一条命令建立8个文件redhat_versionX(x的范围为1-8)
*)redhat_virsionX这些文件都包含在/mnt目录中的VERSION中

2.管理刚才信建立的文件要求如下
*)用一条命令把redhat_versionX中的带有奇数的文件复制到桌面的SINGLE中
*)用一条命令把redhat_versionX中的带偶数数的文件复制到/DOUBLE中
*)用一条命令把WESTOS_classX_linuxY中class1的文件一动到当前用户桌面的CLASS1中
*)用一条命令把WESTOS_classX_linuxY中class2的文件一动到当前用户桌面的CLASS2中
3.备份/etc目录中所有带有名字带有数字并且以.conf结尾的文件到桌面上的confdir中
4.删掉刚才建立或者备份的所有文件

命令如下:
[root@localhost Desktop]# mkdir /root/Desktop/study **创建study目录
[root@localhost Desktop]# cd /root/Desktop/study **切换到/root/Desktop/study目录下
[root@localhost study]# pwd
/root/Desktop/study
[root@localhost study]# touch WESTOS_class{1..2}_linux{1..6} **创建文件
[root@localhost study]# ls
WESTOS_class1_linux1 WESTOS_class1_linux5 WESTOS_class2_linux3
WESTOS_class1_linux2 WESTOS_class1_linux6 WESTOS_class2_linux4
WESTOS_class1_linux3 WESTOS_class2_linux1 WESTOS_class2_linux5
WESTOS_class1_linux4 WESTOS_class2_linux2 WESTOS_class2_linux6
[root@localhost study]# mkdir /mnt/VERSION **创建目录
[root@localhost study]# cd /mnt/VERSION **创建目录
[root@localhost VERSION]# touch redhat_version{1..8} **创建文件
[root@localhost VERSION]# ls
redhat_version1 redhat_version3 redhat_version5 redhat_version7
redhat_version2 redhat_version4 redhat_version6 redhat_version8
[root@localhost VERSION]# cd /root/Desktop/
[root@localhost Desktop]# mkdir SINGLE CLASS1 CLASS2
[root@localhost Desktop]# mkdir /DOUBLE
[root@localhost DOUBLE]# cd /mnt/VERSION
[root@localhost VERSION]# cp redhat_version[1357] /root/Desktop/SINGLE **根据正则表达式筛选符合条件的文件并复制到指定的目录/root/Desktop/SINGLE下
[root@localhost VERSION]# cp redhat_version[2468] /DOUBLE **根据正则表达式筛选符合条件的文件并复制到指定的目录/DOUBLE下
[root@localhost VERSION]# ls /root/Desktop/SINGLE
redhat_version1 redhat_version3 redhat_version5 redhat_version7
[root@localhost VERSION]# ls /DOUBLE
redhat_version2 redhat_version4 redhat_version6 redhat_version8
[root@localhost VERSION]# cd /root/Desktop/study
[root@localhost study]# mv WESTOS_class1_linux{1..6} /root/Desktop/CLASS1 **根据正则表达式将符合条件的文件移动到指定的目录/root/Desktop/CLASS1下
[root@localhost study]# mv WESTOS_class2_linux{1..6} /root/Desktop/CLASS2 **根据正则表达式将符合条件的文件移动到指定的目录/root/Desktop/CLASS2下
[root@localhost study]# ls /root/Desktop/CLASS1 ** 查看/root/Desktop/CLASS1目录下的文件
WESTOS_class1_linux1 WESTOS_class1_linux3 WESTOS_class1_linux5
WESTOS_class1_linux2 WESTOS_class1_linux4 WESTOS_class1_linux6
[root@localhost study]# ls /root/Desktop/CLASS2 **查看/root/Desktop/CLASS2目录下的文件
WESTOS_class2_linux1 WESTOS_class2_linux3 WESTOS_class2_linux5
WESTOS_class2_linux2 WESTOS_class2_linux4 WESTOS_class2_linux6
[root@localhost study]# cd /etc
[root@localhost etc]# mkdir /root/Desktop/confdir
[root@localhost etc]# cp [[:digit:]].conf /root/Desktop/confdir 根据正则表达式将符合条件的文件备份到指定目录/root/Desktop/confdir下
[root@localhost etc]# ls /root/Desktop/confdir **查看备份结果
e2fsck.conf krb5.conf mke2fs.conf pbm2ppa.conf pnm2ppa.conf
[root@localhost etc]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /root/Desktop/
[root@localhost Desktop]# rm -fr * study CLASS1 CLASS2 SINGLE confdir /mnt/VERSION /DOUBLE **将所有建立以及复制、移动、备份的目录和文件彻底删除

小结:
通配符(正则表达式)
* **匹配0-任意字符
? **匹配任意单个字符
~ **匹配当前用户家目录
~user **匹配到user的家目录
~+ **当前目录
~- **当前目录之前所在的
[abc] **有a或者有b或者有c
[!abc] **除了含有a或者b或者c
[^abc] **除了含有a或者b或者c
[[:alpha:]] **单个字母
[[:lower:]] **单个小写字母
[[:upper:]] **单个大写字母
[[:alnum:]] **单个字母或数字
[[:punct:]] **单个符号,不包含数字,字母以及空格
[[:digit:]] **数字
[[:space:]] **单个空格

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值