Linux下find命令在目录结构中搜索文件,并执行指定的操作。

    (1)用法:

用法: find pathname    -option      [-print | -exec | -ok]

        find 路径名           选项参数    [-print | -exec | -ok]

    (2)功能:

功能:用于在文件树种查找文件,并作出相应的处理。

    (3)命令参数:

1) pathname:     find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。

2) -print:       find命令将匹配的文件输出到标准输出。

3) -exec:       find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;注意{ }和\;之间的空格。

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

    (4)选项参数:

1) -name              按照文件名查找文件。

2) -perm              按照文件权限来查找文件。

3) -prune             使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth    选项,那么-prune将被find命令忽略。

    4) -user            按照文件属主来查找文件。

    5) -group          按照文件所属的组来查找文件。

    6) -mtime  -n +n      按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。

7) -nogroup        查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

8) -nouser                   查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

9) -newer file1 ! file2    查找更改时间比文件file1新但比文件file2旧的文件。

10) -empty         查找长度为0的文件或文件夹

11) -type          查找某一类型的文件,诸如:

    b                              -块设备文件。

d             - 目录。

c               - 字符设备文件。

p             - 管道文件。

l            - 符号链接文件。

f            - 普通文件。

12) -size n:           [c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。

   13) -depth:           在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

14) -fstype:          查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

15) -mount:          在查找文件时不跨越文件系统mount点。

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

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

18) -regex<范本样式>:      指定字符串作为寻找文件或目录的范本样式

另外,下面三个的区别:

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

-atime n  查找系统中最后n*24小时访问的文件

-cmin n   查找系统中最后N分钟被改变文件状态的文件

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

-mmin n   查找系统中最后N分钟被改变文件数据的文件

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

(5)实例:

1)[root@localhost findDir]# find .                          列出当前目录及子目录下所有文件和文件夹

[root@localhost Documents]# touch ./findDir/t1.txt //在当前目录的子目录findDir目录下创建t1.txt文本文件
[root@localhost Documents]# cd findDir
[root@localhost findDir]# ll
总用量
-rw-r--r--. root root 5月 : t1.txt
[root@localhost findDir]# find .
.
./t1.txt
[root@localhost findDir]# cd ../
[root@localhost Documents]# find .
.
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir
./findDir/t1.txt
[root@localhost Documents]#

用ls命令列出当前目录及子目录下所有文件和文件夹:

[root@localhost Documents]# ls -Rl
.:
总用量
drwxr-xr-x. root root 5月 : findDir
-rw-r--r--. root root 5月 : head_text
-rw-r--r--. root root 5月 : less1
-rw-r--r--. root root 5月 : less2
-rw-r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : tail_text
-rw-r--r--. root root 5月 : tempory
-rw-r--r--. root root 5月 : uText ./findDir:
总用量
-rw-r--r--. root root 5月 : t1.txt

2)[root@localhost /]# find /home/sunjimeng/Documents -name "*.txt"         在特定目录下找到以.txt结尾的文件

[root@localhost /]# find /home/sunjimeng/Documents -name "*.txt"
/home/sunjimeng/Documents/findDir/t1.txt

3)[root@localhost /]# find /home/sunjimeng/Documents -iname "T*"     在特定目录下找到以T开头的文件或文件夹,但忽略大小写(-i操作)

[root@localhost /]# find /home/sunjimeng/Documents -name "T*"                                       //不忽略
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
[root@localhost /]# find /home/sunjimeng/Documents -iname "T*"                      //忽略大小写
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt

4)[root@localhost /]# find /home/sunjimeng/Documents \( -name "*.txt" -o -name "*.pdf" \)  用两个条件来查询文件

[root@localhost /]# touch /home/sunjimeng/Documents/findDir/{p1.pdf,p2.pdf}
[root@localhost /]# find /home/sunjimeng/Documents \( -name "*.txt" -o -name "*.pdf" \)
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
[root@localhost /]# cd /home/sunjimeng/Documents/findDir
[root@localhost findDir]# find . -name "*.txt" -o -name "*.pdf"
./t1.txt
./T1.txt
./T2.txt
./p1.pdf
./p2.pdf

5)[root@localhost /]# find /home/sunjimeng/Documents ! -name "*.txt"     找寻目录下不是以.txt结尾的文件或目录

[root@localhost /]# find /home/sunjimeng/Documents ! -name  "*.txt"
/home/sunjimeng/Documents
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf

6)[root@localhost sunjimeng]# find /home/ -path "*cume*"           在指定目录下的后代目录中进行路径匹配,匹配完成后列出匹配目录下的所有文件及文件夹及其子目录及文件。

