3

I am trying to display the top 20 largest files in a specific directory. I want to include all sub directories but not the actual directories themselves. (I only want files.) I have been trying to find a way to do this and all the solutions I have found online do not work with the version on Unix I am using.

I have this so far:

find /dir -type f -exec ls -al {} \; | sort -nr | head -n 20 du -a -g /dir/ | sort -n -r | head -n 20

The fist gives me a list as follows:

file1.txt
file1.txt
file1.txt
file2.txt

And so on. The second command gives me the following:

500    \path\
250    \path\to\
100    \path\to\directory\

And so on. The result I am looking for is:

500    \path\file1.txt
250    \path\to\file2.txt
100    \path\to\directory\file3.txt

And so on. I have tried the solutions from the following questions:

I have also tried to follow this tutorial:

1
  • 4
    You state that the solutions you have found don’t work on your UNIX version, but you don’t mention what it is. Please edit question to include this information
    – bxm
    Nov 19, 2019 at 23:58

2 Answers 2

7
find dir/ -type f -exec du -a {} + | sort -nr | head -n 20
1
  • This doesn't output friendly. Some unknown numbers. Perhaps -h?
    – Alex G
    Oct 17, 2023 at 10:28
2

In the zsh shell, the globbing pattern

/dir/**/*(.DOL[1,20])

would expand to the pathname of the 20 largest files in or below the directory /dir (with hidden names also being considered and symbolically linked files being ignored).

The parts of the glob qualifier (.DOL[1,20]) means:

  • .: Only consider regular files.
  • D: Also consider files with hidden names (like the effect of dotglob in bash).
  • OL: Order the result in decreasing order by file size (O = decreasing order, as opposed to o = increasing order; L = "length").
  • [1,20]: Return the 20 first element that the glob expands to.

The ** pattern matches "recursively", so /dir/**/* by itself would match any name in or below the /dir directory.

Using this with ls:

ls -l -f /dir/**/*(.DOL[1,20])

You would have to use -f with ls to stop it from sorting the list that it shows.

Or, to call du on the files:

du /dir/**/*(.DOL[1,20])

You must log in to answer this question.

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