Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 28 '10 at 23:25

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 71 down vote accepted

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.
share|improve this answer
7  
Oh... I read that but it wasn't clear to me that you can modify the size argument with + or -. I thought it meant exact match. Did I miss the faq or something? –  ceiling cat Oct 28 '10 at 23:50
3  
Nope, I don't think your missing anything. I didn't know you could use + or - for greater/less than. The man page didn't mention that part. –  Jay Oct 28 '10 at 23:56
3  
@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." –  Dennis Williamson Oct 29 '10 at 2:14
2  
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) –  Slartibartfast Oct 29 '10 at 2:24
3  
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 '10 at 14:14

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
share|improve this answer

Greater than 2000 bytes:

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

Less than 2000 bytes:

du -a . | awk '$1*512 < 2000 {print $2} '
share|improve this answer
    
This is what happens when I go from sed to perl without learning awk :) Very cool. +1 –  vivin Oct 28 '10 at 23:42
    
yeah, awk is pretty bad ass –  Jay Oct 28 '10 at 23:44
    
du... I don't know how I forgot about it. Thanks! –  ceiling cat Oct 28 '10 at 23:57
find . | xargs du -b | perl -ne 'chomp;@p=split(/\s/);print"$p[1] ($p[0])\n"if$p[0]<=4096'

Prints all files that are lesser than or equal to 4096 bytes in size. Simply change the operator and number at the end of this one-liner to change the criteria.

The -b option for du gives you the number of bytes and is equivalent to the apparent size with a block size of 1. Apparent size is usually smaller and may not reflect actual disk-usage.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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