let's shell---Find 指令用法

来源:互联网 发布:win7电脑摄像头软件 编辑:程序博客网 时间:2024/06/03 14:07

以前学习shell命令的时候感觉find指令全盘搜索,简直要命所以就没怎么关注这个,没想到在工作还是用到了。。。可见理论和实践还是差距很大的

1. 找出 . 底下的 php 檔案

find . -name "*.php"

2. 找出 . 底下非 php 副檔名檔案

find . -not -name "*.php"
3. 刪除 . 底下 php 檔案,有兩種作法

# 系統詢問之後才刪除# 先把 -exec 後面的東西先清掉, 用 -print 來先確認輸出# rm 可以多用 -i 的參數來加以確認find . -name "*.php" -exec rm -i {} \;## 系統直接刪除find . -delete -name "*.php"find . -name "*.php" | xargs /bin/rm -rf
4. 如何刪除 7 天前之料呢?

find /path_name -type f -mtime +7 -exec rm '{}' \;find /path_name -type f -mtime +7  | xargs /bin/rm -rffind /path_name -delete -type f -mtime +7
5. 找出7天以內修改的資料

find . -type f -mtime -7 -name "*.php"
6. find 後只顯示目錄名稱不顯示路徑

find . -maxdepth 1 -type d  -exec basename {} \;find . -maxdepth 1 -type d | awk -F"/" '{print $NF}'find . -maxdepth 1 -type d | sed 's!.*\/\([^\/]*\).*!\1!g'
7. find 後只顯示目錄名稱不顯示路徑,也不顯示第一個 . 目錄
find . -maxdepth 1 -mindepth 1 -type d -exec basename {} \;
8. -mmin (minutes) or -mtime (24 hour periods, starting from now) For example:
find . -mtime 0   # find files modified between now and 1 day ago                  # (i.e., within the past 24 hours)find . -mtime -1  # find files modified less than 1 day ago                  # (i.e., within the past 24 hours, as before)find . -mtime 1   # find files modified between 24 and 48 hours agofind . -mtime +1  # find files modified more than 48 hours ago find . -mmin +5 -mmin -10 # find files modifed between                          # 6 and 9 minutes ago