[root@localhost sunjimeng]# find /home/ -path "*cume*"           //等价于find /home -path "*cume*"
/home/sunjimeng/Documents
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
/home/sunjimeng/Document
/home/sunjimeng/Document/newDir
/home/sunjimeng/Document/newDir/text1
/home/sunjimeng/Document/newDir/text2
/home/sunjimeng/Document/text1
/home/sunjimeng/Document/text1/newDir
/home/sunjimeng/Document/text1/newDir/text1
/home/sunjimeng/Document/text1/newDir/text2
/home/sunjimeng/Document/text2
/home/sunjimeng/Document/text2/newDir
/home/sunjimeng/Document/text2/newDir/text1
/home/sunjimeng/Document/text2/newDir/text2
/home/sunjimeng/Document/text3
/home/sunjimeng/Document/text3/text1
/home/sunjimeng/Document/text3/text2
/home/sunjimeng/Document/text4
/home/sunjimeng/Document/text4/text1
/home/sunjimeng/Document/text4/text2
/home/sunjimeng/Document/mytext
/home/sunjimeng/Document/mytext.txt

7)[root@localhost /]# find /home/sunjimeng -regex ".*\(txt\|pdf\)$"            进行字符串匹配

[root@localhost /]# find /home/sunjimeng -regex ".*\(\.txt\|\.pdf\)$"               //原始
[root@localhost /]# find /home/sunjimeng -regex ".*\(.txt\|.pdf\)$" //去掉两个斜杠
[root@localhost /]# find /home/sunjimeng -regex ".*\(txt\|pdf\)$"           //把点去了
/home/sunjimeng/.cache/tracker/db-version.txt
/home/sunjimeng/.cache/tracker/db-locale.txt
/home/sunjimeng/.cache/tracker/miner-applications-locale.txt
/home/sunjimeng/.cache/tracker/last-crawl.txt
/home/sunjimeng/.cache/tracker/first-index.txt
/home/sunjimeng/.local/share/Trash/files/test1.txt
/home/sunjimeng/.local/share/Trash/files/test2.txt
/home/sunjimeng/.local/share/Trash/files/test3.txt
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
/home/sunjimeng/Document/mytext.txt

8)[root@localhost /]# find /home/sunjimeng/Documents -type d | p | f | c | l | b 查找特定类型的文件

[root@localhost /]# find /home/sunjimeng/Documents -type d
/home/sunjimeng/Documents
/home/sunjimeng/Documents/findDir
[root@localhost /]# find /home/sunjimeng/Documents -type p
[root@localhost /]# find /home/sunjimeng/Documents -type f
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf

9)[root@localhost /]# find /home -maxdepth 3 -type f              找指定目录下的普通文件,文件目录深度不超过3

[root@localhost /]# find /home -maxdepth  -type f
/home/sunjimeng/.bash_logout
/home/sunjimeng/.bash_profile
/home/sunjimeng/.bashrc
/home/sunjimeng/.config/user-dirs.dirs
/home/sunjimeng/.config/user-dirs.locale
/home/sunjimeng/.config/gnome-initial-setup-done
/home/sunjimeng/.cache/event-sound-cache.tdb.localhost.localdomain.x86_64-redhat-linux-gnu
/home/sunjimeng/.ICEauthority
/home/sunjimeng/.esd_auth
/home/sunjimeng/.bash_history
/home/sunjimeng/Documents/less1
/home/sunjimeng/Documents/less2
/home/sunjimeng/Documents/head_text
/home/sunjimeng/Documents/tail_text
/home/sunjimeng/Documents/tempory
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Document/mytext
/home/sunjimeng/Document/mytext.txt

10)[root@localhost /]# find /home/sunjimeng -mindepth 5 -type d                         找指定目录下的普通文件,文件目录深度不低于5

[root@localhost /]# find /home/sunjimeng -mindepth  -type d
/home/sunjimeng/.local/share/evolution/addressbook/trash
/home/sunjimeng/.local/share/evolution/addressbook/system
/home/sunjimeng/.local/share/evolution/addressbook/system/photos
/home/sunjimeng/.local/share/evolution/calendar/trash
/home/sunjimeng/.local/share/evolution/calendar/system
/home/sunjimeng/.local/share/evolution/mail/trash
/home/sunjimeng/.local/share/evolution/memos/trash
/home/sunjimeng/.local/share/evolution/tasks/trash
/home/sunjimeng/.local/share/Trash/files/未命名文件夹

11)[root@localhost /]# find /home/sunjimeng/Documents -atime -2        查找指定时间内修改过的文件

[root@localhost /]# find /home/sunjimeng/Documents -atime -2          //两个以内
/home/sunjimeng/Documents
/home/sunjimeng/Documents/newlocate
/home/sunjimeng/Documents/uText
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf
[root@localhost /]# find /home/sunjimeng/Documents -atime -1         //一天以内
/home/sunjimeng/Documents
/home/sunjimeng/Documents/findDir
/home/sunjimeng/Documents/findDir/t1.txt
/home/sunjimeng/Documents/findDir/T1.txt
/home/sunjimeng/Documents/findDir/T2.txt
/home/sunjimeng/Documents/findDir/p1.pdf
/home/sunjimeng/Documents/findDir/p2.pdf

12)[root@localhost /]# find . -perm 777                      查找权限为777的文件及文件夹

[root@localhost /]# find . -perm 
[root@localhost /]# find /usr/libexec/gcc -perm
/usr/libexec/gcc/x86_64-redhat-linux/4.8./liblto_plugin.so
/usr/libexec/gcc/x86_64-redhat-linux/4.8./liblto_plugin.so.
/usr/libexec/gcc/x86_64-redhat-linux/4.8.

    (6)其他:

