歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 工作中常用的Linux命令(1):find命令,linuxfind

工作中常用的Linux命令(1):find命令,linuxfind

日期:2017/3/3 18:08:17   编辑:學習Linux

工作中常用的Linux命令(1):find命令,linuxfind

工作中常用的Linux命令(1):find命令,linuxfind


1.命令格式

  find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

2. 命令功能

  在文件目錄層級中查找文件並做相應的處理

3. 命令選項

  -name finename

    按照文件名查找文件,文件名可使用通配符

  -perm mode

    按照文件權限查找文件

  -type c

    按照文件類型查找文件,文件類型如下:

    b - 塊設備文件
    c - 字符設備文件
    d - 目錄文件
    p - 管道文件
    f  - 普通文件
    l  - 符號鏈接文件
    s - 套接字文件

  -size [+/-]n[cwbkMG]

    按照文件大小查詢文件,+n表示文件大小大於n,-n表示文件大小小於n

    文件大小單位如下:

     'b'  512-byte 的塊

     'c'  字節單位

     'w'  字單位

     'k'  KB(210 bytes)

     'M'  MB(220 bytes)

     'G'  GB(230 bytes)

  -gid n

    按照文件屬組ID查找文件

  -group gname

    按照文件屬組名查找文件,也可用文件屬組ID

  -uid n

    按照文件所有者ID查找文件

  -user uname

    按照文件所有者名查找文件,也可用文件所有者ID

  -amin n

    查找最後n分鐘被訪問過的文件

  -atime n

    查找最後n*24小時被訪問過的文件

  -cmin n

    查找最後n分鐘文件狀態被改變的文件

  -ctime n

    查找最後n*24小時文件狀態被改變的文件

  -mmin n

    查找最後n分鐘文件數據被修改過得文件  

  -mtime n

    查找最後n*24小時文件數據被修改過得文件

  -newer file

    查找文件修改時間比文件file新的文件

  -anewer file

    查找文件訪問時間比文件file新的文件

  -cnewer file

    查找文件狀態改變的時間比文件file新的文件

  查找到文件後的相應處理動作:

  -delete

    刪除查找到的文件

  -exec command {} \;

    對查找到的文件執行指定命令。{}和\;之間必須有空格,'\'為轉義字符

  -ok command {} \;

    用法同exec,但在執行命令前會進行確認

  -print

    將文件輸出到標准輸出

  -printf format

    將查找到的文件按照format格式輸出到標准輸出

4. 實例

  實例1:在當前目錄查找指定文件

[martin@localhost perl]$ find . -name "*.pl"
./ex3/ex3-1.pl
./ex3/ex3-3.pl
./ex3/ex3-2.pl
./ex4/ex4-1.pl

  實例2:查找當前目錄下的普通文件

[martin@localhost perl]$ find . -type f
./ex3/3-1.txt
./ex3/3-3.txt
./ex3/ex3-1.pl
./ex3/ex3-3.pl
./ex3/ex3-2.pl
./ex3/3-2.txt

  實例3:查找文件權限為775,並且文件大小超過4000byte的文件

[martin@localhost change]$ find . -perm 775 -size +4000c  
./dos2unix.pl
./test.pl
./space2tab.pl

  實例4:備份查找到的文件

[martin@localhost data]$ ll
total 8
-rw-rw-r--. 1 martin martin 7514 Aug 16 23:58 in4_G_002_224001_12345667789.s
[martin@localhost data]$ find . -name "*.s" -exec cp {} {}.old \;
[martin@localhost data]$ ll
total 16
-rw-rw-r--. 1 martin martin 7514 Aug 16 23:58 in4_G_002_224001_12345667789.s
-rw-rw-r--. 1 martin martin 7514 Nov 12 15:48 in4_G_002_224001_12345667789.s.old

  實例5:刪除查找到的文件前進行確認

[martin@localhost data]$ find . -name "*.old" -ok rm {} \;
< rm ... ./in4_G_002_224001_12345667789.s.old > ? y

工作中常用的Linux命令:目錄

http://xxxxxx/Linuxjc/1172228.html TechArticle

Copyright © Linux教程網 All Rights Reserved