前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Find 命令使用大全

Find 命令使用大全

作者头像
用户8418197
修改2021-08-09 11:25:42
8260
修改2021-08-09 11:25:42
举报
文章被收录于专栏:howtouselinuxhowtouselinux

find命令

一般格式: find + 目录名称 + 参数

1

@1参数的含义:

name #文件名称

实验1:按照文件名查找

##查找/etc目录中文件名为passwd的文件

root@localhost ~# find /etc/ -name passwd

/etc/passwd

/etc/pam.d/passwd

##查找/etc目录中文件名以.conf文件结尾的文件

root@localhost mnt# find /etc/ -name *.conf

Advanced Find exec xargs Examples in Linux

@2参数含义:

not #非,取反

-user #文件所有人

-group #文件所有组

-a #并且关系

-o #或者关系

实验2:按文件所有人和文件所有组查找

root@localhost ~# cd /mnt

##建立文件

root@localhost mnt# touch file{1..5}

root@localhost mnt# ls

file1 file2 file3 file4 file5

监控:

root@localhost mnt# watch -n 1 ls -l /mnt

1

10 Linux Find Exec examples - Advanced Part

root@localhost ~# id student

uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)

root@localhost ~# id westos

uid=1001(westos) gid=1001(westos) groups=1001(westos)

##更改文件的所有人和所有组

root@localhost ~# chown student.student /mnt/file1

##更改文件的所有组

root@localhost ~# chgrp westos /mnt/file2

root@localhost ~# chown student.westos /mnt/file3

##按文件的所有人查找

root@localhost ~# find /mnt -user student

/mnt/file1

/mnt/file3

##按文件的所有组查找

root@localhost ~# find /mnt -group westos

/mnt/file2

/mnt/file3

##默认表示并且

root@localhost ~# find /mnt -user root -group westos

/mnt/file2

-a表示并且

root@localhost ~# find /mnt -user root -a -group westos

/mnt/file2

-o表示或者

root@localhost ~# find /mnt -user root -o -group westos

/mnt

/mnt/file2

/mnt/file3

/mnt/file4

/mnt/file5

-not表示非;即反向选择

root@localhost ~# find /mnt -not -user student

/mnt

/mnt/file2

/mnt/file4

/mnt/file5

@3参数含义:

maxdepth #最大深度

-mindepth #最小深度

实验3:按文件所在的深度(层次)查找

##-maxdepth表示最大深度,即最多层次

root@localhost ~# find /etc/ -maxdepth 1 -name passwd

/etc/passwd

root@localhost ~# find /etc/ -maxdepth 2 -name passwd

/etc/passwd

/etc/pam.d/passwd

##-mindepth表示最小深度,即最少层次

root@localhost ~# find /etc/ -mindepth 2 -name passwd

/etc/pam.d/passwd

root@localhost ~# find /etc/ -mindepth 1 -name passwd

/etc/passwd

/etc/pam.d/passwd

##查找/etc目录下最少层次为1最多层次为2的以.conf结尾的文件

root@localhost ~# find /etc/ -mindepth 1 -maxdepth 2 -name *.conf

Understanding Depth In Linux Find Command

@4参数含义:

-size 表示文件大小

代码语言:txt
复制
 -size  20K      # 查找大小为20K的文件
代码语言:txt
复制
 -size  -20K     # -表示小于;查找比20K小的文件
代码语言:txt
复制
 -size  +20k     # +表示大于;查看比20K大的文件

实验4:按文件的大小查找

root@localhost ~# cd /mnt

root@localhost mnt# rm -rf *

root@localhost mnt# ls

##dd表示截取,if输入,of输出

root@localhost mnt# dd if=/dev/zero of=file1 bs=1 count=10240

10240+0 records in

10240+0 records out

10240 bytes (10 kB) copied, 0.0113629 s, 901 kB/s

##查看文件所占磁盘的大小

root@localhost mnt# du -sh file1

12K file1

root@localhost mnt# dd if=/dev/zero of=file2 bs=1 count=20480

20480+0 records in

20480+0 records out

20480 bytes (20 kB) copied, 0.0198726 s, 1.0 MB/s

root@localhost mnt# du -sh file2

