第1章 linux的特殊符号

1.1 通配符 * {}

1.1.1 含义

方便查找文件 通配符是用来找文件名字的。

1.1.2 *

通过find 命令找以 .sh 结尾的文件,使用*替代文件名字。

  1. find /clsn -type f -name "*.sh" -mtime +7 -size +100k -size -10M

查找文件名中,包含有clsn字节的文件。

  1. [root@znix 20170118]# find -type f -name "*clsn*"
  2. [root@znix 20170118]# ls -l *clsn*

1.1.3 {}

{} 用来生成序列

  1. [root@znix 20170118]# echo clsn{1..3}.txt
  2. clsn1.txt clsn2.txt clsn3.txt
  3. [root@znix 20170118]# echo {a,c,d,f}
  4. a c d f

echo {a..z} {A..Z} 中间需要有空格,表示两个无关的序列

  1. [root@znix 20170118]# echo {a..z} {A..Z}
  2. a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

生成按规则序列{开始..结束..间隔}

  1. [root@znix ~]# echo {1..10..3}
  2. 1 4 7 10

备份一个文件的时候使用

  1. [root@znix ~]# cp clsn.txt{,.bak}
  2. [root@znix ~]# ll clsn*
  3. -rw-r--r-- 3 root root 241 Aug 30 11:40 clsn.txt
  4. -rw-r--r-- 1 root root 241 Aug 31 09:38 clsn.txt.bak

1.2 特殊符号

1.2.1 特殊符号

  1. > 标准输出重定向,先把内容清空,再向文件里放其他东西
  2. >> 标准追加重定向 向文件内加内容
  3. < 标准输入 xargs
  4. << 追加输入 cat>/clsn.txt<<EOF 追加多行
  5. . 当前目录/linux下面的隐藏文件
  6. .. 当前用户的上一级目录
  7. ~ 当前用户的家目录
  8. / 路径的分割符号
  9. \ 临时取消别名
  10. | 管道
  1. !
  2. 1 vim中强制
  3. 2 取反 find awk
  4. 3 表示使用你用过的命令 使用历史命令
  5. !可以看历史命令 history 命令
  6. !ls ===== history |grep ls
  1. [root@znix ~]# history
  1. # 注释
  2. $ 取出变量里的内容
  3. && 并且 前一个命令运行成功,然后再运行后面的命令
  4. ifdown eth0 && ifup eth0
  5. ; 分开多条命令 在同一行里面放入多个命令。
  6. ls; pwd; hostname

1.2.2 单引号、双引号、不加引号

‘ ‘
吃啥吐啥

  1. [root@znix ~]# echo 'LANGLANG(pwd) `hostname` {1..3}'
  2. LANGLANG(pwd) `hostname` {1..3}

“ “
把双引号里面的特殊符号进行解析

  1. [root@znix ~]# echo "LANGLANG(pwd) `hostname` {1..3}"
  2. en_US.UTF-8 /root znix {1..3}

不加引号

  1. [root@znix ~]# echo LANGLANG(pwd) `hostname` {1..3}
  2. en_US.UTF-8 /root znix 1 2 3


反引号 先运行,把结果留下 与$()作用相同

  1. [root@znix ~]# du -sh `find -type d`
  2. 764K .

第2章 正则表达式

2.1 什么是正则

特殊符号表示文字 文本
^ 开头
[0-9] 数字

2.2 作用

提高效率 省事

2.3 分类

2.3.1 基础正则表达式

  1. ^ ^^ . * .* [0-9] [^0-9]

2.3.2 扩展正则表达式

  1. | () + {} ?

2.4 正则表达式与通配符的区别

1、通配符是用来找文件的。
2、正则表达式用来的文件中找内容、文本。

2.5 基础正则表达式

2.5.1 环境准备

cat -A 在每一行最后加上一个$符号。

  1. [root@znix ~]# clsn.txt
  2. I am clsn teacher!$
  3. I teach linux.$
  4. $
  5. I like badminton ball ,billiard ball and chinese chess!$
  6. my blog is http://clsn.blog.51cto.com$
  7. $
  8. our site is http://www.etiantian.org$
  9. $
  10. my qq num is 49000448.$
  11. $
  12. not 4900000448.$
  13. my god ,i am not oldbey,but clsn!$

2.5.2 找以m开头的行 ^

^m 表示以m开头,^表示以什么开头。

  1. [root@znix ~]# grep "^m" clsn.txt
  2. my blog is http://clsn.blog.51cto.com
  3. my qq num is 49000448.
  4. my god ,i am not oldbey,but clsn!

2.5.3 以m结尾的行结尾的行 $

m$ 表示以m结尾。

  1. [root@znix ~]# grep "m$" clsn.txt
  2. my blog is http://clsn.blog.51cto.com

2.5.4 显示空行,并且加上行号

-n 显示行号
^$ 表示开头和结尾中间没有东西,即空行

  1. [root@znix ~]# grep -n "^$" clsn.txt
  2. 3:
  3. 6:
  4. 8:
  5. 10:

2.5.5 表示任意一个字符 . (点)

