Linux 查找命令(find)详解

1. 文件查找

在文件系统上查找符合条件的文件

命令解释
which查看可执行文件的位置,只能寻找执行文件,并在PATH变量里面寻找
whereis查看文件的位置;只能查二进制文件,说明文档,源文件等
locate配合数据库查看文件位置;能查所有,但跟whereis一样都是查询数据库里面的内容(/var/lib/locatedb),速度快,但是有延时,极耗资源
find实际搜索硬盘查询文件名称;最强大,但是检索硬盘,比较慢

1.1 find

find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件。

find的使用格式如下:

$ find <指定目录> <指定条件> <指定动作>
# <指定目录>: 所要搜索的目录及其所有子目录。默认为当前目录。
# <指定条件>: 所要搜索的文件的特征。
# <指定动作>: 对搜索结果进行特定的处理。

如果什么参数也不加,find默认搜索当前目录及其子目录,并且不过滤任何结果(也就是返回所有文件),将它们全都显示在屏幕上。

find的使用实例:

$ find . -name 'my*'

搜索当前目录(含子目录,以下同)中,所有文件名以my开头的文件。

$ find . -name 'my*' -ls

搜索当前目录中,所有文件名以my开头的文件,并显示它们的详细信息。

$ find . -type f -mmin -10

搜索当前目录中,所有过去10分钟中更新过的普通文件。如果不加-type f参数,则搜索普通文件+特殊文件+目录。

1.2 locate

  • locate 依赖于查询系统上实现构建好的文件索引数据库 /var/lib/mlocate/mlocate.db

  • 索引的构建是在系统较为空闲时自动进行(周期性任务),执行updatedb可以更新数据库

  • 索引构建过程需要遍历整个根文件系统,很消耗资源

  • locate和updatedb命令来自于mlocate包

locate命令其实是"find -name"的另一种写法,但是要比后者快得多,原因在于它不搜索具体目录,而是搜索一个数据库(/var/lib/locatedb),这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,所以使用locate命令查不到最新变动过的文件。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。

1.2.1 索引库的更新有两种方式
  • 系统自动实现(周期性任务)

  • 手动更新数据库(命令:updatedb)

1.2.2 工作特性
  • 查找速度快(基于数据库实现)

  • 模糊查找

  • 非实时查找

  • 搜索的是文件的全路径,不仅仅是文件名

  • 可能只搜索用户具备读取和执行权限的目录

1.2.3 命令使用格式
locate [OPTION]... PATTERN...

选项:

header 1header 2
-b只匹配路径中的基名,而非整个路径名
例:locate -b nginx
-c统计出共有多少个符合条件的文件
例:locate -c nginx;locate -b -c nginx
-r基于基本正则表达式来编写模式
-i不区分大小写的搜索
-n N只列举前N个匹配项目

注意:索引构建过程需要遍历整个根文件系统,极消耗资源

示例:

# 搜索etc目录下所有以sh开头的文件。
$ locate /etc/sh

# 搜索用户主目录下,所有以m开头的文件。
$ locate ~/m

# 搜索用户主目录下,所有以m开头的文件,并且忽略大小写。
$ locate -i ~/m

# 搜索名称或路径中包含"conf"的文件 
locate conf 

# 使用Regex来搜索以".conf"结尾的文件 
locate -r '\.conf$'

示例:locatedb创建数据库

[root@centos8 ~]#dnf -y install mlocate
[root@centos8 ~]#locate conf
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[root@centos8 ~]#updatedb
[root@centos8 ~]#ll /var/lib/mlocate/mlocate.db
-rw-r----- 1 root slocate 1040305 Apr 24 14:54 /var/lib/mlocate/mlocate.db
[root@centos8 ~]#locate -n 3 conf
/backup/etc_2021-04-01/chkconfig.d
/backup/etc_2021-04-01/dracut.conf
/backup/etc_2021-04-01/dracut.conf.d
[root@centos8 ~]#

示例:文件新创建和删除,无法马上更新locate数据库

[root@centos8 ~]#touch test_redis.log
[root@centos8 ~]#locate test_redis.log
[root@centos8 ~]#updatedb
[root@centos8 ~]#locate test_redis.log
/root/test_redis.log
[root@centos8 ~]#touch test_redis2.log
[root@centos8 ~]#locate test_redis2.log
[root@centos8 ~]#updatedb
[root@centos8 ~]#locate test_redis2.log
/root/test_redis2.log
[root@centos8 ~]#rm -f test_redis2.log
[root@centos8 ~]#locate test_redis2.log
/root/test_redis2.log
[root@centos8 ~]#

