0

I'm trying to read the size of the files in a folder using du -ah, but the output keeps showing me the current directory information( . ). I was wondering if there's some flag I can use with du to avoid showing the dot directory info?

456K    ./EmptyShield.png
320K    ./FullShield.png
384K    ./HalfShield.png
1.2M    ./Heart.ai
1.2M    ./Shield.ai
3.4M    .

1 Answer 1

0

I am not aware of any such option. I think the simplest approach is to filter out the line associated with .

In the POSIX specification of du I have found nothing that states this has to be the last line. Still I expect it to be the last line because du -ah must process everything else before it knows the size of .. I'm not a programmer, nevertheless I think if du -ah wanted to print . before some ./whatever… for any reason, then it would have to deliberately postpone printing ./whatever… and still process it before printing .; such postponing would be extra work. So I am eager to assume . is always in the last line because this is natural from the point of view of any implementation, even if the specification says nothing about it. I have never encountered du that surprises me in this matter.

To filter out the last line:

du -ah | sed '$d'
# or
du -ah | head -n -1    # this usage of head is not portable though

Note this does not change what du -ah does or prints. To make du itself not show the line you need another approach. See below.


. is the default when there is no operand for du. Your du -ah is equivalent to du -ah ..

To make the tool omit . you must not specify .. Specify all files inside ./ instead:

du -ah ./* ./.[!.]* ./..?*

or if your shell supports brace expansion:

du -ah ./{,.[!.],..?}*

There are some problems with this approach:

  • Unmatched globs will probably stay literal and cause no such file or directory error; non-fatal though.

  • Too many matches will cause argument list too long error; fatal.

  • The specification states:

    A file that occurs multiple times under one file operand and that has a link count greater than 1 shall be counted and written for only one entry. It is implementation-defined whether a file that has a link count no greater than 1 is counted and written just once, or is counted and written for each occurrence. It is implementation-defined whether a file that occurs under one file operand is counted for other file operands.

    This means if there are hardlinked files in . then your du -ah will count and print just one of the bunch (the "multiple times under one file operand" case). My du -ah ./* ./.[!.]* ./..?* may or may not print all of the bunch (the "file that occurs under one file operand is counted for other file operands" case). Even if your du prints just one of the bunch, it may be not the same one as from du -ah (see the next bullet point).

  • The order of patterns affects the output.

    E.g. if A/foo and .B/bar are the same file then du -ah will

    • print A/foo and account it for the size of A, it won't print .B/bar and the size of B will be as if bar wasn't there;

    • xor it will print .B/bar and account for .B, won't print A/foo and won't account for A.

    du -ah ./* ./.[!.]* ./..?* may print both A/foo and .B/bar (see the previous bullet point). If it prints just one then it will be A/foo because ./* (matching ./A) comes before ./.[!.]* (matching ./.B). Specifying the patterns in the reverse order will make the tool print .B/bar instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .