yhara.jp

Recent Posts

ファイルの多いディレクトリを表示するRubyスクリプト

2023-10-26
Tech

NASを買い替えたので先代から中身をコピーしたのだけど、改めて思ったのがどうでもいいファイルの多さだ。昔はマシンを捨てるときに何があとで必要になるかよくわかってなかったので、ユーザディレクトリを丸ごとバックアップしたりしてたのだけど、そのせいでrubyのインストールファイル(だいたい1.8.x)があちこちに点在している。

NASは故障に備えて定期的に普通のHDDにsyncしているのだけど、こういう細かいファイルが大量にあるせいで、毎回無駄に時間がかかっている。ということで、Rubyスクリプトを書いて「ファイル数が多いディレクトリ」を調べてみた。

TH = 1000
$stdout.sync = true
def count(dir)
  n = 0
  Dir.each_child(dir) do |child|
    n += 1
    newpath = "#{dir}/#{child}"
    if File.directory?(newpath)
      n += count(newpath)
    end
  end
  if n > TH
    puts "#{n} #{dir}"
  end
  n
end
root = ARGV[0] or raise "usage: #$0 /some/folder"
count(root)

こういうのがさらっと書けるとQOLが高まってよい。

More posts

Posts

(more...)

Articles

(more...)

Category

Ads

About

About the author
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy