--- Begin Message ---
Subject: |
add an option to du that allows to control which file types are counted |
Date: |
Wed, 01 Mar 2023 04:18:34 +0100 |
User-agent: |
Evolution 3.46.4-1 |
Hey.
When I want to count the nominal sizes of the (usually regular) files
in a directory I do something like:
du --apparent-size --block-size=1
This however also counts in the sizes of the directories themselves
(and I guess also of symlinks, etc.).
The "problem" with that is in particular, that for the exact same
dir/file structure, the results differ e.g. between ext4 and btrfs,
because of different sizes for the directories (themselves).
It would be nice if there was a option that allowed to select which
file types are counted.
Yes I know that one can do something like:
find . -type f -print0 | du --apparent-size -l -c -s --block-size=1
--files0-from=- | tail -n
But that's rather cumbersome... also I cannot do something like
du path1 path2 path3
and get totals for each and a grand summary.
And even if I make an shell alias out of this, I cannot do bash completion on
it.
Thanks,
Chris.
--- End Message ---
--- Begin Message ---
Subject: |
Re: bug#61884: add an option to du that allows to control which file types are counted |
Date: |
Sat, 4 Mar 2023 17:00:25 -0800 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.7.1 |
On 2023-03-04 15:33, Christoph Anton Mitterer wrote:
But would symlinks (i.e. their length) count for it?
Sure, because you can read symlinks by using readlink, and that gives
you their lengths.
Come to think of it, POSIX specifies st_size only for regular files and
symlinks among the files you'll find in a directory. So du --apparent
should count st_size only for these file types; it should ignore st_size
for other file types unless we know somehow that those sizes make sense
(which for directories is problematic for the reasons you mention).
What about hardlinked files, would they count once or n times?
That's an independent axis and is handled by -l. Hard links are not a
file type.
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
D door (Solaris)
I expect Coreutils's already-existing usable_st_function should tell us
which types have usable st_size. This will exclude directories, which
should be the right thing for your use case.
So I installed the attached patch to fix du --apparent to count sizes
only when st_size is well-defined. This should address your use case so
I'm boldly closing the bug report.
0001-du-apparent-counts-only-symlinks-and-regular.patch
Description: Text Data
--- End Message ---