Linux find command and how it helps to display readable File Sizes

Linux find command and how it helps to display readable File Sizes

03.06.2024
Author: HostZealot Team
2 min.
222

Find is a rather effective method in the Linux operating system that is required for finding directories/files according to specific attributes. When talking about the size of the files, the default results can be not as understandable for the users.

To change this overwhelming situation for a clearer output, we decided to share our experience in this sphere. The mixture of find command with other ones can be helpful in this situation and in such a way it will definitely simplify the searching process.

Here in the article, we will share some of the most useful command combinations that can give human-relatable outputs. If that topic sounds interesting to you, then let’s dive into a couple of good examples.  

The find Command's -printf Action

With the usage of the find command, it is probable to search for the files based on their size, permission, ownership, type, name, and way more. When we apply another command that is -printf, it prints the information about particular searched files. In order to get information about the size of the file, you should apply such specifiers as %s.

Here is how this command should look like:

$ find /pathtodirectory -type f -printf "%s %p\n"

With the usage of %s, you are getting output in the bytes. Here the output will be numbers in bytes and the titles of the files after them. For getting more human-readable information, for instance, in kilobytes rather than reviewing the default option, use the following command:

$ find /pathtodirectory -type f -printf "%k KB %p\n"

By the usage of %k, you are specifying the kilobytes criteria in your search. Prior to the usage of any of the discussed examples, you should better check whether that is appropriate for your operating system because this command can function slightly differently on various Unix systems.

The du Command in Action

One more great option is the usage of du command together with find. Du is already preinstalled on all the Linux systems, so you don’t need to waste additional time on the installation process. Here is an example of how this command can be used:

 $ find /pathtodirectory -type f -exec du -h {} +

Now, let’s try to understand what commands have been used here. -type f part searches for all the files in the certain directory. -exec is executing the following command, the plus symbol allows to search for several file names, and the braces are used to show all the needed data in the form of the list.

For showing all the files in the megabytes, you can also use du. However, in this case, files that are less than 1 MB will be shown as they have one megabyte. The command line will look like this:

$ find /pathtodirectory -type f -exec du -m {} +

You can use this command and get reliable output only when files are 1 MB and above.

If you need such kind of information only about the directories, you can also use it will the same command. Here is how it looks:

$ find /pathtodirectory -maxdepth 1 -type d -exec du --max-depth=1 -h {} +

With the usage of this line, you are searching for all the directories in the path that you mention immediately after the find. Maxdepth characteristics only search for directories in the chosen folder. The last number in the output shows the total size of all the directories.

Exploring File Sizes with ls

Ls is a typical Linux command that is required for listing directories/files. To show human-related outputs of the file sizes, you can utilize the next line:

$ find /pathtodirectory -type f -exec ls -lh {} +

In this sequence, ls -lh is responsible for showing all the elements of every file, including the size characteristics as well. To make this line more specific and show the size results in kilobytes (it can be also used for megabytes and bytes) use the following example:

$ find /pathtodirectory -type f -exec ls -l --block-size=KB {} +

In case you need more structured information about the sizes of the files, you can choose to show them in descending/ascending order. This can be done with the usage of the following line:

 $ find /pathtodirectory -type f -print0 | xargs -0 ls -lS --block-size=KB

The output here will show the results in descending order.

Advanced Filtering using grep

For advanced filtering the most utilized command is grep with the help of which you can select the needed pattern and all the shown sequences will include this kind of specified pattern. As an illustration, let’s type the command that will show only files which are in MB:

$ find /pathtodirectory -maxdepth 1 -type f -exec du -h {} + | grep -E '\b[0-9]+M\b'

In this line, \b is used as a specific boundary which means that M will be searched as a part of the size, but not in another way. For narrowing down the search criteria, you can use the same command and add a needed keyword. The line should look like this:

$ find /pathtodirectory -type f -exec du -h {} + | grep 'keyword'

Sorting and Refining Results

If you want to sort the files according to their sizes, there are a couple of choices that can be utilized and here we will offer you a couple of examples of how sort command can be used for this purpose.

$ find /pathtodirectory -maxdepth 1 -type f -exec du -h {} + | sort -h -k1

Here in this line, as in most mentioned above, we use du in order to get human-readable sizes. With the -k1 characteristic, we clarify that filtering should be done depending on the initial field of every line. It is a default way of sorting, that the order is ascending, but with the addition of -r you can also get descending results. Here is how it should look:

$ find /pathtodirectory -maxdepth 1 -type f -exec du -h {} + | sort -rh -k1

We can go even further in our search and specify the exact file size that we need or below/above a certain range. Let’s for instance find online files that are below 30 MB:

$ find /pathtodirectory -maxdepth 1 -type f -size -30M -exec du -h {} +

For finding results that are above 30 megabytes, you can just change the minus into a plus. In case you need files that have a specific size and not higher or lower, you just need to remove +/- before the number of megabytes.

One more thing that you can do with the sorting command is showing results in descending/ascending order, this can be done as in the following line:

find /pathtodirectory -maxdepth 1 -type f -size +30k -exec du -h {} + | sort -h -k1

To Sum Up

Here in this writing, we tried to share our practical knowledge in the field and offer real examples of how find command can be utilized for showing search results in a more comprehensible and human-readable manner. As for the default output, the user will get results in the bytes and this is not always as convenient as it should be and way harder for a proper interpretation.

To get more customizable results for your search, there are a couple of good options such as ls, grep, du, and more. With the usage of user-friendly outputs, it became so much easier to get better insights. You have a great opportunity to test everything that you have just learned in theory, there are no challenges to implementing these commands even for inexperienced users. 

Related Articles