Shell指令碼第二篇(find)

來源:互聯網
上載者:User

Shell指令碼第二篇(find) 001#cat 命令002cat -n /usr/local/php/lib/php.ini  #顯示添加行號003cat -s  cat.txt    #壓縮多個空行為一個空行004 005#find 命令006 007find  /home/wangdk -iname "*.txt"  -print    # -print 指名列印匹配檔案名稱,分行符號\n  -i 忽略大小寫008 009find  . \( -name "*.txt" -o -name "*.pdf" \) -print  # -o 是邏輯或010 011# -name  對一個檔案名稱進行匹配012# -path 對一個檔案路徑進行匹配013# -regex 用一個Regex匹配檔案路徑014 015#支援正則016# find . -regex ".*\(\.sh\)$"017 018#否定參數019find . ! -name "*.txt" -print020 021#基於目錄的深度搜尋022#maxdepth 最大搜尋深度023#mindepth  最小搜尋深度024 025find . -maxdepth 1 -type f -print026find . -mindepth 2 -type f -print027 028# type  f  普通檔案029# type  l   連結符號030# type  d 目錄031#type  c  字元裝置032#type  b  塊裝置033#type  s  通訊端034#type p  Fifo035 036 037#根據檔案時間進行搜尋038# 訪問時間 -atime039# 修改時間 -mtime040# 變化時間 -ctime041 042# -atime -mtime -ctime 單位為天,支援 + - 大於,小於043 044#列印出最近7天內被訪問過的所有檔案045find . -type  f -atime  -7 -print046 047#列印出正好7前內訪問過的檔案048find . type f -atime 7 -print049 050#超過7天051find . -type f -atime  +7 -print052 053#基於時間搜尋,分鐘054# -amin -mmin -cmin055 056#基於檔案大小的搜尋057find . -type f -size +2k   #大於2k的檔案058 059find . -type f -size -2k   #小於2k的檔案060 061# 除了k 以外還有其他單位062# b   塊(512位元組)063# c   位元組064# w   字065# k   KB066# M  MB067#G  GB068 069#刪除匹配的檔案070 071find  . -type f -name "*.swp" -delete072 073#基於檔案許可權的匹配074find  .-type f -perm 644 -print075 076# 以apache 為例子,web伺服器上的PHP需要合適的執行許可權,我們可以用下面來搜尋077find . -type f -name "*.php" | -perm 644 -print078 079#基於檔案所有者的檔案080find . -type f -user wangdk -print081 082#結合find 執行命令或動作083find . -type -user root  -exec chown wangdk {} \;  #在這個命令中{}是一個特殊字元串與 -exec 選項結合是使用,對每個匹配的檔案替換成相應的檔案, 例如084# find 命令找到test1.txt 和 test2.txt 其所有者均為wangdk 那麼find 將會執行 chown wangk{} 他會被解析為  chown wangdk test1.txt  chown wangdk test2.txt085 086find . -type f -name "*.c" -exec cat {} \; > all_c._files.txt087 088#-exec 之後可以接任何命令 { }表示一個匹配,匹配任意檔案名稱089 090find . -type f  -mtime  +10 -a "*.txt" -exec cp { } OLD \;091 092# exec 無法執行多個命令,但是可以執行shell指令碼  -exec ./commands.sh { } \;093 094find . -type f -name "*.txt" -exec printf "text file: %s\n" { } \;095# -exec 能夠同printf結合起來產生有用的輸出資訊096 097# 讓 find 跳過指定目錄098find deve1//source_path \ ( -name ".git" -prune \) -o \ (-type f -print \)099#不使用 \( -type -print \) 二十選擇需要的過濾器100#這裡,\( -name ".git -prune \) 的作用是用於進行排序,她只ing了.git目錄應該被排斥外,而\( -type f -print \) 只指名了需要執行的動作 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.