There are a few different ways to search files by size in Linux. One way is to use the find command.
The find command is a powerful tool that can be used to search for files based on a variety of criteria, including size.
Table of Contents
Search files by size in Linux
To search for files by size using the find command, you can use the following syntax:
find /path/to/directory -type f -size {+/-}<size>
Boost Your Website Speed!
If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!Let’s break down the syntax:
/path/to/directory: Replace this with the directory path where you want to start the search. You can use . to search in the current directory.
-type f: This option ensures that only regular files are considered, excluding directories and other special file types.
-size: This option is used to specify the size criteria.
{+/-}<size>: Replace <size> with the desired file size. Use + to find files larger than the specified size, – to find files smaller than the specified size, and omit the symbol to find files with the exact specified size. You can use suffixes like k (kilobytes), M (megabytes), G (gigabytes), etc., to specify the size in different units.
For example, to search for all files in the current directory that are larger than 100 megabytes, you would use the following command:
find . -size +100M
The find command can also be used to search for files that are smaller than a certain size, or that fall within a specific size range.
For example, to search for all files in the current directory that are smaller than 10 megabytes, you would use the following command:
find . -size -10M
To search for all files in the current directory that are between 10 and 100 megabytes in size, you would use the following command:
find . -size +10M -size -100M
how to use exec option and size option in find command
The -exec option of the find command enables you to perform actions on the files or directories that match your search criteria.
The general syntax for using -exec is as follows:
find /path/to/directory -type {f|d} -exec <command> {} \;
-exec <command> {} \;: This is the -exec option followed by the command you want to execute and the placeholder {} which represents the path of the files or directories found by find. The \; at the end indicates the end of the -exec command.
Here are a couple of examples that combine the find command with the -size option and the -exec option:
Find files larger than 1 gigabyte and perform an action on them:
find /path/to/directory -type f -size +1G -exec ls -l {} \;
This command searches for regular files (-type f) in the specified directory and its subdirectories that are larger than 1 gigabyte (-size +1G).
For each file found, the -exec option is used to execute the ls -l command, which lists detailed information about the file.
Find files smaller than 100 kilobytes and delete them:
find /path/to/directory -type f -size -100k -exec rm {} \;
This command searches for regular files (-type f) in the specified directory and its subdirectories that are smaller than 100 kilobytes (-size -100k).
For each file found, the -exec option is used to execute the rm command, which deletes the file.
In both examples, the {} placeholder is used to represent the file path found by find, and the \; is used to terminate the -exec command.
Sort files by size with find command in Linux
You can pipe the output of find command to the du command (disk usage) and then use the sort command to sort the results. Here’s an example:
find /path/to/directory -type f -exec du -h {} + | sort -rh
Let’s break down the command:
find /path/to/directory -type f: This part of the command finds all regular files (-type f) in the specified directory and its subdirectories.
-exec du -h {} +: This part executes the du command on each file found. du -h displays the disk usage of files in human-readable format.
The {} is a placeholder for each file found by find, and + ensures that du processes multiple files at once to improve performance.
|: This symbol is a pipe, which redirects the output of the du command to the next command (sort).
sort -rh: This command sorts the output from du in reverse (-r) order, considering human-readable file sizes (-h). The largest files will be listed first.
After running this command, you will get a sorted list of files in the specified directory and its subdirectories, ordered by their size, with the largest files appearing at the top.
Remember to replace /path/to/directory with the actual directory path you want to search in.
find . -type f -exec du -h {} + | sort -rh
Here’s an example of what the output looks like:
4.3G ./path/to/largefile1.txt
2.1G ./path/to/largefile2.jpg
1.8G ./path/to/largefile3.mp4
986M ./path/to/mediumfile1.pdf
548M ./path/to/mediumfile2.avi
250M ./path/to/mediumfile3.wav
78M ./path/to/smallfile1.png
32M ./path/to/smallfile2.docx
9.8M ./path/to/smallfile3.xls
In this example, the files are listed with their respective sizes, displayed in human-readable format (e.g., gigabytes, megabytes). The largest files are listed at the top, followed by files of decreasing size.
To use the find command with the -size option and sort the files by size in Linux, you can combine the -size option with the -exec option and pipe the output to the sort command. Here’s an example:
find /path/to/directory -type f -size +100M -exec du -h {} + | sort -rh
4 Ways to Check File Size in Linux - howtouselinux
Saturday 2nd of December 2023
[…] Search files by size in Linux […]