示例

[root@centos8 ~]#locate -n 10 -ir '\.CONF$'
/backup/etc_2021-04-01/dracut.conf
/backup/etc_2021-04-01/host.conf
/backup/etc_2021-04-01/idmapd.conf
/backup/etc_2021-04-01/kdump.conf
/backup/etc_2021-04-01/krb5.conf
/backup/etc_2021-04-01/ld.so.conf
/backup/etc_2021-04-01/libaudit.conf
/backup/etc_2021-04-01/libuser.conf
/backup/etc_2021-04-01/locale.conf
/backup/etc_2021-04-01/logrotate.conf
[root@centos8 ~]#

1.3 whereis

whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。

whereis命令的使用实例:

[root@centos ~]#whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz /usr/share/info/grep.info.gz
[root@centos ~]#

1.4 which

which命令的作用是,在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。

which命令的使用实例:

[root@centos ~]#which grep
alias grep='grep --color=auto'
	/usr/bin/grep
[root@centos ~]#

1.5 type

type命令其实不能算查找命令,它是用来区分某个命令到底是由shell自带的,还是由shell外部的独立二进制文件提供的。如果一个命令是外部命令,那么使用-p参数,会显示该命令的路径,相当于which命令

type命令的使用实例:

# 系统会提示,cd是shell的自带命令(build-in)
[root@centos ~]#type cd
cd is a shell builtin
[root@centos ~]#

