4

I want to list all files in descending order based on disk usage, and have obtained the following commands from this:

To list the top 10 largest files from the current directory: du . | sort -nr | head -n10

To list the largest directories from the current directory: du -s * | sort -nr | head -n10

However, they both list only directories.

How can I make du only include regular files in its output?

Bonus:

What is the following du option used for (it seems to have no effect)?:

 -a, --all             write counts for all files, not just directories 

1 Answer 1

5

IF you have GNU du, then send it a list of regular files (or directories, or whatever you want to filter on) using find (or as arguments using shell globs in zsh):

find . -type f -print0 | du --files0-from=-
du *(.)  # zsh, regular non-hidden files in the current directory

du by default prints only the sum of the file sizes inside a directory for that directory, but you can have it print those individual file sizes too, which is what -a does:

% du /usr/bin
68  /usr/bin/lou_maketable.d
100 /usr/bin/vendor_perl
464 /usr/bin/core_perl
4   /usr/bin/site_perl
1104024 /usr/bin
% du /usr/bin -a | head
4   /usr/bin/preparetips5
36  /usr/bin/autoupdate
4   /usr/bin/python3.9-config
0   /usr/bin/mkoctfile
60  /usr/bin/zdump
16  /usr/bin/fstopgm
4   /usr/bin/brltty-setcaps
40  /usr/bin/sleep
8   /usr/bin/pnmquantall
20  /usr/bin/extract_mpeg2
5
  • Thanks. Good idea regarding --files0-from=-. Do you know why du . list directories/files recursively, while du /usr/bin only lists that single directory? Furthermore, your example is incorrect (at least on my system): du /usr/bin only results in 618192 /usr/bin.
    – Shuzheng
    May 14, 2021 at 9:14
  • @Shuzheng Does your system have directories in /usr/bin? Mine has and du usually (well, at least, GNU du and macOS du) lists subdirectories too.
    – muru
    May 14, 2021 at 9:21
  • You're correct. I had only executables in /usr/bin.
    – Shuzheng
    May 14, 2021 at 9:23
  • 2
    @Shuzheng Subdirectories in /usr/bin (or /bin) are not really common, but some packages use them (e.g. perl). Also, a standard du "By default, ... shall write to standard output the size of the file space allocated to, and the size of the file space allocated to each subdirectory of, the file hierarchy rooted in each of the specified files." I.e. du . should only list directories (you may need to make sure your du is not an alias or a function).
    – fra-san
    May 14, 2021 at 9:26
  • @fra-san You're correct. du <dir> only lists directories, not files.
    – Shuzheng
    May 14, 2021 at 9:50

You must log in to answer this question.

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