How do I perform a file search in linux based on the size of the file?
-
Also see: Man page for 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 popu‐ lated 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 speci‐ fiers of -printf handle sparse files differently. The `b' suf‐ fix 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.
Find all nonempty files in current directory.
$ find . -size +0 . ./bigfile ./filex ./test ./bigfile2
Remember the
-ls
option of find to list the results, verify they are nonempty.$ find . -size +0 -ls 237398 9 drwxrwxr-x 3 trainer trainer 12 Nov 16 14:34 . 237376 1 -rw-rw-r-- 1 trainer trainer 195429376 Nov 16 14:34 ./bigfile 237332 1 lrwxrwxrwx 1 trainer trainer 5 Nov 14 12:38 ./filex -> filea 237402 1 drwxrwxr-x 2 trainer trainer 3 Nov 14 11:45 ./test 237378 1 -rw-rw-r-- 1 trainer trainer 498598912 Nov 16 14:34 ./bigfile2
Find files that are nonempty and less than 10 blocks. (512 bytes) is 1 block, the default in find. This use the boolean logic AND
-a
option, is powerful as it used to find a size range.$ find . -size +0 -a -size -10 -ls 237398 9 drwxrwxr-x 3 trainer trainer 12 Nov 16 14:34 . 237332 1 lrwxrwxrwx 1 trainer trainer 5 Nov 14 12:38 ./filex -> filea 237402 1 drwxrwxr-x 2 trainer trainer 3 Nov 14 11:45 ./test
Find an file of exacts size. Converting 195429376 blocks ends up as 187M. Notice we did not use the negative or positive symbols for the size value, which mean the exact size.
$ find . -size 187M -ls 237376 1 -rw-rw-r-- 1 trainer trainer 195429376 Nov 16 14:34 ./bigfile $ ls -lh bigfile -rw-rw-r-- 1 trainer trainer 187M Nov 16 14:34 bigfile
Find a file greater than a certain size. We use the
+
sign to find file greater than the value.$ find . -size +187M ./bigfile2 $ find . -size +187M -ls 237378 1 -rw-rw-r-- 1 trainer trainer 498598912 Nov 16 14:34 ./bigfile2 $ ls -lh bigfile2 -rw-rw-r-- 1 trainer trainer 476M Nov 16 14:34 bigfile2
© Lightnetics 2024