欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

马哥 07_03 _Linux文件查找详解 有大用

[root@ebs-22618 ~]# help let

image.png


let I =$[$I+1]

let I+=1 相当于 let I++

let SUM=$[$SUM+$I]  这里其实可以不要使用 let 了 

let SUM+=$I


-=

let I-=1 相当于 let I--

其实还有 ++I  --I

*=

/=

%=



image.png


[root@ebs-22618 ~]# I=5

[root@ebs-22618 ~]# let I=$I+1

[root@ebs-22618 ~]# echo $I

6

[root@ebs-22618 ~]#



[root@ebs-22618 ~]# I=10

[root@ebs-22618 ~]# let I+=1

[root@ebs-22618 ~]# echo $I

11

[root@ebs-22618 ~]# let I+=1

[root@ebs-22618 ~]# echo $I

12

[root@ebs-22618 ~]#

[root@ebs-22618 ~]# let I++

[root@ebs-22618 ~]# echo $I

13

[root@ebs-22618 ~]#


[root@ebs-22618 ~]# let I++

[root@ebs-22618 ~]# echo $I

14

[root@ebs-22618 ~]#



[root@ebs-22618 ~]# let I/=3

[root@ebs-22618 ~]# echo $I

4

[root@ebs-22618 ~]# let I*=3

[root@ebs-22618 ~]# echo $I

12

[root@ebs-22618 ~]# let I+=2

[root@ebs-22618 ~]# echo $I

14

[root@ebs-22618 ~]#

[root@ebs-22618 ~]# let I%=3

[root@ebs-22618 ~]# echo $I

2

[root@ebs-22618 ~]#


计算 100 以内的所有奇数的和以及所以偶数的和,并分别显示之

[root@ebs-22618 ~]# vim showsum.sh

#!/bin/bash

#


declare -i EVENSUM=0

declare -i ODDSUM=0

for I in {1..100}; do

  if [ $[$I%2] -eq 0 ]; then

    let EVENSUM+=$I

  else

    let ODDSUM+=$I

  fi

done

echo "Odd sum is: $ODDSUM"

echo "Even sum is: $EVENSUM"

[root@ebs-22618 ~]# chmod +x showsum.sh

[root@ebs-22618 ~]# ./showsum.sh

image.png


grep egrep fgrep:文本查找

文件查找 

locate:

    非实时,模糊匹配,查找是根据全系统文件数据库进行的:每天晚上根据计划任务更新文件信息到数据库

    updatedb (新系统或刚建的文件找不到)此时,使用  updatedb 就可以了(生成或更新数据库)(搜集每一个文件的信息,所以耗时大)

    它支持正则

    但是最大优点就是速度快

find

    实时查找 精确 

    支持众多查找标准 (根据文件,根据文件权限,根据文件类型等 还可以支持正则表式式进行匹配)

    遍历指定目录中的所有文件完成查找,速度慢


find 查找路径 查找标准 查找到以后的处理动作

查找路径可以省略,默认为当前目录

查找标准可以省略,默认为所以文件

处理动作可以省略,默认为显示至屏幕上(打印)


匹配标准 (查找标准):    使用选项指定的

    -name 'FILENAME': 对文件名作精确匹配的,严格区分大小写来查找的

            文件名通配:

                *:任意长度的任意字符

                ?:

                []:

    -iname 'FILENAME': 文件名匹配不区分大小写来查找

    -regex PATTERN:基于模式正则表达式进行文件查找

    -user USERNAME:根据属主来进行查找

    -group GROUPNAME:根据属组查找

    -uid UID: 根据UID查找

    -gid GID:根据GID查找

    -nouser: 查找没有属主的文件 (应该定期把 -nouser 的文件通通给管理员,免得其它用户乘虚而入,因为没有属主的文件,可能其它用户也有权限访问,或者通过其它的漏洞机制,进而获得文件的控制权,如果再在这个文件中放入危害性的文件,麻烦就大了)

    -nogroup: 查找没有属组的文件



    -type 根据文件类型来查找

        f: 普通文件

        d:目录

        c:字符设备

        b:块设备

        l:符号链接文件

        p:管道设备

        s:socket套接字设备


     -size:根据文件大小来查找  后面跟个数字和单位(k,M,G等) (默认单位是字节)

        [+|-]#k           +10k表示大于10k,-10k表示小于10k    (没有+或-,表示精确为10k)    中括号表示可省的

        #M

        #G


