正课五

查找、高级文本分析

什么是查找?

为什么使用查找?

 

which 查找对象为 PATH 指定路径下的可执行的 shell 命令或可执行脚本。

 #echo $PATH

 #which passwd

whereis 查找一个命令的二进制文件位置,源代码位置,man 页的位置

  #whereis passwd

locate 基于数据库查找,速度快。 依赖于数据库,更新数据库命令为 updatedb

  #touch /root/rootfile

  #locate rootfile

  #updatedb

  #locate rootfile

find    全盘扫描

find  要查找的位置   选项   参数    要查找的内容

 

参数:

  -name 按照文件名查找

  -iname 名字不区分大小写

    #cd /tmp

    #touch redhat

    #REDHAT

    #find . -name redhat

    #find . -iname redhat

 

  -user 用户名    按照用户查找文件

  -group  组名    按照组查找文件

  -nouser   没有所有者  孤儿文件

  -nogroup  没有所属组

    #cd /tmp

    #su - candan

    $cd /tmp 

    $touch candanfile

    $exit

    #su - uplooking

    $cd /tmp

    $touch uplookingfile

    $exit

    #cd /tmp

    #find . -user candan

    #find -group uplooking

    #userdel uplooking

    #find . -nouser

    #find . -nogroup

  -size  文件大小

    c  byte

    k  +.k (大于)   -.k(小于)   k(等于)

    M  +…M    -…M    ….M

    #cd /tmp

    #dd if=/dev/zero of=1Mfile bs=1M count=1

    #dd if=/dev/zero of=3Mfile bs=1M count=3

    # dd if=/dev/zero of=8Mfile bs=1M count=8

    #find . -size +1M

    #find .-size -8M

    #find . -size +1M -and -size -8M

    #find . -size 3M

  -type 文件类型

    f:普通文件

    d:目录文件

    b:块设备文件

    l:链接文件

    p:管道文件

    ssocket 文件

    c:字符设备文件:打印机

  

  -perm 权限    0000   7777

    + 只要满足条件就输出

    - 只有满足条件才输出

    绝对匹配

[root@x1 test1]# ll

总计 12308

-rw-r--r-- 1 root root 1048576 07-29 14:01 1Mfile

-rw-r--r-- 1 root root 3145728 07-29 14:03 3Mfile

-rw-r--r-- 1 root root 8388608 07-29 14:05 8Mfile

-rwxr--r-- 1 root root       0 07-29 14:06 a

-rwxrwxrwx 1 root root       0 07-29 14:06 b

[root@x1 test1]# find   -perm  +111

.

./b

./a

[root@x1 test1]# find  -perm  -111

.

./b

[root@x1 test1]# find  -perm  744

./a

[root@www ~]# ls –l  `find /sbin -perm +7000`

[root@x1 test1]#

 

  -empty  空文件

  -amin n 分钟

-cmin n

-mmin n

 

  -atime n (24 小时, 从当前时间开始)  

  -ctime n   

  -mtime n

  -anewer filename

-cnewer filename

-mnewer -newerfilename

 

查找以后直接处理找到的文件

  -exec 非交互

  -ok   交互

  # find . -perm 744 -exec rm -f {} \;

  # find . -perm 744 -ok rm -f {} \;

 

查找/etc/目录下所有.conf 的文件,将这些文件拷贝到/backup 目录内

  # find /etc -name *.conf -exec cp {} /backup \;

 

逻辑关系运算

  -o或者

    #find /etc -name passwd -o -name shadow

  -and: -a

    # find . -type f -and -size 3M

  -not: 非(!)

    # find . -type f -not -name 3Mfile

 ---------------------------------------------------------------------------------------

管道和重定向

 

Linux中,管道和 I/O 重定向是两个最强大的命令行功能。

 

I/O重定向   允许你将标准输出或错误消息从程序重定向至文件,以进行保存或稍后进行分析,或禁止其在终端显示,你还可以通过文件而非键盘输入读取至命令行程序。

 

管道  允许你将标准输出信息从程序连接至另一个程序的输入。这允许将多个小程序连接成一个管道,每个程序作用于前一个程序的输出。

名称

说明

编号

默认

STDIN

标准输入

0

键盘

STDOUT

标准输出

1

终端

STDERR

标准错误

2

终端

 

关键字

定义

示例

> 1>

STDOUT重定向到文件(覆盖)

date > /tmp/file

cat file1 file2 > /tmp/file

echo hello > /dev/tty2

ls –l / > /dev/tty3

>> 1>>

STDOUT重定向到文件(追加)

date >> /tmp/file

ls >> /tmp/file

2>

STDERR重定向到文件(覆盖)

find / -user mike 2>/tmp/error

find / -user mike >/tmp/output 2>/tmp/error

2>/dev/null

通过重定向到

/dev/null (黑洞文件)丢弃错误

find / -user mike 2>/dev/null

find / -user mike >/tmp/output 2>/dev/null

2>&1

STDERR

STDOUT组合

find / -user mike > /tmp/all 2>&1

ls  bucunzai  2>&1 | less

(错误输出不能通过管道)

&>

STDERR

STDOUT一起重定向到文件

find / -user mike &> /tmp/all

 

< 0<

重定向STDIN

# echo y > y.txt

# touch file

# rm file < y.txt

<<  

重定向STDIN(追加)

cmd<< 界位符 > filename 打开临时缓冲区,直到遇到界位符

$ cat << END > f5

> a

> b

> end

> END

|

管道 将一个命令的STDOUT 发送到另一个命令的

STDIN

# cat /etc/passwd | less

# ls –l /bin | tail

 

 

 

 

你可能感兴趣的:(find)