簡明Linux命令列筆記:find

來源:互聯網
上載者:User
文章目錄
  • 參數
  • 條件
  • 樣本1 (以屬性尋找)
  • 樣本2 (以時間尋找)
  • 樣本3 (以檔案類型尋找)
  • 樣本4 (串連符)
  • Tips

根據條件尋找檔案

find [directory-list] [expression]

 

參數

directory-list用來指定find要搜尋的目錄階層或範圍

當沒有指定directory-list時,find將搜尋當前工作目錄的階層

 

條件

在expression中可以使用的條件很多,我把它們分類寫出來了

其中部分條件包含+-n,意思是:

+n         比n大

-n          比n小

 n          正好是n

 

以檔案屬性尋找

-user name                       尋找所有者為name的檔案

-group name                     尋找檔案所在組為name的檔案

-name filename                 尋找檔案名稱為filename的檔案

-nogroup                           尋找不屬於任意群組的檔案

-nouser                             尋找不屬於任意使用者的檔案

-perm mode                      依據檔案許可權尋找檔案

-inum n                              尋找節點為n的檔案

-links +-n                           尋找檔案連結數+-n的檔案

-size +-n[c|k|M|G]              依據檔案大小來尋找檔案,預設大小是+-n倍512位元組的塊,或使用Kb、Mb、Gb為單位

 

以時間單位尋找

時間單位後面接+-n,表示多少(天,分鐘)之內/外,下面是6種時間單位

天:ctime   atime   mtime

分鐘:cmin   amin   mmin

那,上面的c   a    m代表什麼意思呢

c                  change改變 ,表示檔案屬性被修改過,包括所有者、群組、許可權等(也就是ls -l能看到的內容)

a                  access訪問

m                 modify修改,表示檔案內容被修改

 

以檔案類型尋找

-type filetype                   如果檔案類型為filetype,則檔案滿足尋找條件,檔案類型包括:

                                        b                  特殊的塊檔案

                                        c                  特殊的字元檔案

                                        d                  目錄檔案

                                        f                   普通檔案

                                        l                   符號連結

 

串連符

-a                                    and邏輯與

-o                                    or邏輯或

-exec command { } \;        此處是固定格式,表明尋找到檔案後執行某些操作

                                         { } 代表find中尋找的結果

                                         \ 代錶轉義符,使用命令本身的命令(忽略別名)

                                         ; 代表整條語句結束

-ok                                    詢問是否執行操作,用法同exec 

 

樣本1 (以屬性尋找)find -user
$ find /etc -user siu/etc/firefox-3.0/etc/firefox-3.0/pref/etc/firefox-3.0/pref/apturl.js

根據所有者來尋找檔案

 

find -perm
$ find /etc -perm 600/etc/ppp/chap-secrets/etc/ppp/pap-secrets/etc/passwd-/etc/group-/etc/gshadow-/etc/.pwd.lock/etc/shadow-/etc/apt/trustdb.gpg/etc/default/cacerts/etc/security/opasswd/etc/mysql/debian.cnf/etc/mtab.fuselock

根據檔案許可權來尋找檔案

 

find -name
$ find /etc -name init/etc/init/etc/apparmor/init$ find /etc -name init*/etc/init/etc/apparmor/init/etc/initramfs-tools/etc/initramfs-tools/scripts/init-premount/etc/initramfs-tools/scripts/init-bottom/etc/initramfs-tools/scripts/init-top/etc/initramfs-tools/initramfs.conf/etc/bash_completion.d/initramfs-tools/etc/init.d/etc/kernel/postinst.d/initramfs-tools/etc/kernel/postrm.d/initramfs-tools/etc/wireshark/init.lua

根據檔案名稱全名尋找,如果不是全名可以使用萬用字元*或?

 

find -nogroup -nouser
$ find /etc -nogroup -nouser$ 

尋找沒群組沒使用者的檔案,此處木有

 find -size
$ find /etc/ -size +1M/etc/brltty/zh-tw.ctb/etc/skel/.mozilla/firefox/mwad0hks.default/places.sqlite/etc/skel/.config/chromium/Safe Browsing Bloom

尋找指定單位大小的檔案,預設的單位是512位元組的資料區塊block,很不方便

此處以M為單位,尋找大於1M的檔案

 

樣本2 (以時間尋找)find -mmin
$ find /etc -mmin -120 /etc/cups/etc/cups/subscriptions.conf.O/etc/cups/subscriptions.conf

尋找兩個小時內被修改過內容的檔案

 

find -mtime
$ find /etc -mtime -2/etc/etc/mtab/etc/update-motd.d/10-help-text/etc/gnome/defaults.list/etc/cups

尋找兩天內被修改過內容的檔案

 

樣本3 (以檔案類型尋找)find -type
$ find /etc -type d/etc/etc/at-spi2/etc/purple/etc/ndiswrapper/etc/init/etc/ImageMagick/etc/gnome-app-install

尋找/etc目錄下檔案類型是目錄的檔案

Tips:Linux中的一切都是檔案,包括目錄

 

樣本4 (串連符)-a    邏輯與
$ find /etc -name init* -a -type f/etc/initramfs-tools/initramfs.conf/etc/bash_completion.d/initramfs-tools/etc/kernel/postinst.d/initramfs-tools/etc/kernel/postrm.d/initramfs-tools/etc/wireshark/init.lua

尋找/etc目錄下名為init開頭並且檔案類型是普通的檔案

 

-p   邏輯或
$ find /etc -name system -o -name init/etc/init/etc/apparmor/init/etc/systemd/system

查看/etc目錄下名為system或init的檔案

 

find -exec
$ find /etc -name system -exec ls -l {} \;總用量 4drwxr-xr-x 2 root root 4096 12月  9 21:49 multi-user.target.wants

尋找名為system的檔案並使用ls -l顯示其詳細資料

 

find -ok
$ ls -i672684 aa$ find ./ -inum 672684 -ok rm {} \;< rm ... ./aa > ? y$ ls -i$ 

在本目錄下尋找指定節點的檔案,並詢問是否刪除

Tips:-ok和-exec不一樣的地方就是,一個詢問後執行,另一個直接執行

          假如部分Linux發行版設定了別名rm=‘rm -i’,那麼此處 \ 轉義符可還原rm本身的命令,此處是固定用法,潛規則

 

 

Tips

1.find命令異常強大,可以設定很多限制條件進行搜尋,具體查看man頁

2.使用find命令最好先指定尋找範圍,範圍越小越好。同時,條件越精確效率越高

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.