find命令带不同参数的作用及配合其他命令使用时的强大功能

17 篇文章 1 订阅

find命令带不同参数的作用及配合其他命令使用时的强大功能

一、 find命令介绍

find是linux 命令,它将档案系统内符合 expression 的档案列出来。你可以指要档案的名称、类别、时间、大小、权限等不同资讯的组合,只有完全相符的才会被列出来。find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部分为 path,之后的是 expression。
--------------------------来自百度百科

find 命令用法

[root@a1e14095-cca7-5fa6-8~]# find --help
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

默认路径为当前目录;默认表达式为 -print
表达式可能由下列成份组成:操作符、选项、测试表达式以及动作:

操作符 (优先级递减;未做任何指定时默认使用 -and):
      ( EXPR )   ! EXPR   -not EXPR   EXPR1 -a EXPR2   EXPR1 -and EXPR2
      EXPR1 -o EXPR2   EXPR1 -or EXPR2   EXPR1 , EXPR2

positional options (always true): -daystart -follow -regextype

normal options (always true, specified before other expressions):
      -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
      --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race

比较测试 (N 可以是 +N 或 -N 或 N): -amin N -anewer FILE -atime N -cmin N
      -cnewer 文件 -ctime N -empty -false -fstype 类型 -gid N -group 名称
      -ilname 匹配模式 -iname 匹配模式 -inum N -ipath 匹配模式 -iregex 匹配模式
      -links N -lname 匹配模式 -mmin N -mtime N -name 匹配模式 -newer 文件
      -nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
      -readable -writable -executable
      -wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
      -used N -user NAME -xtype [bcdpfls]
      -context 文本


操作: -delete -print0 -printf 格式 -fprintf 文件 格式 -print 
      -fprint0 文件 -fprint 文件 -ls -fls 文件 -prune -quit
      -exec 命令 ; -exec 命令 {} + -ok 命令 ;
      -execdir 命令 ; -execdir 命令 {} + -okdir 命令 ;

man find

linux命令行输入man find命令后发现输出的帮助信息太多了,提供能够使用的参数很多,如果想都掌握,还是比较难的,用到的时候再去深究吧

二、抛砖引玉

以下记录了我曾今用过、了解过的find用法,希望能起到抛砖引玉的作用

find命令单独使用

1、通过文件名称查找

格式:find path -name XXX
实例:

[root@a1e14095-cca7-5fa6-8 ~]# find ./ -name test2
./pert/test/test2
[root@a1e14095-cca7-5fa6-8 ~]# find ./ -name '[a-z][a-z][0-9][0-9].txt'
[root@a1e14095-cca7-5fa6-8 ~]# touch tr88.txt tr99.doc
[root@a1e14095-cca7-5fa6-8 ~]# ls -al tr*
-rw-r--r-- 1 root root 0 222 22:04 tr88.txt
-rw-r--r-- 1 root root 0 222 22:04 tr99.doc
[root@a1e14095-cca7-5fa6-8 ~]# find ./ -name '[a-z][a-z][0-9][0-9].txt'
./tr88.txt
[root@a1e14095-cca7-5fa6-8 ~]# find ./ -name "[a-z][a-z][0-9][0-9].txt"
./tr88.txt
[root@a1e14095-cca7-5fa6-8 ~]find ./ -name '[a-z][a-z][0-9]*'
./tr88.txt
./tr99.doc
#加参数-print0 与不加该参数的区别(见上下的搜索结果)
[root@a1e14095-cca7-5fa6-8 ~]# find ./ -name '[a-z][a-z][0-9]*' -print0
./tr88.txt./tr99.doc[root@a1e14095-cca7-5fa6-8 ~]# 

2、通过用户查找

格式:find path -user username
实例:

[root@a1e14095-cca7-5fa6-8 ~]# find ~ -user sshMTBlMzJm
/root/pert/stress-1.0.4
/root/pert/stress-1.0.4/depcomp
/root/pert/stress-1.0.4/configure.in
/root/pert/stress-1.0.4/INSTALL
/root/pert/stress-1.0.4/configure
/root/pert/stress-1.0.4/AUTHORS
/root/pert/stress-1.0.4/ChangeLog
/root/pert/stress-1.0.4/Makefile.in
/root/pert/stress-1.0.4/TODO
/root/pert/stress-1.0.4/aclocal.m4
.......
[root@a1e14095-cca7-5fa6-8 ~]

查找无所属用户的文件
(在/home目录找没有属主的文件—文件属主在/etc/passwd中已经被删除)

find /home -nouser

3、通过用户组查找

find /apps -group accts #在/apps下寻找属于accts用户组的文件
find /-nogroup #寻找没有用户组的文件 