# 系统会提示,grep是一个外部命令,并显示该命令的路径。
[root@centos ~]#type grep
grep is aliased to `grep --color=auto'
[root@centos ~]#

# 加上-p参数后,就相当于which命令
$ type -p grep

2. find:实时查找工具

find 是实时查找工具,通过遍历指定起始路径下文件系统层级结构完成文件查找

工作特点

  • 查找速度略慢

  • 精确查找(find /etc -name “passwd”)

  • 实时查找

  • 查找条件丰富

  • 可能只搜索用户具备读取和执行权限的目录

格式

find [OPTIONS] [查找起始路径] [查找条件] [处理动作]

查找起始路径:指定具体搜索目标起始路径,默认为当前目录

查找条件:指定的查找标准,可以根据文件名、大小、类型、从属关系、权限等标准进行,默认为找出指定路径下的所有文件

处理动作:对符合查找条件的文件作出的操作,例如删除等操作,默认为输出至标准输出

注:查找条件或者查找标准,也称为表达式,由选项和测试组成,而测试的结果通常为布尔型(“true”,“false”)

2.1 指定搜索目录层级

参数说明
-maxdepth level最大搜索目录深度,指定目录下的文件为第1级
-mindepth level最小搜索目录深度

示例

[root@centos8 data]#tree
.
├── mbr
├── redis3679
│   ├── redis3679.log
│   ├── redis3679.rdb
│   └── redis.conf
├── redis3680
│   ├── redis3680.aof
│   ├── redis3680.rdb
│   ├── redis.conf
│   └── redisdata
│       └── redis3680_data
├── sum.sh
└── test.file

3 directories, 10 files
[root@centos8 data]#find /data -maxdepth 1 -mindepth 1
/data/test.file
/data/sum.sh
/data/mbr
/data/redis3679
/data/redis3680
[root@centos8 data]#find /data -maxdepth 2 -mindepth 2
/data/redis3679/redis.conf
/data/redis3679/redis3679.rdb
/data/redis3679/redis3679.log
/data/redis3680/redis.conf
/data/redis3680/redis3680.aof
/data/redis3680/redis3680.rdb
/data/redis3680/redisdata
[root@centos8 data]#find /data -maxdepth 3 -mindepth 3
/data/redis3680/redisdata/redis3680_data
[root@centos8 data]#

2.2 对每个目录先处理目录内的文件,再处理目录本身

-depth

-d         # warning: the -d option is deprecated; please use -depth instead, because the latter is a POSIX-compliant feature

示例

[root@centos8 data]#tree /data/redis3680
/data/redis3680
├── redis3680.aof
├── redis3680.rdb
├── redis.conf
└── redisdata
    └── redis3680_data

1 directory, 4 files
[root@centos8 data]#find /data/redis3680
/data/redis3680
/data/redis3680/redis.conf
/data/redis3680/redis3680.aof
/data/redis3680/redis3680.rdb
/data/redis3680/redisdata
/data/redis3680/redisdata/redis3680_data
[root@centos8 data]#find /data/redis3680 -depth   # 对每个目录先处理目录内的文件,再处理目录本身
/data/redis3680/redis.conf
/data/redis3680/redis3680.aof
/data/redis3680/redis3680.rdb
/data/redis3680/redisdata/redis3680_data
/data/redis3680/redisdata
/data/redis3680
[root@centos8 data]#

2.3 根据文件名和inode查找

参数说明
-name “文件名称”支持使用glob风格的通配符,如:*,?,[],[^],通配符要加双引号引起来
-iname “文件名称”支持使用glob风格的通配符,如 :*,?,[],[^],通配符要加双引号引起来,字母不区分大小写
-inum n按inode号查找
-samefile name相同inode号的文件
-links n链接数为n的文件
-regex “PATTERN”以PATTERN匹配整个文件路径,而非文件名称

示例

# 查找所有的passwd文件(区分大小写)
find /etc/ -name "passwd"

# 查找所有的passwd文件(不区分大小写)
find /etc/ -iname "passwd"

# 查找以passwd开头
find /etc/ -iname "passwd*"

# 查找以passwd结尾
find /etc/ -iname "*passwd"

# 查找以passwd结尾只能是字母的文件
find /etc/ -iname "passwd[[:alnum:]]"

# -regex pattern:基于正则表达式模式查找文件,匹配是整个路径,而非其名
[root@centos8 data]#find -regex ".*\.sh$"
./sum.sh
./for.sh
./while.sh
[root@centos8 data]#

示例

# 查找test目录下所有以.c结尾的文件,注意"*.c"中的双引号
$ find ./test/ -name "*.c"
./test/test_c/c.c
./test/test_b/b.c
./test/test_a/a.c
$ find ./test/ -name "*.c" -exec cat {} \;
ccccc
bbbbb
aaaa
$ find ./test -name "*.c"|xargs -I {} cat  {}
ccccc
bbbbb
aaaa

2.4 根据文件的从属关系查找

参数说明
-user USERNAME查找属主为指定用户(UID)的文件
-group GROUPNAME查找属组为指定组(GID)的文件
-uid UserID查找属主为指定的 UID 号的文件
-gid GroupID查找属组为指定的 GID 号的文件
-nouser查找没有属主的文件
-nogroup查找没有属组的文件

示例

# -nouser:查找没有属主的文件
find /tmp -nouser
# -nogroup:查找没有属组的文件
find /tmp -nogroup

示例

# 查找 harry 用户拥有的文件,拷贝到目录 /opt/finddir
find / -user harry -exec cp -r {} /opt/finddir/ \;

# 查找 /tmp 目录下属组是 song 的文件
find /tmp -group song

2.5 根据文件的类型查找

-type TYPE:

TYPE可以是以下形式

参数说明
f普通文件
d目录文件
l符号链接文件
b块设备文件
c字符设备文件
p管道文件
s套接字文件

示例

# 查找/dev目录下所有的块设备文件
find /dev -type b -ls
# 查找/etc目录下的所有符号链接
find /etc -type -l -ls

[root@centos8 ~]#find /etc/ -type d -o -type l -a -ls |wc -l
103
[root@centos8 ~]#find /etc/ \( -type d -o -type l \) -a -ls |wc -l
305
[root@centos8 ~]#

2.6 空文件或目录

-empty

示例

[root@centos8 data]#ll /data/temp_data/
total 0
drwxr-xr-x  2 root root   6 Apr 24 11:09 ./
drwxr-xr-x. 5 root root 129 Apr 24 11:09 ../
[root@centos8 data]#find /data/ -type d  -empty
/data/temp_data
[root@centos8 data]#

2.7 组合条件

参数解释示例
-a与,默认多个条件是与关系find /tmp -nouser -type f -ls
-ofind /tmp -nouser or -nogroup -ls
-not非,也可以用!表示find /etc -not -user root -ls

示例

# 找出/etc目录下属主为非root的所有文件
find /etc -not -user root -ls

# 找出/etc目录下文件名中不包含fstab字符串的文件
find /etc -not -iname "*fatab*" -ls

# 找出/etc目录下属主为非root,而且文件名不包含fstab字符串的文件
find /etc -not -user root -a -not -iname "*fatab*" -ls
find /etc -not \( -user root -o -iname "*fatab*" \) -ls

# 找出/tmp目录下,属主不是root,且文件名不以f开头的文件
find /tmp \( -not -user root -a -not -name 'f*' \) -ls 
find /tmp -not \( -user root -o -name 'f*' \) –ls

德·摩根定律

  • (非 A) 或 (非 B) = 非(A 且 B)

  • (非 A) 且 (非 B) = 非(A 或 B)

!A -a !B = !(A -o B)

!A -o !B = !(A -a B)

示例

[root@centos8 data]#ls
for.sh  mbr  redis3679  redis3680  sum.sh  temp_data  test.file  while.sh
[root@centos8 data]#find  ! \( -type d -a  -empty \)
.
./test.file
./sum.sh
./mbr
./redis3679
./redis3679/redis.conf
./redis3679/redis3679.rdb
./redis3679/redis3679.log
./redis3680
./redis3680/redis.conf
./redis3680/redis3680.aof
./redis3680/redis3680.rdb
./redis3680/redisdata
./redis3680/redisdata/redis3680_data
./for.sh
./while.sh
[root@centos8 data]#find  ! -type d -o ! -empty | wc -l
16
[root@centos8 data]#find  ! -type d -o ! -empty
.
./test.file
./sum.sh
./mbr
./redis3679
./redis3679/redis.conf
./redis3679/redis3679.rdb
./redis3679/redis3679.log
./redis3680
./redis3680/redis.conf
./redis3680/redis3680.aof
./redis3680/redis3680.rdb
./redis3680/redisdata
./redis3680/redisdata/redis3680_data
./for.sh
./while.sh
[root@centos8 data]#
[root@centos8 data]#find ! -user song ! -user apache
.
./test.file
./sum.sh
./mbr
./redis3679
./redis3679/redis.conf
./redis3679/redis3679.rdb
./redis3679/redis3679.log
./redis3680
./redis3680/redis.conf
./redis3680/redis3680.aof
./redis3680/redis3680.rdb
./redis3680/redisdata
./redis3680/redisdata/redis3680_data
./for.sh
./while.sh
./temp_data
[root@centos8 data]#

2.8 排除目录

示例

# 查找/etc/下,除/etc/security目录的其它所有.conf后缀的文件
find /etc -path '/etc/security' -a -prune -o -name "*.conf"


# 查找/etc/下,除/etc/security和/etc/systemd,/etc/dbus-1三个目录的所有.conf后缀的文件 
find /etc \( -path "/etc/security" -o -path "/etc/systemd" -o -path "/etc/dbus- 1" \) -a -prune -o -name "*.conf"

# 排除/proc和/sys目录
[root@centos8 data]#find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1
/proc
/run/log/journal/19a097d00fb64eb3b484b14346661eda/system.journal
/sys
/var/lib/rsyslog/imjournal.state
/var/log/messages
[root@centos8 data]#

2.9 根据文件的大小查找

header 1header 2
-size [+|-]#UNIT常用单位:k, M, G,c(byte),注意大小写敏感
#UNIT表示(#-1, #],如:6k 表示(5k,6k],左边包含,右边不包含,是一个左闭右开的区间
-#UNIT表示[0,#-1],如:-6k 表示[0,5k]
+#UNIT表示(#,∞),如:+6k 表示(6k,∞)

示例

[root@centos8 ~]#dd if=/dev/zero of=f1.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.351111 s, 299 MB/s
[root@centos8 ~]#dd if=/dev/zero of=f2.txt bs=1M count=99
99+0 records in
99+0 records out
103809024 bytes (104 MB, 99 MiB) copied, 0.433386 s, 240 MB/s
[root@centos8 ~]#find -size 100M -ls
201335915 102400 -rw-r--r--   1  root     root     104857600 Apr  2 15:47 ./f1.txt
[root@centos8 ~]#ll f2.txt;echo >> f2.txt;ll f2.txt
-rw-r--r-- 1 root root 103809024 Apr  2 15:47 f2.txt
-rw-r--r-- 1 root root 103809025 Apr  2 15:49 f2.txt
[root@centos8 ~]#find -size 100M -ls
201335915 102400 -rw-r--r--   1  root     root     104857600 Apr  2 15:47 ./f1.txt
201335918 101380 -rw-r--r--   1  root     root     103809025 Apr  2 15:49 ./f2.txt
[root@centos8 ~]#

示例

# 查找/etc下所有以.conf结尾的大于10k的文件
find /etc/ -name "*.conf" -type f -size +10k -exec du -a -k {} \; | sort -rn

# 查找/etc下所有以.conf结尾的大于10k的文件并复制到/tmp下
find /etc/ -name "*.conf" -type f -size +10k -exec du -a -k {} \; -exec cp -rf {} /tmp/ \;

2.10 根据时间戳查找

2.10.1 以"天"为单位
  -atime [+|-]#
      #:[#,#-1)   表示大于等于#,小于#-1,左闭右开的区间   -atime 1
      -#:(#,0]     表示小于#,大于0                     -atime -1
      +#:(oo,#-1]		                               -atime +1
  -mtime [+|-]#
  -ctime [+|-]#
2.10.2 以"分钟"为单位
header 1header 2
-amin
-mmin
-cmin

示例

# 线上删除日志示例(-mmin +1440以分钟为单位表示不够精准,改用秒数)  
find /data/search/index/ -maxdepth 1 -daystart -mmin +1440 -type d -exec rm -rf {} \;
find /search/odin/script/data/ -maxdepth 1 -daystart -mmin +1440 -type d -exec rm -rf {} \; 
find /search/odin/run/tmp -maxdepth 1 -daystart -mmin +43200 -type d -exec rm -rf {} \; 
2.10.3 与特定的文件的时间比较,即,查找比某文件新或某文件旧的文件
# -newer,-anewer,-cnewer选项用于查找与特定的文件比较的已修改或访问过的文件,类似mtime,atime,ctime
# 环境上日志文件太多,想删除某个时间之前的文件,该怎么处理?
# 可以利用如下参数
-newer 指内容最近被修改的文件
-anewer 指最近被读取过的文件
-cnewer 指状态最近发生变化的文件

示例

# 列出比1.log更旧的文件
find ./ ! -newer 1.log |xargs ls -al 
# 列出比1.log更新的文件
find ./  -newer 1.log |xargs ls -al 

清理线上日志

cat /search/odin/extserver/archive_ext_server.sh
echo "[`date +"%F %T"`]begin to archive logs .."
cd ${PREFIX}
rm -f core.*
# touch一个以当前时间戳为准的隐藏文件
touch -d"$(date -d "1 hour ago")" .rm.time.flag
# 查看log目录下比.rm.time.flag文件更旧的文件并删除
find log ! -newer .rm.time.flag | xargs -I {}  rm -f {} 2>/dev/null
echo "[`date +"%F %T"`]done"

清理日志

touch -d"$(date -d'5 days ago')" .clean_time_flag
for dir in log watch/*; do
  find ${dir} ! -newer .clean_time_flag | xargs -I{}  rm -f {}   
done
find download ! -newer .clean_time_flag | xargs -I{}  rm -rf {} 

2.11 根据权限查找

-perm  [/|-]mode

mode:精确权限匹配

/mode:任何一类用户(u,g,o)的权限中的任何一位(r,w,x)符合条件即满足,"或"关系,+ 从CentOS 7开始淘汰
9位权限之间存在"或"关系

-mode:每一类用户(u,g,o)的权限中的每一位(r,w,x)同时符合条件即满足,"与"关系
9为权限之间存在"与"关系

0 表示不关注

说明:

  • find -perm 755 会匹配权限模式恰好是755的文件
  • 只要当任意人有写权限时,find -perm /222就会匹配
  • 只有当每个人都有写权限时,find -perm -222才会匹配
  • 只有当其它人(other)有写权限时,find -perm -002才会匹配

示例

find /tmp -perm /666 -ls;find /tmp -perm /002 -ls

# 查找权限为644的(精确权限匹配)
find /tmp -perm 644 -ls

2.12 正则表达式

-regextype type
    Changes the regular expression syntax understood by -regex and -iregex tests
which occur  later  on  the command  line.   Currently-implemented  types  are
emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-
extended.

-regex pattern
    File name matches regular expression pattern.  This is a match on the whole
path,  not  a  search.   For example,  to match a file named `./fubar3', you can
use the regular expression `.*bar.' or `.*b.*3', but not `f.*r3'.  The regular
expressions understood by find are by default Emacs Regular  Expressions,  but
this can be changed with the -regextype option.

