码迷,mamicode.com
首页 > 系统相关 > 详细

每天一个Linux命令(19)find命令_初识

时间:2016-05-17 21:18:38      阅读:614      评论:0      收藏:0      [点我收藏+]

标签:

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

    

    (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
总用量 0
-rw-r--r--. 1 root root 0 5月  17 03:50 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
.:
总用量 20
drwxr-xr-x. 2 root root  19 5月  17 03:50 findDir
-rw-r--r--. 1 root root 664 5月   9 07:59 head_text
-rw-r--r--. 1 root root  45 5月   9 08:15 less1
-rw-r--r--. 1 root root  57 5月   9 08:16 less2
-rw-r--r--. 1 root root   0 5月  15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月  12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月  12 22:24 tempory
-rw-r--r--. 1 root root   0 5月  15 18:34 uText

./findDir:
总用量 0
-rw-r--r--. 1 root root 0 5月  17 03:50 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 3 -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 5 -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 777
[root@localhost /]# find /usr/libexec/gcc -perm 777
/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/liblto_plugin.so
/usr/libexec/gcc/x86_64-redhat-linux/4.8.2/liblto_plugin.so.0
/usr/libexec/gcc/x86_64-redhat-linux/4.8.3

      

    (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命令_初识

标签:

原文地址:http://www.cnblogs.com/MenAngel/p/5503020.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!