To find large files in Linux using the find
command, you can use the -size
option along with other criteria to locate files that exceed a certain size threshold. Here are some examples of how to use find
to search for large files:
1. Find Files Larger Than 100 MB
To find files larger than 100 MB in the current directory and its subdirectories:
find . -type f -size +100M
2. Find Files Larger Than 1 GB
To find files larger than 1 GB in the current directory and its subdirectories:
find . -type f -size +1G
3. Find Files Smaller Than 500 KB
To find files smaller than 500 KB in the current directory and its subdirectories:
find . -type f -size -500k
4. Find Files Between 1 MB and 5 MB
To find files between 1 MB and 5 MB in the current directory and its subdirectories:
find . -type f -size +1M -size -5M
5. Find Large Files in a Specific Directory
To find files larger than 100 MB in the /home/user/documents
directory and its subdirectories:
find /home/user/documents -type f -size +100M
6. Find Large Files and Display Their Sizes
To find files larger than 100 MB in the current directory and its subdirectories and display their sizes:
find . -type f -size +100M -exec ls -lh {} \;
7. Find Large Files and Summarize Their Sizes
To find files larger than 100 MB in the current directory and its subdirectories and summarize their sizes:
find . -type f -size +100M -exec du -ch {} \; | grep total$
8. Find Large Files and Delete Them
To find files larger than 100 MB in the current directory and its subdirectories and delete them:
find . -type f -size +100M -exec rm -i {} \;
9. Find Large Files and Exclude Certain Directories
To find files larger than 100 MB in the current directory and its subdirectories, excluding the node_modules
and .git
directories:
find . -type f -size +100M -not \( -path "./node_modules/*" -o -path "./.git/*" \)
10. Find Large Files and Exclude Specific Files
To find files larger than 100 MB in the current directory and its subdirectories, excluding files named example.txt
:
find . -type f -size +100M ! -name "example.txt"
Example Usage
Let’s say you want to find all files larger than 100 MB in the /home/user/documents
directory and its subdirectories:
find /home/user/documents -type f -size +100M
Conclusion
These examples demonstrate how to use the find
command to search for large files based on their size. You can combine these options with other criteria, such as file name, modification time, and file type, to create more complex queries. If you have specific requirements or need more advanced functionality, feel free to ask, and I can provide more tailored examples.