点表示任意一个字符,oldb.y 表示点的位置是什么都可以 。

  1. [root@znix ~]# grep "oldb.y" clsn.txt
  2. I am clsn teacher!
  3. my blog is http://clsn.blog.51cto.com
  4. my god ,i am not oldbey,but clsn!

grep -o 显示grep/egrep执行的过程(每一次找到的东西)。

  1. [root@znix ~]# grep -o "." clsn.txt
  2. [root@znix ~]# grep -o "oldb.y" clsn.txt
  3. clsn
  4. clsn
  5. oldbey

2.5.6 找到以点结尾的行

\ 转意符号,把特殊含义的的去掉特殊含义。
.$ 表示以点结尾。

  1. [root@znix ~]# grep '\.$' clsn.txt
  2. I teach linux.
  3. my qq num is 49000448.
  4. not 4900000448.

2.5.7 * 前一个文本连续出现了0次或1次以上

连续出现了0次就是没出现
-o 显示grep找到的过程

  1. [root@znix ~]# grep "0*" clsn.txt
  2. I am clsn teacher!
  3. I teach linux.
  4. I like badminton ball ,billiard ball and chinese chess!
  5. my blog is http://clsn.blog.51cto.com
  6. our site is http://www.etiantian.org
  7. my qq num is 49000448.
  8. not 4900000448.
  9. my god ,i am not oldbey,but clsn!
  10. [root@znix ~]# grep -o "0*" clsn.txt
  11. 000
  12. 00000

2.5.8 正则表达式的贪婪性

有多少要多少,尽可能多的匹配。

2.5.9 .* 表示所有

显示所有的内容,一次找到。

  1. [root@znix ~]# grep -o ".*" clsn.txt
  2. I am clsn teacher!
  3. I teach linux.
  4. I like badminton ball ,billiard ball and chinese chess!
  5. my blog is http://clsn.blog.51cto.com
  6. our site is http://www.etiantian.org
  7. my qq num is 49000448.
  8. not 4900000448.
  9. my god ,i am not oldbey,but clsn!

表示所有.* 连续出现的时候会表现贪婪性。

  1. [root@znix ~]# grep "^.*m" clsn.txt
  2. I am clsn teacher!
  3. I like badminton ball ,billiard ball and chinese chess!
  4. my blog is http://clsn.blog.51cto.com
  5. my qq num is 49000448.
  6. my god ,i am not oldbey,but clsn!

2.5.10 [abc] 中括号表示一个整体

相当于一个符号,表示a或者b或者c。

  1. [root@znix ~]# grep "[0-9]" clsn.txt
  2. [root@znix ~]# grep "[A-Z]" clsn.txt
  3. [root@znix ~]# grep "[a-z]" clsn.txt
  4. 找到文本中的大写和小写字母。
  5. [root@znix ~]# grep "[a-zA-Z]" clsn.txt

2.5.11 找以 m或n或o开头的 并且以 m或g 结尾的行

.* 表是什么都可以
^[mno] m 或n或o开头的
[mg]$ m 或g 结尾

  1. [root@znix ~]# grep "^[mno].*[mg]$" clsn.txt
  2. my blog is http://clsn.blog.51cto.com
  3. our site is http://www.etiantian.org

2.5.12 [^abc] 排除a或排除b或排除c

[^abc] 表示找排除a或排除b或排除c之外的其他字符

  1. [root@znix ~]# grep "[^abc]" clsn.txt
  2. I am clsn teacher!
  3. I teach linux.
  4. I like badminton ball ,billiard ball and chinese chess!
  5. my blog is http://clsn.blog.51cto.com
  6. our site is http://www.etiantian.org
  7. my qq num is 49000448.
  8. not 4900000448.
  9. my god ,i am not oldbey,but clsn!

2.5.13 grep -v 排除与[^abc]

  1. grep -v 排除行
  2. [^abc] 字符或文字

第3章 昨日回顾(删除文件、开机自启动)

3.1 linux如何让一个服务/脚本开机自启动?

1)chkconfig
2)/etc/rc.local

3.1.1 被chkconfig管理 需要什么条件

1)必须放在/etc/init.d/
2)这个脚本要有执行权限
3)加上chkconfig要求的内容
# chkconfig: 2345 99 99
4)chkconfig —add 把脚本添加到开机自启动
5)检查

3.2 /etc/rc.local

  1. [root@znix ~]# ls -l /etc/rc3.d/ |grep rc.local
  2. lrwxrwxrwx. 1 root root 11 Aug 10 18:36 S99local -> ../rc.local

3.3 磁盘空间不足 no space left on device

1)block满了 500G 3*200G视频

df -h du -sh / du -sh / |grep G

2)block满了 文件没有被彻底删除 硬链接数为0,进程调用数不为零
检查:lsof|grep delete

3.4 文件的删除原理(条件)

1、硬链接数为0
2、进程调用数为0
日志
/var/log/messages
/var/log/secure
rsyslog
3、inode满了
创建一个文件要占用一个inode和至少一个block
大量的小文件