示例

find /data -regextype posix-extended -regex "regex"

2.13 处理动作

动作解释
-print输出至标准输出,默认的动作
-ls类似于对查找到的文件执行 “ls -dils” 命令,输出文件的详细信息
-delete删除查找到的文件,危险,慎用
-fls /path/to/somefile把查找到的所有文件的长格式信息(详细信息)保存至指定文件中,相当于 -ls > file
-ok COMMAND {} \;对查找到的每个文件执行由COMMAND表示的命令,每次操作都由用户进行确认
-exec COMMADN {} \;对查找到的每个文件执行由COMMAND表示的命令;每次操作不需要用户进行确认
{}用于引用查找到的文件名称自身

示例

# 将其他用户有写权限的文件改名,{}占位符,是引用找到的文件的文件名
find ./ -perm /002 -exec mv {} {}.danger \;

#注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性的传递给后面的命令
#但是,有些命令不能接受过长的参数,此时命令执行会失败,另一种方式可规避此问题,find | xargs COMMAND

示例

# 备份配置文件,添加.orig这个扩展名
find -name ".conf" -exec cp {} {}.orig \;

# 提示删除存在时间超过3天以上的joe的临时文件
find /tmp -ctime +3 -user joe -ok rm {} \;

# 在主目录中寻找可被其它用户写入的文件
find ~ -perm -002 -exec chmod o-w {} \;

