Utilities/du: Add an option to print the total size in the end

This is useful when using du on many files and wanting to get a total
size count in the end after iterating on all the directories and files.
This commit is contained in:
Liav A. authored and Tim Schumacher committed 2024-09-05 16:36:12 +02:00
1 parent c23005a3f0
commit 3e7eb30466
2 files changed
+15 -1

No files matched your search

+1
View File
@@ -16,6 +16,7 @@ $ du [files...]
* `-a`, `--all`: Write counts for all files, not just directories
* `--apparent-size`: Print apparent sizes, rather than disk usage
* `-c`, `--total`: Print total size in the end
* `-h` , `--human-readable`: Print human-readable sizes
* `--si`: Print human-readable sizes in SI units
* `-d N`, `--max-depth N`: Print the total for a directory or file only if it is N or fewer levels below the command line argument
+14 -1
View File
@@ -30,6 +30,7 @@ struct DuOption {
bool all = false;
bool apparent_size = false;
bool one_file_system = false;
bool print_total_size = false;
TimeType time_type = TimeType::NotUsed;
Vector<ByteString> excluded_patterns;
u64 block_size = 1024;
@@ -68,8 +69,19 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(parse_args(arguments, files, du_option));
size_t total_size_count = 0;
for (auto const& file : files)
print_space_usage(file, du_option, 0);
total_size_count += print_space_usage(file, du_option, 0);
if (du_option.print_total_size) {
if (du_option.human_readable) {
outln("{}\ttotal", human_readable_size(total_size_count));
} else if (du_option.human_readable_si) {
outln("{}\ttotal", human_readable_size(total_size_count, AK::HumanReadableBasedOn::Base10));
} else {
outln("{}\ttotal", total_size_count);
}
}
return 0;
}
@@ -120,6 +132,7 @@ ErrorOr<void> parse_args(Main::Arguments arguments, Vector<ByteString>& files, D
args_parser.set_general_help("Display actual or apparent disk usage of files or directories.");
args_parser.add_option(du_option.all, "Write counts for all files, not just directories", "all", 'a');
args_parser.add_option(du_option.apparent_size, "Print apparent sizes, rather than disk usage", "apparent-size");
args_parser.add_option(du_option.print_total_size, "Print total count in the end", "total", 'c');
args_parser.add_option(du_option.human_readable, "Print human-readable sizes", "human-readable", 'h');
args_parser.add_option(du_option.human_readable_si, "Print human-readable sizes in SI units", "si");
args_parser.add_option(du_option.max_depth, "Print the total for a directory or file only if it is N or fewer levels below the command line argument", "max-depth", 'd', "N");