Being a Linux user, managing files and directories efficiently is crucial, especially when dealing with a large number of files. Sometimes, you may find yourself in a situation where you need to locate files of specific sizes to free up disk space or perform some other administrative tasks.
In this article, we will explore various methods to locate kilobyte-sized files, making it easier for you to understand the process of searching files by size.
Pinpointing kilobyte-sized files can be a crucial technique as it allows you to zoom in on those smaller, yet significant, files that might be affecting your system’s performance or taking up more space than necessary.
1. Find Files with a Specific Size
Let’s begin this tutorial by searching all the files that have a specified size by using the “find” command.
To do so, we can use the syntax mentioned below.
$ find . -type f -size <size_of_file>
Here is the breakdown of the above syntax:
-type– This flag allows you to define the type of file or directory that you want to locate. It can be used with multiple arguments depending on your requirements.
f– This flat directs the terminal to search for regular files only.
-size– This flag defines the size of the file that you want to search or find.
size_of_file– You can define the size of the file in bytes (denoted by “c”), kilobytes (denoted by “k”), megabytes (denoted by “M”), gigabytes (denoted by “G”), and terabytes (denoted by “T”). For instance, if you’re looking for files that are exactly 60 kilobytes, you can enter “60k” as the size parameter.
Note: Regular files are common types of files that can store user data including text, binary data, and documents in Linux.
Let’s see an example, to find all the files that are exactly 50 kilobytes in size within the current directory and its subdirectories.
$ find . -type f -size 50k
The output will display all the files which are exactly 50 Kilobytes in size.
2. Find Files Larger Than a Specified Size
Moving forward, we can also use the
“+” symbol before the file size to locate all the files larger or equal to the defined file size.
Let’s execute this command mentioned below to find the regular files that have a size equal to or larger than 1 Kilobyte:
$ find . -type f -size +1k
3. Find Files Smaller Than a Specified Size
Now that you know how to search for files that have a file size equal to or larger than the mentioned file size. It’s time to discuss a similar case of searching for files equal to or less than the defined size by using the
“-” sign before the file size.
To find all files that are 200 Kilobytes or smaller in size, simply execute the following command:
$ find . -type f -size -200k
4. Find File Sizes by Extension
The find command also offers more flags that can aid the users in locating the desired files or directories in Linux.
One such flag is
“-name”, which defines a pattern to specify the filename or extension you want to find. Moreover, you can combine two or more flags in the find command to match your needs.
To display all the files with sizes less than 100 kilobytes and the
“.conf” extension in its filename:
$ find . -type f -size -100k -name *.conf
All the files in the output are configuration files with a size equal to or less than 100 Kilobytes.
5. Find Files Within a Specific Size Range
We can also find a file by specifying a certain file size range. As we have learned, the use of
"-" signs with the file size in Kilobytes. To define a certain range for the file size, you can easily guess what we are going to do.
Yes, we are going to use
"-size" twice!!! Once with the
"+" sign and once with the
Let’s try this out by executing this command to search for files greater than 10kB but smaller than 50kB:
$ find . -type f -size +10k -size -50k
The size of files obtained by the execution of the above command will lie within the range of 10kB to 50kB.
6. Print File Sizes with Location
Lastly, we can also use the
“-printf” flag to know the location of the file having a particular size. The arguments that are used with the
“-printf” flag are
“%p %k KB\n”, as these arguments specify how the output will be displayed on the screen.
Run the command stated below to understand its use:
$ find . -type f -size 296k -printf "%p %k KB\n"
Here is the breakdown of the above syntax:
-printf– This flag is used to customize the output format of the found files.
%p– This indicates the path of the file found.
%k– This indicates the size of the file in Kilobytes.
KB– The size will be continued with the word “KB” in the output.
\n– This moves each output to a new line.
Now you can also see the size of the file along with its path.
Mastering the art of locating files with a specific size in Linux using the find command opens up a realm of possibilities for efficient file management and resource utilization.
Whether you’re conducting system maintenance, managing disk space, or simply curious about where large files reside on your system, the find command empowers you with precise control over your file searches.