Find命令

名称find - search for files in a directory hierarchy

语法find [-H] [-L] [-P] [path...] [expression]

命令参数:

Pathname    find命令所查找的目录路径。

-print      find将匹配的文件的输出到标准输出中-->默认查找到不打印出来

-exec       find命令对匹配的文件执行该参数所给出的shell命令。相应的形式为‘command’{} \---》也就是说我们用这个参数能够对我们用find找出来的文件进行操作。

-ok         和exec的作用相同只不过是以一种更为安全的模式来执行该参数所给出的shell命令。在执行每一个命令之前都会给出提示让用户来确定是否执行。

命令选项

-name       按照文件的文件名来查找文件

-perm       按照文件权限来查找文件

-prune      可以让find命令不在当前目录里面查找。

-user       按照文件所属的拥有者来查找

-group      按照文件所属的用户组来查找

-nogroup    查找无有效所属组的文件即用户组不在/etc/groups文件里面。

-nouser     查找无有效用户的文件

-type       查找某一类型的文件

d  目录

f  普通文件

l  符号链接文件

c  字符设备文件

b  块设备文件

-size n    查找文件长度为n快的文件带c时表示文件长度以字节计。

-fstype    查找位于某一类型文件系统中的文件。

-mount     在查找文件时不跨越文件系统mount点

-follow    如果find命令遇到符号链接文件就跟踪至链接所指向的文件

-cpio      对匹配的文件使用cpio命令将这些文件备份到磁带设备中。

a:access访问   c:change变更   m:modified改动

-amin n     查找系统中最后N分钟访问(access)的文件

-atime n    查找系统中最后n 天前访问的文件   -n表示在n天内+n表示在n天前其余类似。

-cmin n     查找系统最后n分钟被改变(change)文件状态的文件

-ctime n    查找系统中最后n*24小时被改动过文件状态的文件

-mmin n     查找系统中最后n分钟被改动(modified)过文件数据的文件

-mtime n    查找系统中最后n*24小时被改变文件数据的文件-n  +n 按照文件所更改的时间来查找文件-n 表示更改在n天内的+n表示更改在n天前的。

使用实例

查找指定时间内的文件

[root@LiWenTong test]# find -amin  -1 à访问时间为1天内的文件。--》错了应该是1分钟吧。写博文的时候发现的。

./test1

./test1/test2

查找创建时间在最近的。c:change  改变文件状态的。

find -ctime -19

根据关键字查找

[root@LiWenTong test]# find -name *.log

./2.log

根据权限、类型来查找文件

[root@LiWenTong test]# find / -perm 777 -name *.log

/root/test/2.log

[root@LiWenTong test]# find / -perm 777 -type d

find: /proc/4435/task/4435/fd/4: No such file ordirectory

find: /proc/4435/task/4435/fdinfo/4: No such fileor directory

find: /proc/4435/fd/4: No such file or directory

find: /proc/4435/fdinfo/4: No such file ordirectory

/root/test/test1

[root@LiWenTong test]# find /root/ -type d

/root/

/root/test

/root/test/test1

/root/test/test1/test2

[root@LiWenTong test]# find /root/ -type f

/root/.cshrc

/root/2.log

/root/.bash_logout

/root/3.log

/root/1.log

/root/test.txt

/root/2lo

--->查找用户

find -user root

find -type  f文件  d目录

------以下是一些实例----

$find   ~   -name   "*.txt"   -print    #在$HOME中查.txt文件并显示
$find   .    -name   "*.txt"   -print
$find   .    -name   "[A-Z]*"   -print   #查以大写字母开头的文件
$find   /etc   -name   "host*"   -print #查以host开头的文件
$find   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   #查以两个小写字母和两个数字开头的txt文件
$find .   -perm   755   -print
$find   .   -perm -007   -exec ls -l {} \;   #查所有用户都可读写执行的文件同-perm 777
$find   . -type d   -print
$find   .   !   -type   d   -print
$find   .   -type l   -print

