linux通配符 &,Linux通配符知识入门详解

本文详细介绍了Linux中通配符(*, ?, #, --, ~, -, $, >, >>, <, xargs, tr)和正则表达式的区别与用法,涵盖命令行操作、文件名匹配、环境变量替换和复杂逻辑组合实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:通配符和正则表达式是不一样的,因此代表的意义也是有较大区别的。

通配符一般用户命令行bash环境,而Linux正则表达式用于grep,sed,awk场景。

* -- 通配符,代表任意(0到多个)字符*****

? -- 通配符,代表任意1个字符

; -- 连接不同命令的分隔符*****

# -- 配置文件注释*****

| -- 管道*****

~ -- 用户的家目录*****

- -- 上一次所在的目录*****

$ -- 变量前需要加的符号

/ -- 路径分隔符号,也是根的意思

>或1> -- 重定向,覆盖*****

>> -- 追加重写向,追加内容文件尾部 *****

< -- 输入重定向*****(xargs,tr)

<< -- 追加输入重定向

'' -- 单引号,不具有变量置换功能,输出时所见即所得 *****

""-- 双引号,具有变量置换功能,解析变量后输出,什么都不加一般跟加了""差不多(如果是命令需要用`命令`或者$(命令))*****

`` -- tab键上面的键,反引号,两个``中间的为命令,会先执行等价$( ) *****

{} -- 中间为命令区块组合或者内容序列

! -- 逻辑运算中的“非”(not)

&& -- and 并且 当前一个指令执行成功时,执行后一个指令

|| -- or 或者 当前一个指令执行失败时,执行后一个指令

1、常用例子:

[root@centos6 test]# touch {a,b,abc}.sh

[root@centos6 test]# ll

total 0

-rw-r--r-- 1 root root 0 Dec 31 11:07 abc.sh

-rw-r--r-- 1 root root 0 Dec 31 11:07 a.sh

-rw-r--r-- 1 root root 0 Dec 31 11:07 b.sh

[root@centos6 test]# ls *.sh

abc.sh a.sh b.sh

[root@centos6 test]# ls ?.sh

a.sh b.sh

[root@centos6 test]# ls ???.sh

abc.sh

[root@centos6 test]# pwd;ls

/root/test

abc.sh a.sh b.sh

[root@centos6 test]# ls -l|grep a

total 0

-rw-r--r-- 1 root root 0 Dec 31 11:07 abc.sh

-rw-r--r-- 1 root root 0 Dec 31 11:07 a.sh

[root@centos6 test]# echo $LANG

en_US.UTF-8

[root@centos6 test]# echo 'date'

date

[root@centos6 test]# echo "date"

date

[root@centos6 test]# echo "`date`"

Sun Dec 31 11:42:08 CST 2017

[root@centos6 test]# echo "$(date)"

Sun Dec 31 11:42:25 CST 2017

[root@centos6 test]# echo date

date

[root@centos6 test]# echo `date`

Sun Dec 31 11:50:21 CST 2017

2、详细说明''单引号和""双引号的区别:

[root@centos6 test]# echo oldboy >a.txt

[root@centos6 test]# grep oldboy a.txt

oldboy

[root@centos6 test]# a=oldboy

[root@centos6 test]# grep "$a" a.txt

oldboy

[root@centos6 test]# grep '$a' a.txt

[root@centos6 test]# echo $a >>a.txt

[root@centos6 test]# cat a.txt

oldboy

oldboy

[root@centos6 test]# echo '$a' >>a.txt

[root@centos6 test]# cat a.txt

oldboy

oldboy

$a

3、{}内容序列举例,常见用法举例

[root@centos6 test]# echo file{1,2,3}

file1 file2 file3

[root@centos6 test]# echo file{1..3}

file1 file2 file3

[root@centos6 test]# echo file_{a..e}

file_a file_b file_c file_d file_e

备注:seq只能是数字的序列,这里可以是字母序列

[root@centos6 test]# cp a.sh{,.bak}

[root@centos6 test]# ll

total 0

-rw-r--r-- 1 root root 0 Dec 31 11:07 a.sh

-rw-r--r-- 1 root root 0 Dec 31 12:23 a.sh.bak

备注:备份的特殊用法

[root@centos6 /]# mkdir /data/{3306,3307}/data -p

[root@centos6 /]# tree /data

/data

├── 3306

│ └── data

└── 3307

└── data

4 directories, 0 files

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值