权限:

读取权限 r = 4

写入权限 w = 2

执行权限 x = 1

775这三个数字代表拥有者,组用户,其他用户的权限。

例如: 7 拥有者有 读取,写入,执行权限

      7 组用户有 读取,写入,执行权限

5 其他用户有 读取,执行权限(4+1 = 5)

777 与 775的区别是:其他用户有写入权限,而775的没有。

  linux下设置777权限和用户权限

  设置www目录的所有文件可写777sudo chmod 777 -R www  (这里-R是继承)

设置www目录下的所有文件权限为user所有

sudo chown -hR user www
exit

linux下账户的权限是:drwx------ 表示是那些权限?

首先d是代表这个是一个目录文件,rwx是属主权限rw代表可读写x代表可执行,后面三个---是属主同组的权限,如果是---代表没有权限,最后三个是除文件属主组以外所有人的权限,---也是没有权限,所以这个目录只有属主有权限进入,其他人连读的权限也没有,更不用说写入和执行。

每天一个Linux命令(19)find命令_初识的更多相关文章

  1. Linux环境及基础命令(一)

    Linux环境及基础命令 一.认识Linux系统 略 二.配置Linux系统远程登录 2.1虚拟机系统配置 2.11虚拟机配置 统一NAT模式 虚拟机连不上 确定VMnet8网卡的IP地址(每台虚拟机 ...

  2. 每天一个 Linux 命令(19):find 命令概览

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  3. 每天一个linux命令(19):find 命令概览

    Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时 ...

  4. 每天一个linux命令(19):Linux 目录结构

    对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下面 ...

  5. 每天一个Linux命令(19)--find命令

    linux 下 find 命令在目录结构中搜索文件,并执行指定的操作.Linux 下 find 命令提供了相当多的查找条件,功能很强大.由于 find 具有强大的功能,所以它的选项也很多,其中大部分选 ...

  6. 【转】每天一个linux命令(19):find 命令概览

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/13/2767374.html Linux下find命令在目录结构中搜索文件,并执行指定的操作.Li ...

  7. 每天一个Linux命令:locate(19)

    locate locate命令 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中 ...

  8. 【每天一个Linux命令】19. 创建文件夹目录命令mkdir

    命令用途 mkdir 命令用来创建指定的名称的目录 使用说明 1.  创建目录的用户在当前目录中具有写权限 2. 指定的目录名不能是当前目录中已有的目录. 命令实例 0. 帮助文件 bixiaopen ...

  9. 每天一个 Linux 命令(21):find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

随机推荐

  1. 【Shell】建立一个脚本统计当前登录用户数

    who命令 who命令是显示目前登陆系统的用户信息,执行who命令可以得知目前哪些用户登入系统,单独执行who命令会列出登入账号,使用的终端机,登入的时间以及从何处登入或正在使用哪个显示器. 统计用户 ...

  2. Oracle LOB字段判空

    dbms_lob.getlength() dbms_lob.getlength(null) 会报错--- Oracle 默认为clob字段插入empty_clob()

  3. rabbit mq 安装

    不知道为什么上不去erlang的官网,所以只能到中文社区来下载了. http://www.cnerlang.com/ 我下载的是下面这个版本 http://download.cnerlang.com/ ...

  4. spring集成PHPRPC及使用

    PHPRPC,它的商业版本是Hprose.这里仅记录其使用方法.其它相关内容可自行搜索. 对于开源的东西,建议大家看看其源码. 1.需要引入的jar包:phprpc_spring.jar,http:/ ...

  5. C# 接口的隐式与显示实现说明

    以前在用到接口时,从来没注意到接口分为隐式实现与显示实现.昨天在浏览博客时看到相关内容,现在根据自己的理解记录一下,方便日后碰到的时候温习温习. 通俗的来讲,"显示接口实现"就是使 ...

  6. Hibernate学习之属性级别注解

    © 版权声明:本文为博主原创文章,转载请注明出处 属性级别注解 添加方式 1. 写在属性字段上面 2. 写在属性getter方法上面 @Id:必须,定义了映射到数据库表的主键属性,一个实体可以有一个或 ...

  7. angularjs中的时间格式化过滤

    本地化日期格式化: ({{ today | date:'medium' }})Nov 19, 2015 3:57:48 PM ({{ today | date:'short' }})11/19/15  ...

  8. instagram架构分析_转

    转自:http://www.eit.name/blog/read.php?504 Instagram 团队上个月才迎来第 7 名员工,是的,7个人的团队.作为 iPhone 上最火爆的图片类工具,in ...

  9. kafka官方Quick Start

    1.下载kafka,并上传到服务器 2.如果之前没安装zookeeper,这里可以启动一个简单的zookeeper bin/zookeeper-server-start.sh config/zooke ...

  10. 【JMeter4.0学习(四)】之JMeter对JMS性能测试脚本开发

    目录: 安装ActiveMQ并启动服务 JMeter对JMS点到点测试计划 JMeter JMS主题测试计划 附:相关学习地址 一.ActiveMQ官方下载地址:http://activemq.apa ...