# 查找/data下的权限为644,后缀为sh的普通文件,增加执行权限
find /data –type f -perm 644 -name "*.sh" –exec chmod 755 {} \;

3. 常见示例

find /var -user root -a -group mall -ls

find /usr -not -user root -a -not -user bin -a -not -user hadoop
    
find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls

find /etc -mtime -7 -a -not \( -user root -o -user hadoop \) -ls

find /etc -size +1M -type f -ls 

find /etc -size +1M -type f -exec ls -lh {}  \; 

查找当前系统上没有属主或属组,且最近一周内曾被访问的文件或目录

find /   \( -nouser  -o -nogroup \)  -atime -7 -ls

查找/etc目录下所有用户都没有写权限的文件

find /etc -not -perm /222 -type f -ls 
find /etc -not -perm /222 -exec -ls -lh {} \;

查找/etc目录下至少有一类用户没有执行权限的文件

find /etc -not -perm -111 -type f -ls

查找/etc/init.d/目录下,所有用户都有执行权限,且其他用户有写权限的所有文件

find /etc/init.d -perm -111 -a -perm -002 -ls 
# 也可以写成
find /etc/init.d -perm -113 -type f -ls 

删除/tmp下除passwd以外的其他文件(思路:找出passwd之后取反)

find /tmp -type f ! -name "passwd"|xargs \rm -f
tree /tmp