组合条件:  (默认是与条件)

    -a:and 与条件 与符号

    -o:or  或条件 或符号

    -not: 非条件 否条件 否符号

        


表示多少多少天

-mtime 修改时间

-ctime 改变时间

-atime 访问时间

    [+|-]#  (假设为atime) (没有加减号 刚好#天访问过一次) (-表示#天之内访问的) (+表示#天之外,至少有几天没有访问了)


下面表示分钟

-mmin

-cmin

-amin

 [+|-]#  (假设为atime) (没有加减号 刚好#分钟访问过一次) (-表示#分钟之内访问的) (+表示#分钟之外,至少有几天没有访问了)


# find /etc -mtime -2  查找/etc 下面最近 2 天之内个修改过的文件,看看谁改的,可以基本判定异常访问行为



-perm MODE  根据权限来查找    (-perm 755 或 -perm 644 等等 )

        /MODE 任意一个权限位匹配就可以了,就能够显示出来了

        -MODE  每一位(所有)都必须包含匹配才行 (文件权限能完全包含此MODE时才能显示,才符合条件)

        -644

                   644 匹配 -644                                        rw-r--r--

                   755 匹配 -644 因为它包含644                rwxr-xr-x

                    750 不匹配 -644 因为第三个不包含        rwxr-x---


find ./ -perm -001


动作: action

    -print 默认动作 ,显示

    -ls : 类似于 ls -l 的形式显示每一个文件的详细信息,,而且显示的更丰富

    -ok:COMMOND {} \;  每一个操作都需要用户确认  (\;  反斜杠分号结束,花括号作为文件名称占位符) 和下面exec是等价的 (右花括号与斜红之间必须要有空格)

    -exec:COMMOND {} \;   操作不需要用户确认  (\; 反斜杠分号结束,花括号作为文件名称占位符) (右花括号与斜红之间必须要有空格)







        

image.png




[root@ebs-22618 ~]# find

image.png



[root@ebs-22618 ~]# find /etc -name "passwd"

image.png

[root@ebs-22618 ~]# find /etc -name "passwd*"     以passwd开头的文件

image.png

[root@ebs-22618 ~]# find /etc -name "*passwd"    以passwd结尾的文件

image.png

[root@ebs-22618 ~]# find /etc -name "*passwd*"  包含passwd的文件

image.png



[root@ebs-22618 ~]# man find

image.png

image.png


[root@ebs-22618 ~]# ls -l /tmp | more

image.png


[root@ebs-22618 ~]# find /tmp -user hadoop

image.png

[root@ebs-22618 ~]# find /tmp -user student

image.png


image.png

image.png



查找 /tmp 目录下 没有属主的用户

[root@ebs-22618 ~]# find /tmp -nouser

image.png


[root@ebs-22618 ~]# find /tmp -type d         查找/tmp下的目录

image.png


[root@ebs-22618 ~]# find /tmp -type s        套接字文件

image.png


[root@ebs-22618 ~]# find /tmp -size 1M

image.png


[root@ebs-22618 ~]# find /tmp -size 10M   (查找大小为10M的文件 ,9.几M的文件和10M的文件都认为是10M的)


[root@ebs-22618 ~]# find /tmp -size 1k  (查找大小为1k的文件 ,0.几k的文件和1k的文件都认为是1k的)


[root@ebs-22618 ~]# find /tmp -size 10k  (9k到10k之间都可以显示出来)

image.png


[root@ebs-22618 ~]# find /tmp -size 10k -ls  (此时可以显示文件大小的)

image.png


[root@ebs-22618 ~]# find /tmp -size -10k  (所有小于10k的文件)

[root@ebs-22618 ~]# find /tmp -size +10k  (所有大于10k的文件)


[root@ebs-22618 ~]# find /tmp -nouser -a -type d  (没有属主的且类型为目录的文件)


[root@ebs-22618 ~]# find /tmp -nouser -a -type d  -ls (ls看看查找的结果啥样子)


[root@ebs-22618 ~]# find /tmp -nouser -o -type d    (没有属主的或类型为目录的文件)


[root@ebs-22618 ~]# find /tmp -not -type d    (查找非目录类型的文件)



