(70) findでサイズ0のファイルを検出

投稿者: | 2017年12月18日

11,049 views

この記事は最終更新から 942日 が経過しています。

(1) やりたいこと

「ファイルサイズ=0」のファイルを検出し、削除したい。

(2) 実現方法

findコマンドを sizeオプション指定で実行する。
検出したファイルを rmコマンドで削除する。

find . -type f -size 0 | xargs rm

xargsを使わず execオプションで rmを実行してもよい。

find . -type f -size 0 -exec rm {} \;

{} が検出したファイルパスに置き換わる。
最後は \; で閉じる。

参考までに、manのオプション説明の該当箇所は以下の通り。

-size n[cwbkMG]
  File uses n units of space, rounding up.  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.  The + and - prefixes signify
  greater than and less than, as usual, but bear in mind that the size is rounded up to the next unit (so a  1-byte  file  is
  not matched by -size -1M).

その他

sizeオプションは > と < も指定できる。

1) 30[KB]を超えるファイルを検索

find . -type f -size +30k

2) 10[MB]未満のファイルを検索

find . -type f -size -10M

カテゴリー: find

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です