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. hdu 神、上帝以及老天爷 java

    问题: 递推.可是a[i]=(a[i-1]+a[i-2])+(i-1)公式没有推出来. 在递推时,for循环约束值用的输入的m导致数组没有储存数. 在算阶乘时for循环中将i和j用混了,导致没有数输出 ...

  2. 应用程序之UIWebView的使用

    UIWebView简介 知识点总结 代码实现 一.UIWebView简介 1.是iOS内置的浏览器控件,可以浏览网页.打开文档等2.能够加载html/htm.pdf.docx.txt等格式的文件3.系 ...

  3. 数据库表设计时一对一关系存在的必要性 数据库一对一、一对多、多对多设计 面试逻辑题3.31 sql server 查询某个表被哪些存储过程调用 DataTable根据字段去重 .Net Core Cors中间件解析 分析MySQL中哪些情况下数据库索引会失效

    数据库表设计时一对一关系存在的必要性 2017年07月24日 10:01:07 阅读数:694 在表设计过程中,我无意中觉得一对一关系觉得好没道理,直接放到一张表中不就可以了吗?真是说,网上信息什么都 ...

  4. Spring 应用外部属性文件 配置 context 错误

    在Spring配置文件中出现通配符的匹配很全面, 但无法找到元素 'context:property-placeholder' 的声明这个错误,其实主要是我们在引入命名空间时没有正确引入它的DTD解析 ...

  5. bzoj 2458: [BeiJing2011]最小三角形 题解

    [前言]话说好久没有写题解了.到暑假了反而忙.o(╯□╰)o [原题] 2458: [BeiJing2011]最小三角形 Time Limit: 10 Sec  Memory Limit: 128 M ...

  6. asp.net core 系列之Response caching(1)

    这篇文章简单的讲解了response caching: 讲解了cache-control,及对其中的头和值的作用,及设置来控制response caching; 简单的罗列了其他的缓存技术:In-me ...

  7. Oracle Data Provider for .NET的使用(二)-驱动更换与注意事项

    上篇说过了ODP的安装与配置 ,但是个人比较喜欢托管类型的,毕竟非托管类型的,因为考虑到会有用户或者是服务器或者是开发人员有32位的机器,就要强制编译平台平台为32位,只因为这个驱动,有点让人不愉快了 ...

  8. web安全之SQL注入---第五章 如何预防SQL注入 ?

    5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a-zA-Z]{6,}$/5-1严格检查输入变量的类型和格式总结:其实就是做一些判断正则表达式:验证密码:/^[a ...

  9. Android SQLite基本用法

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...

  10. 如何更好的利用Node.js的性能极限

    通过使用非阻塞.事件驱动的I/O操作,Node.js为构建和运行大规模网络应用及服务提供了很好的平台,也受到了广泛的欢迎.其主要特性表现为能够处理庞大的并且高吞吐量的并发连接,从而构建高性能.高扩展性 ...