/tmp目录下,不是目录,并且不是套接字类型的文件

/tmp/test 目录下,属主不是user1,也不是 user2的文件

[root@ebs-22618 ~]# mkdir /tmp/test

[root@ebs-22618 ~]# cd /tmp/test

[root@ebs-22618 test]# ls

[root@ebs-22618 test]# cp /etc/init.d/* ./

[root@ebs-22618 test]# ls -la

image.png

image.png


[root@ebs-22618 test]# useradd user1

useradd:用户“user1”已存在

[root@ebs-22618 test]# useradd user2

useradd:用户“user2”已存在

[root@ebs-22618 test]# chown user1 yunsuo_guard

[root@ebs-22618 test]# chown user2 yunsuo

[root@ebs-22618 test]# find ./ -user user1

./yunsuo_guard

[root@ebs-22618 test]# find ./ -user user2

./yunsuo

[root@ebs-22618 test]#

[root@ebs-22618 test]# find ./ -not -user user1

[root@ebs-22618 test]# find ./ -not -user user2


[root@ebs-22618 test]# rm -rf ./*

[root@ebs-22618 test]# touch a b c d

[root@ebs-22618 test]# chown user1 a

[root@ebs-22618 test]# chown user2 b

[root@ebs-22618 test]# ls -la

总用量 48

drwxr-xr-x  2 root  root  4096 5月  18 19:39 .

drwxrwxrwt. 8 root  root 40960 5月  18 19:35 ..

-rw-r--r--  1 user1 root     0 5月  18 19:39 a

-rw-r--r--  1 user2 root     0 5月  18 19:39 b

-rw-r--r--  1 root  root     0 5月  18 19:39 c

-rw-r--r--  1 root  root     0 5月  18 19:39 d


[root@ebs-22618 test]# find ./ -not -user user1

image.png


[root@ebs-22618 test]# find ./ -not -user user2


image.png


[root@ebs-22618 test]# find ./ -not -user user1 -a -not -user user2        (既不是user1,又不是user2的)

image.png

[root@ebs-22618 test]# find ./ -not \( -user user1 -o  -user user2 \)      (既不是user1,又不是user2的)

image.png

image.png



[root@ebs-22618 test]# mkdir hello

[root@ebs-22618 test]# mkdir hi

[root@ebs-22618 test]# chown user1 hi

image.png


[root@ebs-22618 test]# find ./ -not -user user1 -o -not -type d  (属主不是user1或者类型不是目录)


image.png



[root@ebs-22618 test]# find ./ -not \( -user user1 -a -type d \)  (属主不是user1或者类型不是目录) (也就是既是属主是user1,且 类型是目录的 才不显示出来)

image.png



[root@ebs-22618 test]# pwd

/tmp/test

[root@ebs-22618 test]# stat *

image.png


[root@ebs-22618 test]# find ./ -amin -5

image.png


[root@ebs-22618 test]# touch a

[root@ebs-22618 test]# touch -a a  改的是访问时间

[root@ebs-22618 test]# find ./ -amin -5   5分钟之内访问过

image.png


[root@ebs-22618 test]# stat hi

image.png

[root@ebs-22618 test]# stat hello/

image.png


[root@ebs-22618 test]# find ./ -amin 5

image.png


[root@ebs-22618 test]# find ./ -amin +5      5分钟之外访问过(至少有5分钟没有访问过)

image.png



[root@ebs-22618 test]# find /tmp -atime +7  (至少有7天没有访问过)

image.png


[root@ebs-22618 test]# find /tmp -atime +30 | more    (至少一个月没有访问过)   (其实 /tmp系统会自动维护某些文件,会自动清除某些文件)

image.png



[root@ebs-22618 test]# find /usr -atime +30 | more

image.png


[root@ebs-22618 test]# man find

image.png


[root@ebs-22618 test]# find ./ -perm 644

image.png

[root@ebs-22618 test]# chmod o-r a

[root@ebs-22618 test]# find ./ -perm 644

image.png



[root@ebs-22618 test]# find ./ -perm /644  (斜杠表示只要有一位匹配到,就显示出来)

image.png


[root@ebs-22618 test]# find ./ -perm /640  (斜杠表示只要有一位匹配到,就显示出来)

image.png


