Day14课堂笔记
1. Linux通配符知识
通配符简单说就是键盘上的一些特殊字符,可以实现某些特殊的功能。如下表所示:
符号 | 作用 |
---|---|
* | 匹配任意(0个或多个)字符或字符串,包括空字符串 |
? | 匹配任意1个字符,有且只有一个字符 |
[abcd] | 匹配ABC的中任意一个字符,abcd可是其他任意字符 |
[a-z] | 匹配a到z之间任意一个字符,字符前后连续,也可[0-9] |
[!abcd] | 表示不匹配括号里面的任何一个字符,这里的!可用^t替代 |
以下是操作示例数据环境准备:
[root@oldboyedu ~]# mkdir test
[root@oldboyedu ~]# cd test/
[root@oldboyedu ~/test]# touch oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# touch inca.sh
[root@oldboyedu ~/test]# ls
inca.sh oldboy.txt oldgirl.txt test.txt
1、 通配符之 * 号的实践
[root@oldboyedu ~/test]# ls *.txt
oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# ls *.sh
inca.sh
2、 通配符之 ? 号的实践
[root@oldboyedu ~/test]# ls
inca.sh oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# ls ?.sh
ls: cannot access ?.sh: No such file or directory
[root@oldboyedu ~/test]# touch a.sh
[root@oldboyedu ~/test]# ls ?.sh
a.sh
[root@oldboyedu ~/test]# ls ????.sh
inca.sh
3、 通配符之[abcd]的实践
[root@oldboyedu ~/test]# ls
a.sh inca.sh oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# ls [abcd].sh
a.sh
[root@oldboyedu ~/test]# ls in[abc]a.sh
inca.sh
4、 通配符之[a-z]的实践
[root@oldboyedu ~/test]# touch c.sh d.sh cd.sh
[root@oldboyedu ~/test]# ls
a.sh cd.sh c.sh d.sh inca.sh oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# ls [a-z].sh
a.sh c.sh d.sh
[root@oldboyedu ~/test]# ls [a-z]???.sh
inca.sh
[root@oldboyedu ~/test]# touch 1 2
[root@oldboyedu ~/test]# ls [0-9]
1 2
5、 通配符之[!abcd]的实践
[root@oldboyedu ~/test]# touch {a..f}
[root@oldboyedu ~/test]# ls [^abcd]
1 2 e f
[root@oldboyedu ~/test]# ls
1 2 a a.sh b c cd.sh c.sh d d.sh e f inca.sh oldboy.txt oldgirl.txt test.txt
[root@oldboyedu ~/test]# ls [^a-d]
1 2 e f
[root@oldboyedu ~/test]# ls [^0-9]
a b c d e f
6、 通配符之综合的实践
[root@oldboyedu ~/test]# find /etc/ -type f -name "hosts*"
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
[root@oldboyedu ~/test]# find /etc/ -type f -name "?[opq][^a-r]ts"
/etc/hosts
2. Linux特殊符知识
2.1 与路径和位置有关的特殊符号
符号 | 作用 |
---|---|
~ | 用户的家目录,超级用户为/root,普通用户为/home |
- | 代表上一次(相当于当前路径)用户所在的路径 |
. | 代表当前目录 |
.. | 代表上一级目录 |
1、 波浪号(~)的实践
[root@oldboyedu ~/test]# pwd
/root/test
[root@oldboyedu ~/test]# cd ~
[root@oldboyedu ~]# pwd
/root
2、 短横杠(-)的实践
[root@oldboyedu ~/test]# cd ~
[root@oldboyedu ~]# pwd
/root
[root@oldboyedu ~]# echo $OLDPWD
/root/test
[root@oldboyedu ~]# cd -
/root/test
[root@oldboyedu ~/test]# echo $OLDPWD
/root
[root@oldboyedu ~/test]# cd -
/root
3 、点号(.)的实践
[root@oldboyedu ~/test]# pwd
/root/test
[root@oldboyedu ~/test]# cd .
[root@oldboyedu ~/test]# pwd
/root/test
[root@oldboyedu ~/test]# find . -name "*.sh"
./inca.sh
./a.sh
./c.sh
./d.sh
./cd.sh
4 、双点号(..)的实践
[root@oldboyedu ~/test]# pwd
/root/test
[root@oldboyedu ~/test]# cd ..
[root@oldboyedu ~]# pwd
/root
[root@oldboyedu ~]# mkdir dirl
[root@oldboyedu ~]# ls -a dirl/
. ..
2.2 不同引号特殊符号知识与实践
名称 | 解释 |
---|---|
单引号(' ') | 所见即所得,将单引号内的内容都原样输出。 |
双引号(" ") | 先解析变量、命令、转义字符,然后再输出最终内容。 |
无引号 | 基本和双引号相时同,不过还是用双引号替代比较好。 |
反引号( ) |
一般用于引用命令,执行时命令会被执行,相当于$()。 |
1、 反引号(``)的实践
[root@oldboyedu ~]# date
Sat Mar 23 11:59:40 CST 2019
[root@oldboyedu ~]# echo date
date
[root@oldboyedu ~]# echo `date`
Sat Mar 23 12:00:00 CST 2019
[root@oldboyedu ~]# echo `aa`
-bash: aa: command not found
[root@oldboyedu ~]# cd test/
[root@oldboyedu ~/test]# touch `date +%F`.txt
[root@oldboyedu ~/test]# ls
2019-03-23.txt
[root@oldboyedu ~/test]# ls `date +%F`.txt
2019-03-23.txt
[root@oldboyedu ~/test]# ls -l `which cat`
-rwxr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat
2 、双引号("")的实践
[root@oldboyedu ~/test]# echo "date"
date
[root@oldboyedu ~/test]# echo "`date`"
Sat Mar 23 12:05:38 CST 2019
[root@oldboyedu ~/test]# echo "$(date) `which mkdir`"
Sat Mar 23 12:06:55 CST 2019 /usr/bin/mkdir
3、 单引号('')的实践
[root@oldboyedu ~/test]# echo 'date'
date
[root@oldboyedu ~/test]# echo '`date`'
`date`
4、无引号的实践
[root@oldboyedu ~/test]# echo "today is `date +%w`"
today is 6
[root@oldboyedu ~/test]# echo today is `date +%w`
today is 6
[root@oldboyedu ~/test]# echo I am oldboy
I am oldboy
[root@oldboyedu ~/test]# echo $(date) `which mkdir`
Sat Mar 23 12:13:40 CST 2019 /usr/bin/mkdir
2.3 重定向特殊符号知识与实践
名称 | 文件描述简介 |
---|---|
标准输入(stdin) | 代码为0,配合<或<<使用,数据流从右向左 |
标准输出(stdout) | 代码为1,配合>或>>使用,数据流从左向右 |
标准错误输出(stderr) | 代码为2,配合>或>>使用,数据流从左向右 |
重定向符号,数据流是箭头方向 | |
标准输入重定向 | 0<或<,清空已有内容,数据一般从文件流向处理的命令 |
追加输入重定向 | 0<<或<<,追加内容到底部,数据一般从文件流向处理的命令 |
标准输出重定向 | 1>或>,正常输出重定向到文件,会清空已有的内容 |
标准追加输出重定向 | 1>>或>>,将内容追加重定向到底部,不会清空已有的内容 |
标准错误输出重定向 | 2>,将标准错误内容重定向到文件,如果文件存在内容则清空 |
标准错误输出追加重定向 | 2>>,将标准错误内容追加到文件底部,不会清空已有的内容 |
此外还有一个特殊重定向用法:将标准错误重定向到标准输出,即标准错误和标准输出一样重定向到文件中,这个功能有3种实现命令的方法。
方法1:echo "I am oldboy" 1>>oldboy.txt 2>>oldboy.txt
方法2:echo "I am oldboy" &>>oldboy.txt
方法3:echo "I am oldboy" >>oldboy.txt 2>&1
2.4 其他特殊符号知识与实践
名称 | 解释 |
---|---|
; | 表示一个命令的结束,也是命令间的分隔符 |
# | 1、表示注释内容,2、root用户的命令提示符 |
│ | 表示管道,将一个命令处理后的中间内容输出给下一个命令继续处理 |
$ | 1、字符串前加$符号,表示变量内容,2、普通用户的命令提示符 |
\ | 将特殊含义的字符还原成字符本意 |
{} | 1、生产序列,2、引用变量与普通字符分隔 |
1、 分号(;)的实践
[root@oldboyedu ~/test]# pwd;
/root/test
[root@oldboyedu ~/test]# pwd;pwd
/root/test
/root/test
2 、井号(#)的实践
[root@oldboyedu ~/test]# tail /etc/ssh/sshd_config
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
3、 管道符(|)的实践
[root@oldboyedu ~/test]# ifconfig | grep 10.0.0
inet 10.0.0.201 netmask 255.255.255.0 broadcast 10.0.0.255
[root@oldboyedu ~/test]# cat /etc/services |grep 3306
mysql 3306/tcp # MySQL
mysql 3306/udp # MySQL
4、 美元符($)的实践
[root@oldboyedu ~/test]# echo $OLDPWD
/root
[root@oldboyedu ~/test]# echo $PS1
[\[\e[34;1m\]\u@\[\e[0m\]\[\e[32;1m\]\H\[\e[0m\] \[\e[31;1m\] \w\[\e[0m\]]\$
[root@oldboyedu ~/test]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@oldboyedu ~/test]# oldboy=1
[root@oldboyedu ~/test]# echo $oldboy
1
5、 大括号({})的实践
1){}
表示生成数列。
[root@oldboyedu ~/test]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu ~/test]# echo {a..g}
a b c d e f g
[root@oldboyedu ~/test]# echo {o,l,d,b,o,y}
o l d b o y
2)利用{}
功能来快速备份关键文件
[root@oldboyedu ~/test]# cp /etc/ssh/sshd_config{,..ori}
[root@oldboyedu ~/test]# ls -l /etc/ssh/sshd_config{,..ori}
-rw-------. 1 root root 3907 Apr 11 2018 /etc/ssh/sshd_config
-rw------- 1 root root 3907 Mar 23 12:42 /etc/ssh/sshd_config..ori
3)把变量括起来作为变量的分隔符
[root@oldboyedu ~/test]# week=6
[root@oldboyedu ~/test]# echo $week
6
[root@oldboyedu ~/test]# echo "$week_oldboy"
[root@oldboyedu ~/test]# echo "${week}_oldboy"
6_oldboy
2.5 Bash中逻辑操作符系列
名称 | 解释 |
---|---|
&& | 前一个命令执行成功,再执行后面一个命令 |
ll | 前一个命令执行失败,再执行后面一个命令 |
! | 1、在Bash中表示取反,2、在vi/vim中表示强制,3、!ls表示最近一次以ls开头的命令并运行 |
网友评论