1

Using the command line, how do I find all the files recursively beginning at a specific directory where all those files fall within a size range?

Additionally list the results sorted by size.

5

You can use

find /PATH/TO/specific_directory -size +MIN -size -MAX

For precise info about what MIN and MAX could be, check man find

   -size n[cwbkMG]
          File uses n units of space, rounding up.  The following suffixes can be used:

          `b'    for 512-byte blocks (this is the default if no suffix is used)

          `c'    for bytes

          `w'    for two-byte words

          `k'    for kibibytes (KiB, units of 1024 bytes)

          `M'    for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)

          `G'    for gibibytes (GiB, units of 1024 * 1024 * 1024 = 1073741824 bytes)

          The size is simply the st_size member of the struct stat populated by the lstat (or stat) system call, rounded
          up as shown above.  In other words, it's consistent with the result you get for ls -l.  Bear in mind that  the
          `%k'  and  `%b'  format  specifiers of -printf handle sparse files differently.  The `b' suffix always denotes
          512-byte blocks and never 1024-byte blocks, which is different to the behaviour of -ls.

          The + and - prefixes signify greater than and less than, as usual; i.e., an exact size of  n  units  does  not
          match.   Bear  in mind that the size is rounded up to the next unit.  Therefore -size -1M is not equivalent to
          -size -1048576c.  The former only matches empty files, the latter matches files from 0 to 1,048,575 bytes.

Update to meet your new requirements:

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du --human-readable --files0-from=- | sort --human-numeric-sort

or, in its short form:

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du -h --files0-from=- | sort -h

Update to meet your new requirements (2):

find /PATH/TO/specific_directory -size +MIN -size -MAX -print0 | du --human-readable --bytes --files0-from=- | sort
4
  • Is it possibe for the list to be sorted by size? I've updated the question because I did not specify that and I think that would be helpful. – Keenan Feb 24 '20 at 4:14
  • yes: check my updated answer. – matsib.dev Feb 24 '20 at 4:35
  • I've accepted this as the answer but is it possible to see exact sizes vs. rounded sizes? – Keenan Feb 25 '20 at 5:21
  • yes: If by 'exact sizes', you mean 'size in bytes', check my last update; If by 'exact sizes', you mean 'real size', check man du. – matsib.dev Feb 25 '20 at 5:40
2

zsh Files between 1K – 10M:

print -l /PATH/TO/DIR/**/*(.Lk+1Lm-10oL)
  • Recursive glob: **/*
  • For plain files: (.)
  • > 1KiB: (Lk+1)
  • but < 10MiB: (Lm-10)
  • Sorted by size in ascending order: (oL)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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