That's an excellent question! The find command is indeed very capable of searching files by size.
To find files by size, you use the -size predicate with find. You need to specify a size argument, which is typically a number followed by a unit (like c for bytes, k for kilobytes, M for megabytes, G for gigabytes, etc.).
Here's how it generally works:
find . -size +1M: Finds files larger than 1 megabyte. The+before the number means "greater than".find . -size -1M: Finds files smaller than 1 megabyte. The-before the number means "less than".find . -size 1M: Finds files exactly 1 megabyte in size (this is less commonly used as exact size matches are rare, but it's good to know).
Let's try an example in our current find_lab directory.
First, we need some files with different sizes. Since the files we created earlier (file1.txt, etc.) are empty, let's create a few new temporary files with varying sizes. We can use the fallocate command for this (or even dd if fallocate isn't available, but fallocate is faster for creating empty files of a given size).
fallocate -l 1K small_file.txt # Creates a 1KB file
fallocate -l 5M large_file.bin # Creates a 5MB file
fallocate -l 100K medium_file.log # Creates a 100KB file
Now, let's use find to search by size:
-
Find all files larger than 1KB:
find . -size +1kYou should see
large_file.binandmedium_file.log(and possibly some other files if their actual size is slightly above 1KB). -
Find all files smaller than 2K:
find . -size -2kThis should show
small_file.txtand potentially the original empty files likefile1.txt,report.txt, etc., because they are 0 bytes, which is definitely less than 2KB. -
Find all
.binfiles larger than 1MB:find . -name "*.bin" -size +1MThis will specifically find
large_file.bin.
Important notes about -size:
- Units:
b: 512-byte blocks (default, if no unit is given)c: bytesk: kilobytes (1024 bytes)M: megabytes (1024 kilobytes)G: gigabytes (1024 megabytes)
- Prefixes:
+: greater than-: less than- (no prefix): exactly
Feel free to play around with these commands in your find_lab directory! Creating these temporary files helps you get a good feel for how -size works.
Let me know if you want to try another example or have more questions!