I made a script that wraps the du command with ruby.
Create the name ʻexpand.rb`, paste the script below, and execute it as follows.
# ruby expand.rb (The path you want to find)
$ ruby expand_du.rb /var/lib/
7GB /var/lib/
3GB /var/lib/jenkins
2GB /var/lib/docker
1GB /var/lib/mysql
...
4KB /var/lib/mysql-files
du_-k_--max-depth_1_var_lib__20200516231438.Saved in log
Scripts (github)
#How to use
# ruby expand_du.rb path
#Example: ruby expand_du.rb ~
#
#effect
# -Execute the du command for the path
# -Output execution result to standard output and file
#
#argument
# -path:Target path to execute the du command
require 'rbconfig'
def main(target_path)
return puts "ArgumentError:Please put the path as an argument" if target_path.nil?
max_depth_option_str = if os == :macosx
"-d"
else
"--max-depth"
end
exec_command = "du -k #{max_depth_option_str} 1 #{target_path}"
du_result_str = `#{exec_command}`
return if du_result_str.empty?
output_disksizes(du_result_str, exec_command)
end
def output_disksizes(du_result_str, exec_command)
disk_usages = du_result_str
.split("\n")
.map{|du_result| DiskUsage.new(du_result)}
.sort{|x, y| x.size <=> y.size}.reverse
output_filename = "#{exec_command.gsub(/( |\/){1,}/, "_")}_#{Time.new.strftime("%Y%m%d%H%M%S")}.log"
output_file = File.open(output_filename, "w")
disk_usages.each do |disk_usage|
puts disk_usage.to_s
output_file.puts(disk_usage.to_s)
end
output_file.close
puts "#{output_filename}Saved in"
end
class DiskUsage
attr_reader :size, :path
def initialize(du_result_line)
du_result_params = du_result_line.split(" ").map(&:strip)
@size = du_result_params[0].to_i
@humanreadable_size, @humanreadable_unit = calc_humanreadable_size
@path = du_result_params[1]
end
def to_s
#NOTE Specify 5 digits for the time being. Increase when needed
"#{sprintf("%5d" % @humanreadable_size)}#{@humanreadable_unit} #{@path}"
end
def humanreadable_size_with_unit
"#{@humanreadable_size}#{@humanreadable_unit}"
end
private
def calc_humanreadable_size
return [@size, "KB"] if mb_size < 1
return [mb_size, "MB"] if gb_size < 1
return [gb_size, "GB"] if tb_size < 1
[tb_size, "TB"]
end
def kb_size
@size
end
def mb_size
kb_size.fdiv(1024).round(1)
end
def gb_size
mb_size.fdiv(1024).round(1)
end
def tb_size
gb_size.fdiv(1024).round(1)
end
end
def os
case RbConfig::CONFIG['host_os']
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
:linux
when /solaris|bsd/
:unix
else
:unknown
end
end
When your rental server or Mac is full, you'll want to know which folder is the cause.
Therefore, if you are using a Mac, you can see the storage usage status, and even if you are using a rental server, you can see how much you are using with the df command. However, I can only understand the whole thing and not know which folder is the cause.
Therefore, the du command can be used by those who can use the terminal (the ls command is also a candidate, but the ls command knows the size of the file directly under it, but not how big the folder is).
However, this du command is a bit unsatisfactory, probably because it is a UNIX command.
To make it easier to read, I use the sort command with the -h option to sort the sizes in descending order, but then 5KB comes above 4GB. I can't help because I'm only looking at the numbers.
If you use du, it will output the result to the standard output, but after you do, you will want to record the result. You can output the file from the beginning, but it is troublesome to output the file and then cat. If you devise a command, it will be output to both a file and standard output, but when you need the du command, you are usually in a hurry, so I wanted to do it easily.
The script that solved the problem above is the first script posted.
The features are as follows.
――It sorts after considering the unit --It outputs both standard output and file (& outputs with a file name that shows which folder was searched)
I made it completely for myself, but if anyone is having trouble with the same thing, please use it.
Recommended Posts