20K file2

root@localhost mnt# dd if=/dev/zero of=file3 bs=1 count=40960

40960+0 records in

40960+0 records out

40960 bytes (41 kB) copied, 0.0397736 s, 1.0 MB/s

root@localhost mnt# du -sh file3

40K file3

Find Files in Linux with Find Command Examples

root@localhost mnt# ll -l

total 72

-rw-r--r--. 1 root root 10240 Nov 11 04:06 file1

-rw-r--r--. 1 root root 20480 Nov 11 04:06 file2

-rw-r--r--. 1 root root 40960 Nov 11 04:06 file3

##查找/mnt目录下文件大小为20k的文件

root@localhost mnt# find /mnt/ -size 20k

/mnt/file2

##查找/mnt目录下比20k小的文件

root@localhost mnt# find /mnt/ -size -20k

/mnt/

/mnt/file1

##查找/mnt目录下比20k大的文件

root@localhost mnt# find /mnt/ -size +20k

/mnt/file3

20 Advanced Linux Find Command Examples

@5参数含义:

type #文件类型

主要的文件类型:

代码语言:txt
复制
 f        #普通文件
代码语言:txt
复制
 d        #目录
代码语言:txt
复制
 b        #块设备
代码语言:txt
复制
 s        #套接字
代码语言:txt
复制
 c        #字符设备
代码语言:txt
复制
 l        #链接
代码语言:txt
复制
 p        #管道

实验5:按文件类型查找

##f表示普通文件

root@localhost ~# find /dev -type f

/dev/shm/pulse-shm-620843697

/dev/shm/pulse-shm-1247103260

/dev/shm/pulse-shm-2690706600

/dev/shm/pulse-shm-368331657

##b表示块设备

root@localhost ~# find /dev -type b

/dev/dm-0

/dev/sr0

/dev/vdb1

/dev/vdb

/dev/vda1

/dev/vda

##s表示套接字

root@localhost ~# find /dev -type s

/dev/log

##p表示管道

root@localhost ~# find /dev -type p

/dev/initctl

root@localhost ~# find /mnt -type f

/mnt/file1

/mnt/file3

/mnt/file2

##d表示目录

root@localhost ~# find /mnt -type d

/mnt

Find File By Name in Linux

@6参数含义:

perm 表示权限

-perm 444 #查找文件权限

-perm -444 # -表示并且;查找文件权限中u位有r权限,并且g位有r权限,并且o位有r权限的文件

-perm /444 # /表示或者;查找文件权限中u位有r权限,或者g位有r权限,或者o位有r权限的文件

-perm /777 # 777=rwx rwx rwx 即9个条件中满足任意一个即可

实验6:按文件权限查找

root@localhost ~# cd /mnt

root@localhost mnt# rm -rf *

root@localhost mnt# ls

##建立文件

root@localhost mnt# touch file{1..3}

root@localhost mnt# ll

total 0

-rw-r--r-- 1 root root 0 Nov 14 09:41 file1

-rw-r--r-- 1 root root 0 Nov 14 09:41 file2

-rw-r--r-- 1 root root 0 Nov 14 09:41 file3

##更改文件权限

root@localhost mnt# chmod 777 /mnt/file1

root@localhost mnt# chmod 404 /mnt/file2

root@localhost mnt# chmod 400 /mnt/file3

root@localhost mnt# ll

total 0

-rwxrwxrwx 1 root root 0 Nov 14 09:41 file1

-r-----r-- 1 root root 0 Nov 14 09:41 file2

-r-------- 1 root root 0 Nov 14 09:41 file3

##查找文件权限为404的文件

root@localhost mnt# find /mnt -perm 404

/mnt/file2

##查看文件权限中u位有r权限,并且o位有r权限的文件

root@localhost mnt# find /mnt -perm -404

/mnt

/mnt/file1

/mnt/file2

##查看文件权限中u位有r权限,或者o位有r权限的文件

root@localhost mnt# find /mnt -perm /404

/mnt

/mnt/file1

/mnt/file2

/mnt/file3

root@localhost mnt# ll -d /mnt/

drwxr-xr-x. 2 root root 42 Nov 14 09:41 /mnt/

