3

I have a directory called root. It has deeply nested subdirectories. In these subdirectories, there are a bunch of files. I want to find the biggest files in root.

I don't want to find directories at all. I don't care about the size of the largest directory, I just want to find the largest files.

root
|
- subdir1
  |
  - small file 1
  - large file 1
  - small file 2
  ... lots more files      
- subdir2
  |
  - small file 3
  - large file 2

I want to print out large file 1 and large file 2. I don't want it to print out anything about root, subdir1, or subdir2, even if they're bigger than large file 1 or 2. That's just noise to me.

How do I do that on a mac on the command line?

2 Answers 2

7

Let ls do the sorting->

find . -type f -exec ls -S {} + | head -n10
1
  • 4
    Good, but it doesn't let me see the size of the files. -Sl would be better Aug 21, 2019 at 20:38
2

This is what I came up with:

find . -type f -exec ls -l {} + | sort -rk 5,5 | head -10

Find all files (this ignores directories), then ls -l them so I can see their sizes. Pass that into sort. -k 5,5 says to sort by the 5th column (the column that has size), and -r sorts descending. head -10 gives me the largest 10 (not the full list).

1
  • 1
    For me this just sorts alphabetically rather than numerically. I had to add -h to the sort command to sort by file size numerically
    – Shawn
    Jan 12, 2022 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.