linux的特殊符号与正则表达式

第1章 linux的特殊符号

1.1 通配符 * {}

1.1.1 含义

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

1.1.2  *

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

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

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

[root@znix 20170118]# find -type f -name "*clsn*"

[root@znix 20170118]# ls -l *clsn*

1.1.3  {}

{} 用来生成序列

[root@znix 20170118]# echo clsn{1..3}.txt

clsn1.txt clsn2.txt clsn3.txt

[root@znix 20170118]# echo {a,c,d,f}

a c d f

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

[root@znix 20170118]# echo {a..z} {A..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 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

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

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

1 4 7 10

备份一个文件的时候使用

[root@znix ~]# cp clsn.txt{,.bak}

[root@znix ~]# ll clsn*

-rw-r--r-- 3 root root 241 Aug 30 11:40 clsn.txt

-rw-r--r-- 1 root root 241 Aug 31 09:38 clsn.txt.bak

1.2 特殊符号

1.2.1 特殊符号

标准输出重定向,先把内容清空,再向文件里放其他东西

>> 标准追加重定向 向文件内加内容

标准输入    xargs

<< 追加输入    cat>/clsn.txt<<EOF 追加多行

当前目录/linux下面的隐藏文件

.. 当前用户的上一级目录

当前用户的家目录

 路径的分割符号

临时取消别名

管道

1 vim中强制

2 取反 find awk

3 表示使用你用过的命令 使用历史命令

       !可以看历史命令   history 命令

       !ls   ===== history |grep ls

[root@znix ~]# history

#   注释

$   取出变量里的内容

&& 并且 前一个命令运行成功,然后再运行后面的命令

    ifdown eth0 && ifup eth0

;   分开多条命令 在同一行里面放入多个命令。

    ls; pwd; hostname

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

' '

吃啥吐啥

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

$LANG $(pwd) `hostname` {1..3}

" "

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

[root@znix ~]# echo  "$LANG $(pwd) `hostname` {1..3}"

en_US.UTF-8 /root znix {1..3}

不加引号

[root@znix ~]# echo  $LANG $(pwd) `hostname` {1..3}

en_US.UTF-8 /root znix 1 2 3

`  `

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

[root@znix ~]# du -sh    `find -type d`

764K    .

第2章 正则表达式

2.1 什么是正则

特殊符号表示文字 文本

^      开头

[0-9]  数字

2.2 作用

提高效率 省事

2.3 分类

2.3.1 基础正则表达式

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

2.3.2 扩展正则表达式

|  ()  +   {}  ?  

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

1、通配符是用来找文件的。

2、正则表达式用来的文件中找内容、文本。

2.5 基础正则表达式

2.5.1 环境准备

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

[root@znix ~]# clsn.txt

I am clsn teacher!$

I teach linux.$

$

I like badminton ball ,billiard ball and chinese chess!$

my blog is http://clsn.blog.51cto.com$

$

our site is http://www.etiantian.org$

$

my qq num is 49000448.$

$

not 4900000448.$

my god ,i am not oldbey,but clsn!$

2.5.2 找以m开头的行 ^

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

[root@znix ~]# grep "^m" clsn.txt

my blog is http://clsn.blog.51cto.com

my qq num is 49000448.

my god ,i am not oldbey,but clsn!

2.5.3 m结尾的行结尾的行 $

m$ 表示以m结尾。

[root@znix ~]# grep "m$" clsn.txt

my blog is http://clsn.blog.51cto.com

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

-n 显示行号

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

[root@znix ~]# grep -n "^$" clsn.txt

3:

6:

8:

10:

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

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

[root@znix ~]# grep "oldb.y" clsn.txt

I am clsn teacher!

my blog is http://clsn.blog.51cto.com

my god ,i am not oldbey,but clsn!

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

[root@znix ~]# grep -o "." clsn.txt

[root@znix ~]# grep -o "oldb.y" clsn.txt

clsn

clsn

oldbey

2.5.6 找到以点结尾的行

\ 转意符号,把特殊含义的的去掉特殊含义。

\.$ 表示以点结尾。

[root@znix ~]# grep '\.$' clsn.txt

I teach linux.

my qq num is 49000448.

not 4900000448.

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

连续出现了0次就是没出现

-o 显示grep找到的过程

[root@znix ~]# grep "0*" clsn.txt

I am clsn teacher!

I teach linux.

 

I like badminton ball ,billiard ball and chinese chess!

my blog is http://clsn.blog.51cto.com

 

our site is http://www.etiantian.org

 

my qq num is 49000448.

 

not 4900000448.

my god ,i am not oldbey,but clsn!

[root@znix ~]# grep -o "0*" clsn.txt

000

00000

2.5.8 正则表达式的贪婪性

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

2.5.9 .* 表示所有

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

[root@znix ~]# grep -o ".*" clsn.txt  

I am clsn teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my blog is http://clsn.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but clsn!

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

[root@znix ~]# grep "^.*m" clsn.txt

I am clsn teacher!

I like badminton ball ,billiard ball and chinese chess!

my blog is http://clsn.blog.51cto.com

my qq num is 49000448.

my god ,i am not oldbey,but clsn!

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

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

[root@znix ~]# grep  "[0-9]" clsn.txt

[root@znix ~]# grep  "[A-Z]" clsn.txt

[root@znix ~]# grep  "[a-z]" clsn.txt

       找到文本中的大写和小写字母。

[root@znix ~]# grep  "[a-zA-Z]" clsn.txt

2.5.11 找以 mno开头的 并且以  mg 结尾的行

.*       表是什么都可以

^[mno] m no开头的

[mg]$ m  g 结尾

[root@znix ~]# grep "^[mno].*[mg]$" clsn.txt

my blog is http://clsn.blog.51cto.com

our site is http://www.etiantian.org

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

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

[root@znix ~]# grep "[^abc]" clsn.txt

I am clsn teacher!

I teach linux.

I like badminton ball ,billiard ball and chinese chess!

my blog is http://clsn.blog.51cto.com

our site is http://www.etiantian.org

my qq num is 49000448.

not 4900000448.

my god ,i am not oldbey,but clsn!

2.5.13 grep -v 排除与[^abc]

grep -v  排除行

[^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

[root@znix ~]# ls -l /etc/rc3.d/ |grep rc.local

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

3inode满了

创建一个文件要占用一个inode和至少一个block

大量的小文件

posted @ 2017-09-21 13:55  惨绿少年  阅读(2104)  评论(0编辑  收藏  举报