2016年11月9日 星期三
使用 find 尋找日期區間內的檔案
find 是 Unix/Linux 中好用的尋找檔案指令,本篇介紹如何使用 find 找出指定日期區間內的檔案。
使用 touch 建立一個修改日期為 2013-01-01 的檔案
touch --date "2013-01-01" /tmp/start.txt
使用 touch 建立一個修改日期為 2013-12-31 的檔案
touch --date "2013-12-31" /tmp/end.txt
尋找 /path 裡日期在 2013-01-01 與 2013-12-31 之間的檔案
find /path -type f -newer /tmp/start.txt -not -newer /tmp/end.txt
查看建立檔案的結果
ls --full-time end.txt
-rw-r--r-- 1 root root 0 2013-12-31 00:00:00.000000000 +0800 end.txt
若是要找的檔案要當年,你需要將日期延後一天
touch --date "2014-01-01" end.txt
或是加上時間
touch --date "2013-12-31 23:59:59" end.txt
查看建立檔案的結果
ls --full-time end.txt
-rw-r--r-- 1 root root 0 2013-12-31 23:59:59.000000000 +0800 end.txt
你也可以用
find /path -newermt "2013-01-01 00:00:00" ! -newermt "2013-01-01 23:59:59" -type f
RedHat 4.x, 5.5 沒有 newermt 這參數,RedHat 7.x 就有支援這參數了
參考來源:
https://goo.gl/Xl5Npb
http://liaozi.blogspot.tw/2014/11/find.html
使用 find 尋找日期區間內的檔案
find 是 Unix/Linux 中好用的尋找檔案指令,本篇介紹如何使用 find 找出指定日期區間內的檔案。
使用 touch 建立一個修改日期為 2013-01-01 的檔案
touch --date "2013-01-01" /tmp/start.txt
使用 touch 建立一個修改日期為 2013-12-31 的檔案
touch --date "2013-12-31" /tmp/end.txt
尋找 /path 裡日期在 2013-01-01 與 2013-12-31 之間的檔案
find /path -type f -newer /tmp/start.txt -not -newer /tmp/end.txt
查看建立檔案的結果
ls --full-time end.txt
-rw-r--r-- 1 root root 0 2013-12-31 00:00:00.000000000 +0800 end.txt
若是要找的檔案要當天,你需要將日期延後一天
touch --date "2014-01-01" end.txt
或是加上時間
touch --date "2013-12-31 23:59:59" end.txt
查看建立檔案的結果
ls --full-time end.txt
-rw-r--r-- 1 root root 0 2013-12-31 23:59:59.000000000 +0800 end.txt
你也可以用
find /path -newermt "2013-01-01 00:00:00" ! -newermt "2013-01-01 23:59:59" -type f
RedHat 4.x, 5.5 沒有 newermt 這參數,RedHat 7.x 就有支援這參數了
參考來源:
https://goo.gl/Xl5Npb
http://liaozi.blogspot.tw/2014/11/find.html
2013年3月1日 星期五
在 Linux 上找出空目錄, 而且刪掉它
偶爾你會需要刪除特定目錄下的空目錄,一個一個去找顯然不太聰明而且相當費時。
可以簡單的利用 find 來找,兩個參數可用,分別是 -depth 與 -empty。另外你可以加上 -type d 讓它只找目錄,不加的話,它會會連空檔案都找出來。
$ find /tmp -depth -empty -type d
配合 -exec 就可以將這些檔案全部刪掉啦。
$ find /tmp -depth -empty -type d -exec rmdir -v {} \;
以上文章摘自http://people.debian.org.tw/~chihchun/2010/04/15/find-the-empty-dir/
可以簡單的利用 find 來找,兩個參數可用,分別是 -depth 與 -empty。另外你可以加上 -type d 讓它只找目錄,不加的話,它會會連空檔案都找出來。
$ find /tmp -depth -empty -type d
配合 -exec 就可以將這些檔案全部刪掉啦。
$ find /tmp -depth -empty -type d -exec rmdir -v {} \;
以上文章摘自http://people.debian.org.tw/~chihchun/2010/04/15/find-the-empty-dir/
訂閱:
文章 (Atom)