4、通过时间查找

find / -mtime -5 
find / -mtime +5
find / -mtime 5

(寻找更改在5天以内的文件,如果是+5,就是寻找更改在5日以外的文件,如果是5,就是正好5天的文件)
Numeric arguments can be specified as
+n for greater than n,
-n for less than n,
n for exactly n.
-atime:最近访问时间 access time
-mtime:最近更改时间 modify time
-ctime:最近状态改动时间 change time
-amin n:File was last accessed n minutes ago.
-cmin n:File’s status was last changed n minutes ago.
-mmin n: File’s data was last modified n minutes ago.
-ctime n: File’s status was last changed n*24 hours ago.

5、通过类型查找

find /etc -type d

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
D - door (Solaris) 它提供了一种新的形式的接口,用于客户端和服务器的应用进程之间通信的文件。

6、通过权限查找

find . -perm 755 #在当前目录找权限为755的文件

r:4
w:2
x:1

7、通过文件大小查找

find ~ -size -1000c #查找小于1000字节的文件
find ~ -size +512k #查找大于512k字节的文件
find ~ -size 1M #查找等于1M字节的文件
find ./ -size +5G -size -10G #查找大于5G小于10G的文件

b: 代表 512 位元组的区块(如果用户没有指定后缀,则默认为 b)
c: 表示字节数
k: 表示 kilo bytes (1024字节)
w: 字 (2字节)
M:兆字节(1048576字节)
G: 千兆字节 (1073741824字节)

find和其他命令配合使用

[root@a1e14095-cca7-5fa6-8 ~]# find ./ -type f -name '*.txt' -exec printf "The text file is %s \n" {} \;
The text file is ./tr88.txt 
The text file is ./youdao88.txt 
[root@a1e14095-cca7-5fa6-8 ~]# find ./ \( -name nm* -o -name "*.txt" \)
./tr88.txt
./youdao88.txt
./nm999

注:寻找nm* 或*.txt的文件

[root@a1e14095-cca7-5fa6-8 pert]# find ./ -name "*.txt" |cat -n
     1  ./111.txt
[root@a1e14095-cca7-5fa6-8 pert]# find ./ -name "*.txt"
./22.txt
[root@a1e14095-cca7-5fa6-8 pert]# find ./ -name "*.txt" -print -exec rm -f {} \;
./22.txt
[root@a1e14095-cca7-5fa6-8 pert]# find ./ -name "*.txt"
[root@a1e14095-cca7-5fa6-8 pert]# 

注:-exec 指出要执行后面的系统命令
rm -f 删除找出的文件
{} 只有该符号能跟在命令后面
\ 结束符
3. 也可以使用 xargs 代替 -exec,如下:
find ./ -name “.txt" |xargs rm {} -rf
find ./ -name "
.txt” |xargs rm -rf
find ./ -name".txt" -delete
find ./ -name "
.txt -ok rm -r {} ; #exec和ok的作用差不多,但ok会询问,更安全点

find 查找文件的目录 -path 需要排除的目录 -prune -o -name 需要查询的内容
注意事项:
1)-prune 必须和 -path, -o 一起使用
2)-prune -o 的顺序不 能调换
3)-name等必须放在-prune -o后面才能使用
eg: find . -path ./a -prune -o -name “*.txt”

[root@a1e14095-cca7-5fa6-8 pert]# find ./ -path "./stress-1.0.4" -prune -o -print
./
./usr.tar
./usr.iso
./image-2.iso
./check.ppy
./she.sh
./qemu.iso
./test
./test/test2
./test/33
......
[root@a1e14095-cca7-5fa6-8 pert]# find ./ -path "./stress-1.0.4" -prune -o -name "*.iso" -print
./usr.iso
./image-2.iso
./qemu.iso
[root@a1e14095-cca7-5fa6-8 pert]#
find ./ -name "*.c" |xargs grep "displayStr" ---查找当前文件夹下包含“displayStr”字符的文件
 find /etc -type d -exec ls -l {} \;  
 find ./ -name "*.txt" -exec cat {} \;
find -type d | grep .svn$ | xargs rm -r
find ./ -name "1*.txt" -exec ls -al {} \;
find . -regex '.*\.txt\|.*\.sh'
find ./ -name "*.txt" -exec cat {} \;
find ./ \( -name nm* -o -name "*.txt" \)
find /tmp /bin -name star.log  #同时在多个目录下查找
find ./ -type f -name "log.txt" -delete  #多参数

grep 查找

grep -RHn INVALID_HANDLE ./ #查找当前文件夹中是否有问题包含“INVALID_HANDLE”,还有很多其他更精确的用法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值