@7参数含义:

ctime 与 cmin 都表示按照时间查找被篡改的文件

ctime ##以天为单位

cmin ##以分钟为单位

-cmin 10 #查找文件更新距离现在10分钟的文件

-cmin +10 #查找文件更新距离现在超过10分钟的文件

-cmin -10 #查找文件更新距离现在10分钟以内的文件

-ctime +/-10 #查找文件更新距离现在超过10天/10天以内的文件

实验7:按文件更新的时间

root@localhost ~# cd /mnt

root@localhost mnt# rm -rf *

root@localhost mnt# ls

##建立文件

root@localhost mnt# touch file{1..3}

##查找文件更新距离现在为1分钟的文件

root@localhost mnt# find /mnt/ -ctime 1

##查找文件更新距离现在为1分钟以内的文件

root@localhost mnt# find /mnt/ -ctime -1

/mnt/

/mnt/file1

/mnt/file2

/mnt/file3

##查找文件更新距离现在超过1分钟的文件

root@localhost mnt# find /mnt/ -ctime +1

8 参数含义:

exec 命令 {} \; #对查找到的文件执行某命令;-exec表示开始执行动作 {} 表示用find命令查找出的所有文件

1

2

3

实验8:对查找到的文件执行某些动作

(1).给/mnt下文件权限包含004的文件的g位加w的权限

root@localhost mnt# pwd

/mnt

root@localhost mnt# ll

total 0

-rw-r--r-- 1 root root 0 Nov 14 10:06 file1

-rw-r--r-- 1 root root 0 Nov 14 10:06 file2

-rw-r--r-- 1 root root 0 Nov 14 10:06 file3

##更改权限

root@localhost mnt# chmod 404 /mnt/file2

root@localhost mnt# ll

total 0

-rw-r--r-- 1 root root 0 Nov 14 10:06 file1

-r-----r-- 1 root root 0 Nov 14 10:06 file2

-rw-r--r-- 1 root root 0 Nov 14 10:06 file3

##给/mnt下文件权限包含004的文件的g位加w的权限

root@localhost mnt# find /mnt -perm 404 -exec chmod g+w {} \;

root@localhost mnt# ll

total 0

-rw-r--r-- 1 root root 0 Nov 14 10:06 file1

-r---w-r-- 1 root root 0 Nov 14 10:06 file2

-rw-r--r-- 1 root root 0 Nov 14 10:06 file3

(2).将系统中属于mail组的文件备份到/mnt下

root@localhost ~# ll /mnt

total 0

-rw-r--r-- 1 root root 0 Nov 14 10:06 file1

-r---w-r-- 1 root root 0 Nov 14 10:06 file2

-rw-r--r-- 1 root root 0 Nov 14 10:06 file3

##将系统中属于mail组的文件备份到/mnt下

root@localhost ~# find / -group mail -exec cp {} /mnt \;

find: ‘/proc/6812/task/6812/fd/6’: No such file or directory

find: ‘/proc/6812/task/6812/fdinfo/6’: No such file or directory

find: ‘/proc/6812/fd/6’: No such file or directory

find: ‘/proc/6812/fdinfo/6’: No such file or directory

cp: omitting directory ‘/var/spool/mail’

root@localhost ~# ll /mnt

total 0

-rw-r--r-- 1 root root 0 Nov 14 10:06 file1

-r---w-r-- 1 root root 0 Nov 14 10:06 file2

-rw-r--r-- 1 root root 0 Nov 14 10:06 file3

-rw-r----- 1 root root 0 Nov 14 10:14 rpc

-rw-r----- 1 root root 0 Nov 14 10:14 student

-rw-r----- 1 root root 0 Nov 14 10:14 westos

参考:

10 Linux Find Exec examples - Advanced Part

20 Advanced Linux Find Command Examples

本文系转载,前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系?cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • @1参数的含义:
  • @2参数含义:
    • -a表示并且
      • -o表示或者
        • -not表示非;即反向选择
        • @3参数含义:
          • @4参数含义:
          • @5参数含义:
          • @6参数含义:
            • @7参数含义:
            • 8 参数含义:
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
            http://www.vxiaotou.com