[root@ebs-22618 test]# chmod 006 b

[root@ebs-22618 test]# ls -l

image.png


[root@ebs-22618 test]# find ./ -perm /640

image.png


[root@ebs-22618 test]# ls -la

image.png

[root@ebs-22618 test]# ls -la

image.png

[root@ebs-22618 test]# chmod 750 hi

[root@ebs-22618 test]# find ./ -perm -644

image.png


[root@ebs-22618 test]# find ./ -perm -001   (其它用户有执行权限的)

image.png

[root@ebs-22618 test]# find ./ -perm -022    (组用户有写权取,其它用户有写权限)

image.png



[root@ebs-22618 test]# find ./ -perm /022   (组用户是权取或其它用户是权限)

image.png


[root@ebs-22618 test]# find ./ -perm -007   (其它用户有所有 能读能写能执行权限)



[root@ebs-22618 test]# find ./ -perm -006

image.png


[root@ebs-22618 test]# find ./ -perm -006 -exec chmod o-w {} \;  (找到其它用户权限为可读 可写的文件,  这里o表示other其它的意思   然后去掉 可写权限) (u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。)

[root@ebs-22618 test]# ls -la

image.png


[root@ebs-22618 test]# find ./ -type d -ok chmod +x {} \;    (给所有的目录 加上可执行权限)

< chmod ... ./ > ? y

< chmod ... ./hello > ? y

< chmod ... ./hi > ? y

[root@ebs-22618 test]# ls -la

总用量 56

drwxr-xr-x  4 root  root  4096 5月  18 19:50 .

drwxrwxrwt. 8 root  root 40960 5月  22 21:41 ..

-rw-r-----  1 user1 root     0 5月  22 20:28 a

-------r--  1 user2 root     0 5月  18 19:39 b

-rw-r--r--  1 root  root     0 5月  18 19:39 c

-rw-r--r--  1 root  root     0 5月  18 19:39 d

drwxr-xr-x  2 root  root  4096 5月  18 19:50 hello

drwxr-x--x  2 user1 root  4096 5月  18 19:50 hi

image.png


[root@ebs-22618 test]# chmod 666 b

[root@ebs-22618 test]# ls -l

image.png

[root@ebs-22618 test]# chmod 664 d

[root@ebs-22618 test]# ls -l

总用量 8

-rw-r----- 1 user1 root    0 5月  22 20:28 a

-rw-rw-rw- 1 user2 root    0 5月  18 19:39 b

-rw-r--r-- 1 root  root    0 5月  18 19:39 c

-rw-rw-r-- 1 root  root    0 5月  18 19:39 d

drwxr-xr-x 2 root  root 4096 5月  18 19:50 hello

drwxr-x--x 2 user1 root 4096 5月  18 19:50 hi

image.png

[root@ebs-22618 test]# find ./ -perm -020 -exec mv {} {}.new \;  (把组有写权限的文件改为文件名加.new)

[root@ebs-22618 test]# ls -la

image.png




[root@ebs-22618 test]# cd

[root@ebs-22618 ~]#

[root@ebs-22618 ~]# find ./ -name "*.sh" -a -perm -111

image.png

[root@ebs-22618 ~]# find ./ -name "*.sh" -a -perm -111 -exec chmod o-x {} \;  (把所有(用户,组,其它)包含有执行权限的文件的其它的执行权限移除掉)

[root@ebs-22618 ~]# ls -la

image.png


image.png


image.png


image.png

[root@ebs-22618 ~]# find /var -user root -a -group mail

image.png

[root@ebs-22618 ~]# find /usr -not \( -user root -o -user bin -o -user student \)

[root@ebs-22618 ~]# find /usr -not  -user root -a -not -user bin -a -not -user student 

image.png

[root@ebs-22618 ~]# find /etc -mtime -7 -a -not \( -user root -o -user bin  \)

[root@ebs-22618 ~]# find /etc -mtime -7 -a -not -user root -a not -user bin  \)

image.png

[root@ebs-22618 ~]# find / \( -nouser -o -nogroup \) -a \( -mtime -1 \)

image.png

[root@ebs-22618 ~]# find /etc -size +1M  >> /tmp/testaaa.txt

image.png

[root@ebs-22618 ~]# find /etc -not -perm /222 -ls


普通分类: