How can I find files that are bigger/smaller than x bytes?
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.
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
.
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