319

In a terminal, how can I find files that are bigger or smaller than x bytes?

I suppose I can do something like

find . -exec ls -l {} \;

and then pipe the result to awk to filter by file size. But shouldn't there be an easier way than this?

5 Answers 5

495
+50

Use:

find . -type f -size +4096c

to find files bigger than 4096 bytes.

And :

find . -type f -size -4096c

to find files smaller than 4096 bytes.

Notice the + and - difference after the size switch.

The -size switch explained:

-size n[cwbkMG]

    File uses n units of space. 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 Kilobytes       (units of 1024 bytes)

    `M'    for Megabytes    (units of 1048576 bytes)

    `G'    for Gigabytes (units of 1073741824 bytes)

    The size does not count indirect blocks, but it does count
    blocks in sparse files that are not actually allocated. 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 1 Kilobyte blocks, which is different
    to the behaviour of -ls.
14
  • 13
    @Jay: From man find at the beginning of the "Tests" section: "Numeric arguments can be specified as +n for greater than n, -n for less than n, n for exactly n." Oct 29, 2010 at 2:14
  • 4
    The man page mentions it towards the top and describes that + and - can apply to all switches that take numeric ('n') arguments, including what + and - mean. (Search for TESTS in the man page to find the beginning of the section where this is described) Oct 29, 2010 at 2:24
  • 1
    @Dennis Williamson: weird, + and - work in OS X, but the man page doesn't have the TESTS section. In fact, it is missing a big chunk compared to your link, it's missing TESTs, ACTIONS, OPERATORS, among others. Oct 29, 2010 at 5:48
  • 1
    It appears the GNU version of the man page has the "Tests" section, but the BSD version does not. linuxmanpages.com/man1/find.1.php
    – Jay
    Oct 29, 2010 at 13:07
  • 8
    I just found out the BSD man pages do describe the +/- thing. Its way at the end of the "Primaries" section. -- All primaries which take a numeric argument allow the number to be preceded by a plus sign ( “+” ) or a minus sign ( “-” ) . A preceding plus sign means “more than n”, a preceding minus sign means “less than n” and neither means “exactly n”
    – Jay
    Oct 29, 2010 at 14:14
15

I think find might be useful alone without piping to AWK. For example,

find ~ -type f -size +2k  -exec ls -sh {} \;

The tilde indicates where you want your search to begin and the result should display only files greater than 2 kilobytes.

To make it fancy, you can use the -exec option to execute another command which is to list these directories with their sizes.

For more information, read up the man page for find.

7

AWK really is quite easy for this sort of thing. Here are some things you can do with it in relation to file size checking, like you asked:

List files greater than 200 bytes:

ls -l | awk '{if ($5 > 200) print $8}'

List files less than 200 bytes and write the list to a file:

ls -l | awk '{if ($5 < 200) print $8}' | tee -a filelog

List files of 0 bytes, record the list to a file and delete the empty files:

ls -l | awk '{if ($5 == 0) print $8}' | tee -a deletelog | xargs rm
2
  • 5
    parsing ls is not good
    – phuclv
    Mar 18, 2017 at 13:29
  • What is the difference between piping to tee and just redirecting to a file, like ls -l > filelog (or ls -l >> filelog)?
    – OmarOthman
    Mar 30, 2017 at 12:04
5

Greater than 2000 bytes:

du -a . | awk '$1*512 > 2000 {print $2}'

Less than 2000 bytes:

du -a . | awk '$1*512 < 2000 {print $2} '
1
  • This is bad as du gives the space on the disk occupied. This is not the same as a file size. eg if you have a compressed file system (btrfs/zfs) du will give you a different result from ls which shows the actual decompressed size. Aug 28, 2018 at 15:18
0

Using fd, which is much nicer to use than find

fd -S +1g

will search under the current directory for any files larger than 1GB

fd -S -1g

will search for files smaller than 1GB

You must log in to answer this question.

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