Skip to content

Commit

Permalink
Utilities/du: Add an option to print the total size in the end
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
supercomputer7 authored and timschumi committed Sep 5, 2024
1 parent c23005a commit 3e7eb30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions Base/usr/share/man/man1/du.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion Userland/Utilities/du.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3e7eb30

Please sign in to comment.