day15 通配符与shell特殊符号

通配符

第1章 通配符介绍

1.意义:

将人类的语言转换成计算机能识别的语言

2.作用:

用来查找文件名的

3.常用符号说明

-----------------------

* 匹配任意(0个或多个)字符或字符串,包含空字符串

? 匹配任意1个字符,有且只有一个字符

[abcd] 匹配[abcd]中任何一个字符,abcd也可是其他任意不连续字符

[a-z] 匹配a-z之间的任意一个字符

[0-9] 匹配0-9之间的任意一个数字

[a-Z] 匹配a-zA-Z之间的任意一个数字

[!abcd] 不匹配括号里面的任意字符,也可以写作[!a-d],这里的!可以用替代,即[abcd]

----------------------

第2章 实验通配符

1.创建测试环境

mkdir test

cd test/

touch oldboy.txt oldgirl.txt test.txt oldzhang.sh xintongxue.jpg laotongxue.jpgp old526195417boy.com old7boy.txt

ls

2.* 匹配任意个字符

#查找以txt结尾的文件

[root@centos test]# ll *.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldboy.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldgirl.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 test.txt

img

#查找以old开头.txt结尾

[root@centos test]# ll old*.txt

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

-rw-r--r--. 1 root root 4 10月 12 10:01 oldgirl.txt

img

3.? 匹配任意一个字符(用的不多,了解即可)

#查找以old开头,中间有3个字符,以txt结尾的文件

[root@centos test]# ll old???.txt

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

img

#查找/etc/下host后有一个1字符的文件

[root@centos ~]# ll /etc/host?

-rw-r--r--. 1 root root 158 6月 7 2013 /etc/hosts

img

4.[] 匹配指定的内容

[root@centos test]# ll old[abc]*.txt

img

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

[root@centos test]# ll old[a-z]*

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

-rw-r--r--. 1 root root 4 10月 12 10:01 oldgirl.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldzhang.sh

img

[root@centos test]# ll old[0-9]*

-rw-r--r--. 1 root root 0 10月 12 10:27 old526195417boy.com

[root@centos test]# ll old[0-9][a-z]*

-rw-r--r--. 1 root root 0 10月 12 10:30 old7boy.txt

img

5.[!abcd] 取反匹配指定的内容

#以old来头后面的一个字符不包含abc之一

[root@centos test]# ll old[!abc]*

-rw-r--r--. 1 root root 0 10月 12 10:27 old526195417boy.com

-rw-r--r--. 1 root root 0 10月 12 10:30 old7boy.txt

-rw-r--r--. 1 root root 0 10月 12 10:33 old8BoY.txt

-rw-r--r--. 1 root root 4 10月 12 10:01 oldgirl.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldzhang.sh

img

#排除掉以o开头的文件

[root@centos test]# ll [!o]*

-rw-r--r--. 1 root root 0 10月 12 09:55 laotongxue.jpgp

-rw-r--r--. 1 root root 4 10月 12 10:01 test.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 xintongxue.jpg

img

#排除掉文件名以e结尾的文件

[root@centos test]# ll [!e].

-rw-r--r--. 1 root root 0 10月 12 10:27 old526195417boy.com

-rw-r--r--. 1 root root 0 10月 12 10:30 old7boy.txt

-rw-r--r--. 1 root root 0 10月 12 10:33 old8BoY.txt

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

-rw-r--r--. 1 root root 4 10月 12 10:01 oldgirl.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldzhang.sh

-rw-r--r--. 1 root root 4 10月 12 10:01 test.txt

img

#排除掉old开头后面一个字符是数字的文件

[root@centos test]# ll old[!0-9]*

-rw-r--r--. 1 root root 4 10月 12 10:00 oldboy.txt

-rw-r--r--. 1 root root 4 10月 12 10:01 oldgirl.txt

-rw-r--r--. 1 root root 0 10月 12 09:55 oldzhang.sh

img

6.实战练习

#1.查找/etc目录下包含hosts字符串的所有文件

[root@centos ~]# ll /etc/hosts

-rw-r--r--. 1 root root 158 6月 7 2013 /etc/hosts

-rw-r--r--. 1 root root 370 6月 7 2013 /etc/hosts.allow

-rw-r--r--. 1 root root 460 6月 7 2013 /etc/hosts.deny

img

#2.查找/etc目录下所有网卡配置文件

[root@centos ~]# find /etc -type f -name "ifcfg-*"

/etc/sysconfig/network-scripts/ifcfg-lo

/etc/sysconfig/network-scripts/ifcfg-eth0

img

#3.只查找包含了数字的网卡配置

[root@centos ~]# find /etc -type f -name "ifcfg-[0-9]"

/etc/sysconfig/network-scripts/ifcfg-eth0

img

#4.查找/dev/sda一共有几个分区

[root@centos ~]# ll /dev/sda[0-9]

brw-rw----. 1 root disk 8, 1 10月 12 09:45 /dev/sda1

brw-rw----. 1 root disk 8, 2 10月 12 09:45 /dev/sda2

img

第3章 shell命令行特殊符号

1.什么是shell命令行特殊符号

相比通配符来说,Linux特殊符号更加复杂,且杂乱无章

但是,要想成为一个合格的Linux运维工程师,就必须要掌握这些特殊符号。

2.路径相关
用户家目录

- 上一次目录

. 当前目录

.. 上一层目录 ../../

3.优先执行命令

命令 引用命令,当其他命令和里的命令一起执行时,先执行里的命令

$(命令) 和的作用一模一样,但是比更容易分辨

#小实验,

将/var/log/messages日志按日期打包到/opt目录下

tar zcvf /opt/data +%F-messages.tar.gz /var/log/messages

tar zcvf /opt/$(data +%F)-messages.tar.gz /var/log/messages

img

使用find但是不用xargs和-exec删除文件

rm -rf $(find . -type f -name "*.txt")

img

根据文本里的文件名删除文件

rm -rf $(cat del.txt)

img

4.{} 生成序列

touch 192.168.4.{1..10}

touch oldboy-{a..z}

img

5.; 分隔多个命令

需求:

#1.进入/opt目录

cd /opt/

#2.创建一个目录/opt/dir1/

mkdir dir1

#4.echo 我在:当前路径

echo 我在:$(pwd)

实现:

cd /opt/;mkdir dir1;echo 我在:$(pwd)

6.&& 前面命令执行成功才会执行后面的命令

a.先删除/opt目录下的文件

img

b.ehco 我在:$(cd /opt/ ;mkdir /opt/dir1 -p;pwd)

img

使用方法:

命令1 && 命令2 && 命令3

解释:

命令1执行成功了,才会执行命令2

命令1和命令2都执行成功了,才会执行命令3

需求:

1.进入/opt/dir1目录

cd /opt/dir1

2.如果/opt/dir1目录确实存在,则在dir1目录里创建一个文件file1.txt并且写入hello

echo hello > file1.txt

3.如果file1.txt确实全在,则把他的内容打印到屏幕上

cat file1.txt

实现:

[root@centos ~]# mkdir -p /opt/dir1 && cd /opt/dir1 && echo hello > file1.txt && cat file1.txt

hello

img

7.|| 前面命令执行不成功才会执行后面的命令

使用方法:

命令1 || 命令2

解释:

命令1执行失败了,才会执行命令2

实现:

[root@centos ~]# cd /opt/dir1 || mkdir -p /opt/dir1 && echo hello > file1.txt && cat file1.txt

-bash: cd: /opt/dir1: 没有那个文件或目录

hello

img

posted @ 2021-10-12 19:44  zhaocheng690  阅读(53)  评论(0编辑  收藏  举报