$find   .   -size   +1000000c   -print        #查长度大于1Mb的文件
$find   .   -size   100c         -print       # 查长度为100c的文件
$find   .   -size   +10   -print              #查长度超过期作废10块的文件1块=512字节

$cd /
$find   etc   home   apps    -depth   -print   | cpio   -ivcdC65536   -o   /dev/rmt0
$find   /etc -name "passwd*"   -exec grep   "cnscn"   {}   \;   #看是否存在cnscn用户
$find . -name "yao*"   | xargs file
$find   . -name "yao*"   |   xargs   echo    "" > /tmp/core.log
$find   . -name "yao*"   | xargs   chmod   o-w

-----小结-------------------------------------------------------------------------------

find命令能够找到我们想找的任何文件,因为我们能够设定的参数有很多。如最简单的文件名-name 用户 -user 类型 -type f d l 。修改、访问的时间也能够作为我们查找的参数。

并且我们能对找到的文件进行的操作,只要后面加上-exec参数。当然这个我们后面再讲将怎么操作查找到的文件。现在先说怎么找到我们想要的文件。名称查找是否能够用正则表达式,还需要验证下。接下来就来讲如何对我们查找到的文件进行操作。

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

Find命令之-exec选项


用法简述:

使用find命令查找出来的文件有时候需要做一定的处理。我们通过加上-exec选项,并在后面跟上command命令就可以对我们所查找到的文件进行操作。格式为:-exec command {} \; {}代表的是find查找出来的文件名。此处存有疑问--》后经验证确实是{}代表find找出来的文件,而不是{} \;代表找到的文件。

1)查找文件类型并修改权限

[root@LiWenTong test]# find /root/test -type f -exec ls -l {} \;

--->find /root/test -type f  -name *.log -print

---->有时候为了保证-exec执行正确可以通过-print 或ls先把find查找的文件输出查看下是否是我们需要操作的文件以保误操作。--->这个就是为了在做操作之前先知道下我们要的文件是不是我们想要的。

-rwxrwxrwx 1 root root 26 Apr 29 02:13/root/test/2.log

-rwxrwxrwx 1 root root 43 Apr 29 02:12/root/test/test1/1.log

-rwxrwxrwx 1 root root 22 Apr 29 02:14/root/test/test1/test2/3.log

[root@LiWenTong test]# find /root/test -type f -exec chmod 755 {} \;

-------exec选项后接的命令讲解------------------

-exec后面的命令怎么接命令,有些同学可能还不能直观的理解过来。其实很好理解,我们原本的操作命令格式是什么样的现在的格式还是什么样的。只是当你需要用到查找出的文件的时候,就用{}待入就是了。

比如要删除一个文件:rm  -f  filename 这个是rm原来的格式,那现在是我们要删除找出来的文件,带入就变成  rm -f {},但要记住最后要用\;结束。如果是mv命令呢?原本 mv old.file  /tmp 代入后变 mv {} /tmp \;还是mv {} \; /tmp 呢?

---》以上的问题验证确实是{}代表find找到的文件后文有实例。也就是应该是:mv  old.file {} /tmp \;

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

2)在当前目录下查找数据修改1天内的文件

[root@LiWenTong test]# find -type f -mtime -1 -exec ls -l {} \;

-rwxr-xr-x 1 root root 26 Apr 29 02:13 ./2.log

-rwxr-xr-x 1 root root 43 Apr 29 02:12./test1/1.log

-rwxr-xr-x 1 root root 22 Apr 29 02:14./test1/test2/3.log

[root@LiWenTong test]# find -type f -mtime -1 -exec rm -f {} \;

[root@LiWenTong test]# ll

total 4

drwxrwxrwx 3 root root 4096 Apr 29 04:01 test1

3)使用-ok 选项较为安装的执行命令

使用-ok选项同-exec一样只是在做相应的操作时会有提示。

[root@LiWenTong test]# find /root/test -type d -mtime -1 -ok rm {} \;

< rm ... /root/test > ? y

rm: cannot remove `/root/test': Is a directory

< rm ... /root/test/test1 > ? y

rm: cannot remove `/root/test/test1': Is adirectory

< rm ... /root/test/test1/test2 > ? y

