2010-06-08 37 views
4

我有一堆日誌文件,我必須刪除一些小尺寸的文件,這些文件是創建的錯誤文件。 (63字節)。 我只需要複製那些有數據的文件。刪除一定大小的文件

回答

18

Shell(linux);

find . -type f -size 63c -delete 

會遍歷子目錄(除非你告訴它,否則)

+0

〜unutbu添加了一個目錄是可能的,但不是必需的(默認爲工作目錄),儘管可以給出多條路徑的說明:'find ./foo/ bar ./foz ../../baz -type f'會一次搜索所有3個目錄。 – Wrikken 2010-06-08 00:38:21

+4

@Wrikken:並非所有版本的find都默認爲當前目錄。最好明確指定目錄,以避免以後出現意外失敗的命令。 – 2010-06-08 00:47:46

+2

啊?我不能問哪個版本/平臺?並不是說明確有什麼問題,特別是在刪除時,只是好奇而已。 – Wrikken 2010-06-08 00:53:25

10

既然你用「蟒蛇」標記你的問題在這裏是你如何能在語言做到這一點:

target_size = 63 
import os 
for dirpath, dirs, files in os.walk('.'): 
    for file in files: 
     path = os.path.join(dirpath, file) 
     if os.stat(path).st_size == target_size: 
      os.remove(path) 
5

Perl的一個班輪是

perl -e 'unlink grep {-s == 63} glob "*"' 

雖然,測試它會是一個好主意如果你想走路整個目錄樹

perl -le 'print for grep {-s == 63} glob "*"' 

,您將需要一個不同的版本:運行之前做

#find all files in the current hierarchy that are 63 bytes long. 
perl -MFile::Find=find -le 'find sub {print $File::Find::name if -s == 63}, "."' 

#delete all files in the current hierarchy that 63 bytes long 
perl -MFile::Find=find -e 'find sub {unlink if -s == 63}, "."' 

我使用需要$File::Find::name在發現版本等你拿整路徑,取消鏈接版本不需要它,因爲File::Find將目錄切換到每個目標目錄,並將$_設置爲文件名(-sunlink如何獲取文件名)。您可能還想查看grepglob

相關問題