查看24小时之内修改内容(mtime)的文件

find ./ -mtime 0 或 find ./ -mtime -1

查找当前路径10分钟以内5分钟以外的文件

find ./ -mmin -10 -mmin +5 -type f

查找/search下所有7天以前以log结尾的大于1M的文件移动到/tmp下

find /search -type f -name "*.log" -size +1M -mtime +7 -exec mv {} /tmp \;
find /search -type f -name "*.log" -size +1M -mtime +7 |xargs -i mv {} /tmp

-mtime n 查找系统中最后 n*24 小时被改变文件数据的文件

find ./ -maxdepth 1 -type d -daystart -mtime 1    # 大于24小时,且小于48小时

find ./ -maxdepth 1 -type d -daystart -mtime +1   # 大于48小时,小于72小时

4. 注意事项

# 一般来说这两个参数或命令是一样的。可是在一些情况下尤其是打包压缩的时候差别就很大了。
# find命令找到的文件一次性都给 xargs 处理
find /search -type f |xargs  
# -exec find命令找到一个文件 就传递给 exec 处理一次
find /search -type f -exec   

#默认xargs不支持{}这种形式,xargs加上-i就可以支持,-i参数就可以用{}花括号了。

-exec就是find命令自己的参数,-exec默认的形式是 -exec 命令 {}  \;  
注意:是以\;结尾的。
{}表示find命令找到的文件。
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值