We often encounter a situation where we need to find huge files in Linux. These files can be log files or some other kind of data file. In such scenarios, we need to search files by size. For this, we can easily use the find command in Linux.
The find command in Linux provides an easy way to search files recursively in a directory hierarchy. It also offers various options for doing a selective search. One such option is “-size”, which helps search files by size recursively.
Syntax of find command to find files bigger than given size in Linux
find <directory> -type f -size +N<Unit Type>
In the given, it will recursively search for the files whose size is more significant than N. Here, N is a number, and along with it, we can specify size unit type like,
- G for gigabytes
- M for megabytes
- K for kilobytes
- b for bytes
For example, “-size +4G” makes the find command search for more than 4GB. Here, + sign denotes that look for files greater than or equal to N[Type]; in this case, -size +4G will make the find command look for files bigger than 4GB.
Find files larger than 4gb in Linux
To find files larger than 4GB, we need to pass the -size option with value +4G in the find command.
find /usr -type f -size +4G
Output:
/usr/logs/test_1_logs.txt
/usr/logs/test_2_logs.txt
It recursively searched for files inside the folder “/usr” and filtered out the files with size larger than or equal to 4GB, then printed the paths of such files.
The previous command just printed the file paths more significant than 4GB. If you want to print the size along with the file name then use this command,
find /usr -type f -size +4G -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
Output:
/usr/logs/test_1_logs.txt|| Size : 4.6G
/usr/logs/test_2_logs.txt|| Size : 7.2G
Find files larger than 1gb in Linux
To find files larger than 1GB, we need to pass the -size option with value +1G in the find command.
find /usr -type f -size +1G
Output:
/usr/logs/error_logs.txt<br>/usr/logs/warning_logs.txt
It recursively searched for files inside the folder “/usr/” and filtered out the files with size larger than or equal to 1GB, then printed the paths of such files.
The previous command just printed the file paths which are greater than 1GB. If you want print the size along with file name then use this command,
find /usr -type f -size +1G -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
Output:
/usr/logs/error_logs.txt|| Size : 1.6G
/usr/logs/warning_logs.txt|| Size : 2.2G
Find files larger than 500mb in Linux
To find files larger than 500 MB, we need to pass the -size option with value +500M in the find command.
find /usr -type f -size +500M
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 500MB, then print the paths of each such files.
Output:
/usr/logs/test_3_logs.txt
/usr/logs/test_4_logs.txt
To print file size along with with file paths for files larger than 500MB use this command,
find /usr -type f -size +500M -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
It will print the file paths along with size for the files larger than 500MB.
Output:
/usr/logs/test_3_logs.txt|| Size : 610G
/usr/logs/test_4_logs.txt|| Size : 712G
Find files larger than 100mb in Linux
To find files larger than 100 MB, we need to pass the -size option with value +100M in the find command.
find /usr -type f -size +100M
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 100MB, then print the paths of each such files. To print file size along with with file paths for files larger than 100MB use this command,
find /usr -type f -size +100M -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
It will print the file paths along with size for the files larger than 100MB.
Find files larger than 50mb in Linux
To find files larger than 50 MB, we need to pass the -size option with value +50M in the find command.
find /usr -type f -size +50M
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 50MB, then print the paths of each such files. To print file size along with with file paths for files larger than 50MB use this command,
find /usr -type f -size +50M -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
It will print the file paths along with size for the files larger than 50MB.
Find files larger than 0 bytes in Linux
To find files larger than 0 Bytes, we need to pass the -size option with value +0b in the find command.
find /usr -type f -size +0b
It will recursively search for the files inside the folder “/usr/” and filter out the files with size larger than or equal to 0 Bytes, then print the paths of each such files. To print file size along with with file paths for files larger than 0 Bytes use this command,
find /usr -type f -size +0b -exec ls -lh {} \; | awk ‘{ print $9 “|| Size : ” $5 }’
It will print the file paths along with size for the files larger than 0 bytes.