如何找到大於/小於 x 位元組的檔案?

如何找到大於/小於 x 位元組的檔案?

在終端機中,如何找到大於或小於 x 位元組的檔案?

我想我可以做類似的事情

find . -exec ls -l {} \;

然後將結果透過管道傳輸到awk按檔案大小過濾。但不應該有比這更簡單的方法嗎?

答案1

使用:

find . -type f -size +4096c

尋找大於 4096 位元組的檔案。

和 :

find . -type f -size -4096c

尋找小於 4096 位元組的檔案。

注意大小切換後的 + 和 - 差異。

開關-size解釋:

-size n[cwbkMG]

    File uses n units of space. The following suffixes can be used:

    `b'    for 512-byte blocks (this is the default if no suffix  is
                                used)

    `c'    for bytes

    `w'    for two-byte words

    `k'    for Kilobytes       (units of 1024 bytes)

    `M'    for Megabytes    (units of 1048576 bytes)

    `G'    for Gigabytes (units of 1073741824 bytes)

    The size does not count indirect blocks, but it does count
    blocks in sparse files that are not actually allocated. Bear in
    mind that the `%k' and `%b' format specifiers of -printf handle
    sparse files differently. The `b' suffix always denotes
    512-byte blocks and never 1 Kilobyte blocks, which is different
    to the behaviour of -ls.

答案2

我認為find單獨使用可能很有用,無需透過管道傳輸到 AWK。例如,

find ~ -type f -size +2k  -exec ls -sh {} \;

波形符表示您希望搜尋開始的位置,結果應僅顯示大於 2 KB 的檔案。

為了使它更有趣,您可以使用該-exec選項執行另一個命令,該命令列出這些目錄及其大小。

欲了解更多信息,請閱讀手冊頁find

答案3

AWK 對於這類事情確實很容易。正如您所問的,您可以使用它來執行以下與檔案大小檢查相關的操作:

列出大於 200 位元組的檔案:

ls -l | awk '{if ($5 > 200) print $8}'

列出小於 200 位元組的檔案並將清單寫入檔案:

ls -l | awk '{if ($5 < 200) print $8}' | tee -a filelog

列出0位元組的文件,將清單記錄到文件中並刪除空文件:

ls -l | awk '{if ($5 == 0) print $8}' | tee -a deletelog | xargs rm

答案4

使用fd,這比使用好得多find

fd -S +1g

將在目前目錄下搜尋大於 1GB 的文件

fd -S -1g

將搜尋文件較小大於1GB

相關內容