linux-通配符与特殊符号

通配符

通配符简介

通配符就是使用键盘上的一些特殊符号,以实现某些特殊,方便的功能,下面是一些简单常见的示例

批量创建多个文件
[root@node1 ~]# touch hello{1..5}.txt
[root@node1 ~]# ls hello*
hello1.txt  hello2.txt  hello3.txt  hello4.txt  hello5.txt  hello.txt

备份文件
[root@node1 ~]# cp hello.txt{,.bak}
[root@node1 ~]# ls hello.txt hello.txt.bak 
hello.txt  hello.txt.bak

常用的通配符

*  (星号) 匹配任意字符串,0个或多个
?   (问号) 匹配任意1个字符

[abcd] 匹配abcd中任意一个字符,abcd无需连续
[a-z] 或 [0-9] 匹配连续的字符或数字中的任意1个字符
[^a-z] 或 [^0-9] 上面情况的反面,不匹配这里其中任何1个字符

通配符常见使用场景举例

只显示 .conf 结尾的文件
[root@node1 etc]# ls /etc/*.conf
/etc/asound.conf  /etc/GeoIP.conf   /etc/krb5.conf      /etc/locale.conf     /etc/nfs.conf          /etc/resolv.conf    /etc/sos.conf        /etc/tcsd.conf
...


删除 .txt 结尾的文件
[root@node1 ~]# ls *.txt
dp.txt  hello1.txt  hello2.txt  hello3.txt  hello4.txt  hello5.txt  hello.txt  md5test.txt  ping.txt  tmp.txt
[root@node1 ~]# rm -f *.txt
[root@node1 ~]# ls *.txt
ls: cannot access *.txt: No such file or directory


查找 /etc/ 下所有文件名中包含 host 的文件
[root@node1 ~]# find /etc/ -type f -name "*host*"
/etc/ssh/ssh_host_rsa_key
/etc/ssh/ssh_host_rsa_key.pub
...


只看某几天的日志,比如查看25号~27号的日志
[root@node1 ~]# ls /var/log/messages*
/var/log/messages           /var/log/messages-20200120  /var/log/messages-20200224  /var/log/messages-20200226  /var/log/messages-20200228
/var/log/messages-20191231  /var/log/messages-20200131  /var/log/messages-20200225  /var/log/messages-20200227  /var/log/messages-20200229
[root@node1 ~]# ls /var/log/messages-2020022[567]
/var/log/messages-20200225  /var/log/messages-20200226  /var/log/messages-20200227

通配符和正则表达式的区别

通配符和正则表达是不一样的,通配符的使用范围非常广,大部分shell命令都可以用,而正则表达式只有grep、sed、awk等这些支持正则表达式的才能使用

正则表达式通常作用于文本内容,通配符通常用来匹配文件名

特殊符号

与路径位置相关的特殊符号

~ 代表用户家目录
- 返回上一次所在的目录
. 代表当前目录
.. 代表上一级目录

常见使用场景

根据本人在实际工作中的经验,我简单说一下这些表示位置的特殊符号常见的使用场景

场景1:有些特殊的文件,需要存放在用户的家目录下,而家目录的具体路径取决于当前登录的用户,此时我们直接使用类似于
echo "xxx" >> ~/xmy.conf
这样就可以不用关注当前登录的用户名,以及用户的家目录在哪里了, ~ 帮助我们搞定这些

场景2: 快速回到家目录
cd  OR cd ~

场景3:当前的操作需要经常在固定的2个目录切换,这两个目录的路径还挺长的
cd -

场景4:以当前目录为主要目录,妥善利用 ./ ../ 切换 避免混乱
[root@node1 hadoop-2.7.7]# ls
bin  etc  include  lib  libexec  LICENSE.txt  logs  NOTICE.txt  README.txt  sbin  share

单引号与双引号

在书写脚本的时候,要注意单引号和双引号的区别,最简单的区别就是单引号原样输出,而双引号会解析变量

[root@node1 ~]# num=20
[root@node1 ~]# echo $num 
20
[root@node1 ~]# echo "num=$num"
num=20
[root@node1 ~]# echo 'num=$num'
num=$num
[root@node1 ~]# echo "num=\$num"
num=$num
[root@node1 ~]# num1=20; num2=30
[root@node1 ~]# echo '"$num1" "$num2"'
"$num1" "$num2"
[root@node1 ~]# echo "'$num1' '$num2'"
'20' '30'

shell脚本的单引号,双引号,以及它们之间的嵌套有时候不是那么容易看出来,建议多实用bash -x xx.sh,调试脚本,逐步达到满意的效果

反引号

`` 反引号的功能和$()一样,把要执行的命令嵌套其中

[root@node1 ~]# echo date
date
[root@node1 ~]# echo `date`
Mon Mar 2 22:36:49 CST 2020
[root@node1 ~]# echo $(date)
Mon Mar 2 22:37:07 CST 2020

重定向符号

标准输入   <
标准输出   > >>
标准错误输出 2>

这类特殊符号有很多种写法,网上也有很多,我这里只说下我在工作中常用的

清空某个文件
> file.txt

echo "ss" > file.txt
echo "sss" >> file.txt


控制某个可能出错的命令,让出错内容去它该去的地方
消失
cat file.txt 2> /dev/null
去某个文本里
cat file.txt 2> /tmp/file.txt
cat file.txt 2>> /tmp/file.txt

不管命令的对错,输出的内容全部到指定的地方
cat file.txt &> /dev/null
cat file.txt &> /tmp/file.txt
cat file.txt &>> /tmp/file.txt

其它重要的特殊符号

; 分号,表示一个命令的结束,正常shell执行命令,直接敲回车执行,但是有时我们想让多个命令在一行执行,就需要这个

&& 并且,多个命令在一行执行时,前一个命令成功在执行后面的命令

# 注释符号,root用户的命令提示符

| 管道符号

$ 变量引用;普通用户的命令提示符

\ 撬棍,反斜线  让特殊符号失效,打印特殊符号本身

{} 生成序列,用作变量引用


使用场景举例
cd /etc; ls
# do not run this line   echo hello

ifconfig | grep 10  管道的执行效率不高,尽量避免在脚本中大规模,多层嵌套使用

[root@node1 ~]# echo {1..10}
1 2 3 4 5 6 7 8 9 10

当路径非常长,这种备份方法,敲命令确实快些,缺点就是对于Linux新手来说,不容易懂
[root@node1 ~]# cp /as4k/hadoop-2.7.7/etc/hadoop/core-site.xml{,.bak} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凯尔kyle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值