rm: cannot remove `/root/test/test1/test2': Is adirectory

4)-exec使用grep选项

[root@LiWenTong test]# find  /etc -name "passwd" -exec grep"root" {} \;

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

说明任何形式的命令都可以在-exec选项中使用。---》这个说明很强劲哦,在系统中能够对文件进行操作的命令都能够在这里使用的。注意咯!

5)使用find查找文件并将移动指定的目录下。

[root@LiWenTong tmp]# ll log_mv

total 0

-rw-r--r-- 1 root root 0 Apr 29 07:08 1.log

-rw-r--r-- 1 root root 0 Apr 29 07:08 2.log

-rw-r--r-- 1 root root 0 Apr 29 07:08 3.log

-rw-r--r-- 1 root root 0 Apr 29 07:08 4.log

[root@LiWenTong tmp]# find /tmp/log_mv  -name"*.log"  -exec mv {} /root/test/ \;

[root@LiWenTong tmp]# ll /root/test--à需要记住{}是find查找出来的文件那么-exec的命令就需要对应的把位置放好。因为之前老是写成  mv /root/test/ {} \; 如果带入来看 这个mv命令来说就是错误的。

total 4

-rw-r--r-- 1 root root    0 Apr 29 07:01 1

-rw-r--r-- 1 root root    0 Apr 29 07:08 1.log

-rw-r--r-- 1 root root    0 Apr 29 07:08 2.log

-rw-r--r-- 1 root root    0 Apr 29 07:08 3.log

-rw-r--r-- 1 root root    0 Apr 29 07:08 4.log

drwxr-xr-x 3 root root 4096 Apr 29 07:05 test1

Find命令之xargs选项

功能简述:

在使用-exec选项进行文件处理时,有些系统对传给-exec的命令长度有限制。如果太长在执行时会溢出错误、或出现参数太长的错误。-exec是一次性的获取传来的参数-xargs是分多次的获取传来的参数每次获取多少还是一次获取是根据该命令的选项及系统的内核中相应的可调参数决定。并且-exec在处理命令时会发起一个相应的进程可能多个。而xargs则只有一个进程[U2]。如果通过-exec命令来执行如果出现进程很多的情况那么有可能会影响到系统的性能。

xargs如何从find命令中获取结果

我们知道-exec选项是通过{}来代入我们查找到的文件。那么我们的xargs又是如何获取我们查找到的文件呢?其实格式有一下两种:

1) find  -type f | xargs  rm ;  看不到什么代入的{}而是直接xargs 接上命令。那是因为默认的xargs会将(stdin)标准输入作为我们后面命令的参数。其实rm命令会接上在管道(|)之前的命令所产生的结果,也就是我们find查找出来的文件。那么也就相当于是:rm  查找的文件

2) find -type f  |xargs -i mv {} /tmp;这种方式就有代入{}了,相应的也多了个-i的参数。-i参数就是指定我们要用{}来作为find命令结果的替代。这个就和-exec没啥区别咯。然后现在来看看以下的一些实例就可以了。

1)Find查找普通文件并通过xargs file命令来判断文件的类型

[atong@LiWenTong tmp]$ find -type f | xargs file

find: ./test1/tmp/ssh-YmJglw2954: Permissiondenied

./1.txt.tar.gz:                           gzip compresseddata, from Unix, last modified: Thu May 2 20:14:25 2013

./2.txt:                                 ASCIItext

./tar.man:                                UTF-8 UnicodeEnglish text, with overstriking

./rc.sysinit:                             Bourne-Again shellscript text executable

./oldboy/test/test1/test2/test3/test.txt: empty

2)在系统中查找内存信息转存文件core dump然后把结果保存到/tmp/core.log中。

[atong@LiWenTong tmp]$ find /  -name "core" | xargs echo"">>/tmp/core.l

3)在当前目录下查找所有用户具有读、写和执行权限的文件并收回相应的写权限

[atong@LiWenTong tmp]$  find -perm  777  | sudo xargs chmod 744--->这里应为atong没有权限所以需要在xargs前面加上个sudo 要不会提示权限限制。^_^

-rwxr--r-- 1 root root     167 May  2 20:16 1.tar.gz

-rwxr--r-- 1 root root      45 May  2 20:16 1.txt

-rwxr--r-- 1 atong atong    152 May 2 20:14 1.txt.tar.gz

drwxr--r-- 2 root root    4096 Apr 29 07:0620130425175009

-rwxr--r-- 1 root root       5 May  2 21:41 2.txt

-rw-r--r-- 1 root root       0 May  2 21:41 3.txt

4)用grep命令在所有的普通文件中搜索hostname这个词

[root@LiWenTong tmp]# find  -type f   | xargs grep "good"

./1.log:good

5)使用xargs执行mv

[root@LiWenTong test4]# find -name "*.log"| xargs  -i  mv{} test4---》此处的-i选项是表示默认用{}来代表前面的输出[U3]。

[root@LiWenTong test4]# ll /tmp/test4

total 8

-rw-r--r-- 1 root root    5 May  3 04:12 1.log

-rw-rw-r-- 1 atong atong 619 May  3 03:28 core.log

[root@LiWenTong test4]# find -name"*.log"| xargs  -i-p mv {} test4--->-p选项是表示会提示让你确认是否执行之后的操作。

[U1]《find之-exec

Find查找到的结果能够再给-exec作为输入再进行相应的处理。-exe的格式为-exec command {} \:  其中{}代表的就是find出来的结果那具体放在command之后的哪个位置需要具体看是什么command

[U2]《exec和xargs的区别》

Exec会有多个进程在执行命令。而xargs只会有一个进程这样减少系统资源开销。Exec对传过来的命令长度有限制xargs没有。

[U3]《xargs如何从find获取结果》

Exec是通过command后的{}作为find的结果传输通道。而xargs若没有指定就直接是作为stdin来传。如果加上-i参数那么一样是通过{}来作为find的结果传输通道。

最后附上几个可以参考的文章

http://www.cnblogs.com/wanqieddy/archive/2011/06/09/2076785.html【这篇有点杂,内容多】

http://oldboy.blog.51cto.com/2561410/792396 【find+sed的实例运用很好很强大】

http://oldboy.blog.51cto.com/2561410/792396/

-----------------------------后续自我小结--------------------------

find首先在文件查找上基本上我们希望找到的文件它都能够帮我们找到的。另外就是-exec和xargs参数能够让我们对我们所找到的文件进行相应的操作。其实理解了-exec和xargs的格式含义也不算很难使用只要我们对我们的操作命令熟悉就可以了其实就只是一个代入的动作而已。

也正是因为我们的-exec和xargs两个参数后面能够跟上我们系统的任何操作命令也就使得我们find命令能够跟非常多的命令进行结合只要是我们能想到的操作。并且文中最后也提到了find和sed命令的相结合。本身sed命令就地内容的编辑上很灵活 选址、输出、替换、改写等等。这样两个很强的命令一结合就变成了你可以对你系统中任何文件进行任何操作、对文件内容进行任何操作。

Any operation of any files, make any edits to the contents of any

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

转载于:https://blog.51cto.com/atong/1343666

[一文一命令]find命令详解相关推荐

  1. linux 文件 字符集设置,Linux字符集和系统语言设置-LANG,locale,LC_ALL,POSIX等命令及参数详解...

    博文说明[前言]: 本文将通过个人口吻介绍Linux字符集和系统语言设置,包括LANG,locale,LC_ALL,POSIX等命令及参数详解的相关知识,在目前时间点[2017年6月21号]下,所掌握 ...

  2. 【Python】Python3.7.3 - Python命令行参数详解

    文章目录 Python命令行参数概览 -c cmd参数示例 -m mod参数示例 file参数示例 - 参数示例 命令行选项详解 -b 选项 -B选项 -d选项 -E选项 -h / -? / --he ...

  3. docker实践(2)常用命令和DockerFile详解

    <docker实践(1) 入门和springBoot实践部署> <docker实践(2)常用命令和DockerFile详解> <docker实践(3) 仓库registr ...

  4. 离线调用linux命令,GitHub - rinetd/linux-command: Linux命令大全搜索工具,内容包含Linux命令手册、详解、学习、搜集。...

    Linux Command 516 个 Linux 命令大全,内容包含 Linux 命令手册.详解.学习,值得收藏的 Linux 命令速查手册.请原谅我写了个爬虫,爬了他们家的数据linuxde.ne ...

  5. 实验楼linux命令,GitHub - encorechow/linux-command: Linux命令大全搜索工具,内容包含Linux命令手册、详解、学习、搜集。...

    Linux Command 516 个 Linux 命令大全,内容包含 Linux 命令手册.详解.学习,值得收藏的 Linux 命令速查手册.请原谅我写了个爬虫,爬了他们家的数据linuxde.ne ...

  6. spark常用的linux命令,GitHub - Sparkinzy/linux-command: Linux命令大全搜索工具,内容包含Linux命令手册、详解、学习、搜集。...

    Linux Command 516 个 Linux 命令大全,内容包含 Linux 命令手册.详解.学习,值得收藏的 Linux 命令速查手册.请原谅我写了个爬虫,爬了他们家的数据linuxde.ne ...

  7. 【学习笔记】sed 命令及参数详解

    sed 命令及参数详解 标签:Sed 文章目录 sed 命令及参数详解 一.简介 二.基本用法 2.1. 三种方式 2.2. 常用选项 2.3. 命令体的组成 2.3.1. 定位文本部分 2.3.2. ...

  8. Latex技巧:LaTex插图命令includegraphics参数详解

    转自:https://www.cnblogs.com/yifdu25/p/8326025.html Latex技巧:LaTex插图命令includegraphics参数详解 Latex插图的命令是\i ...

  9. Linux中ls -l命令展示信息详解

    Linux中ls -l命令展示信息详解 在Linux系统中,ls -l命令是用以列表的形式来查看当前目录下的文件或目录,下面就让我们一起来看看展示的信息是什么意思吧 以CentsOS7为例子 [roo ...

  10. ownCloud/Nextcloud OCC命令行工具详解

    OCC命令行工具详解 ownCloud/Nextcloud OCC命令行工具详解(1) ownCloud/Nextcloud OCC命令行工具详解(2) ownCloud/Nextcloud OCC命 ...

最新文章

  1. 作业综合练习-初始化工作
  2. python中的魔法参数:*args和**kwargs
  3. WCF,Net remoting,Web service概念(转)
  4. 双一流大学毕业的我,应该何去何从?
  5. C#如何用Graphics画出一幅图表
  6. 《Python编程从入门到实践》记录之Python处理CSV文件数据
  7. C# 数组增加元素_C语言数组——任意位置插入一个元素
  8. 明明有印象却找不到,APP内搜索为什么这么难用?
  9. nodejs 批处理执行 app.js
  10. Mac端SVN工具CornerStone详解
  11. spark的集群安装
  12. 质性数据分析软件NVivo教程:自动编码情绪如何运作?
  13. Mac版idea快速切换大小写快捷键
  14. 「新功能」对接金蝶云星空K3 Cloud插件支持版本升级
  15. 什么是BSIC及其在GSM系统中的应用
  16. iOS SpriteKit 小游戏开发实例 - Flappy Bird
  17. 线性代数学习笔记(二十八)——齐次方程组的解
  18. 电脑 桌面图标上多了一个白色的文档图标 怎么去掉?
  19. A-Playing with Paper
  20. 神经网络衣服分类器详解(Fashion-MNIST数据集)

热门文章

  1. 【Spring】SpringMVC之REST编程风格
  2. ural1297 后缀数组+RMQ
  3. windows下的C/C++精确计时
  4. IT项目管理的十六个字心得体会
  5. C# Redis实战(七)
  6. Ajax,再生还是幻灭---好文推荐
  7. Ubuntu单用户修改root密码
  8. XCTF-高手进阶区:ics-06
  9. oracle脂肪分析仪,CEM推出油脂快速分析新技术
  10. HTML5语义化的理解