1. Introduction

The find command is a tool in the Linux environment that provides a powerful means to search for files and directories based on specified attributes. Yet, when it comes to file sizes, the default output can often be overwhelming, especially for users seeking human-readable formats.

However, combining find with other commands can transform the raw, machine-friendly file size output into a more user-friendly format. This simplifies the process of navigating and managing files within a directory hierarchy.

In this tutorial, we’ll learn more about the find command’s resulting output and explore the different commands we can use to display find results in a human-readable format.

2. Using the find Command’s -printf Action

The find command allows users to search for files based on various criteria: file names, modification times, file types, and more.

The -printf action in the find command allows us to format and print information about the files found. To display file sizes using the -printf action, we can use the %s format specifier.

Let’s take a look at a basic example of using the -printf action:

$ find /path/to/directory -type f -printf "%s %p\n"
977 ./user_script_openssl
70944384 ./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
42 ./numeric_strings.txt

In this example, the command displays the file size in bytes using the %s option. The %p format specifier represents the file name, including the path, and the \n adds a newline character for better readability.

We can also modify the command to display the results in kilobytes or megabytes, which is more human-readable than the default of bytes.

As an illustration, let’s display the file sizes in kilobytes:

$ find /path/to/directory -type f -printf "%k KB %p\n"
72 KB ./company_care_final_invoice.pdf
69288 KB ./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
68 KB ./doc.pdf

Here, we’re using the %k format specifier to display the file sizes in kilobytes.

We need to note that the options and features of the find command can vary slightly between different Unix-like systems, so it’s a good idea to check the man page on our system for specific details and options.

3. Using the du Command

Another common and effective method employs the du command to display human-readable file sizes from find results.

The du (disk usage) command is a Linux command that estimates file and directory space usage. It comes preinstalled in all Linux distros, so we don’t need to install it.

Let’s find out how to use the du command to display human-readable file sizes from the find command results:

$ find /path/to/directory -type f -exec du -h {} +
4.0K	./user_script_openssl
72K	./company_final_invoice.pdf
68M	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
...

Specifying the -type f option above finds all files in the specified directory, and using the -exec option, we execute the du -h command on the files found. The du command’s -h option prints the sizes in a human-readable format.

The plus sign (+) at the end of the -exec argument allows the find command to pass multiple file names to a single execution of the du command, which is more efficient. Finally, the curly braces {} represent the list of files found by the find command.

The output shows the size and filename of each file in the /path/to/directory directory specified.

We can also use the du command to show all files found in megabytes, however, files less than one megabyte will also be displayed as 1:

$ find /path/to/directory -type f -exec du -m {} +
1	./user_script_openssl
1	./company_final_invoice.pdf
68	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
...

This method is only reliable when dealing with file sizes above one megabyte.

On the other hand, we can modify the command to include directories as well and get a summary of each directory:

$ find /path/to/directory -maxdepth 1 -type d -exec du --max-depth=1 -h {} +
652M ./official_baeldung_fork
...
8.0K ./Java
1.4G ./

The command finds all directories in the /path/to/directory. We’re passing the -maxdepth 1 option to only show directories in the current specified folder.

We then execute the du -h –max-depth=1 command on each directory found. This ensures that the du command only shows the total size of each directory without descending to subsequent subdirectories. The output’s last line shows the directory’s total size.

4. Using the ls Command

We can also use the ls command and some of its options to display human-readable file sizes in find results. ls is a Linux command that allows users to list files or directories and their attributes from the CLI.

Let’s use the ls command to display human-readable file sizes:

$ find /path/to/directory -type f -exec ls -lh {} +
-rwxrw-r-- 1 user user 977 Hag 18 07:24 ./user_script_openssl
-rw-rw-r-- 1 user user 68M Sad  8 22:02 ./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
-rwxrw-r-- 1 user user 989 Hag 21 07:22 ./create-user
...

In this command, we’re using the -f option to find all files in the /path/to/directory directory. We then use the ls -lh command to display detailed information about each file, including the size in human-readable format.

Moreover, the ls command allows us to display the size in bytes, kilobytes, or megabytes.

For example, let’s display the file size information in kilobytes:

$ find /path/to/directory -type f -exec ls -l --block-size=KB {} +
-rwxrw-r-- 1 user user 1kB Hag 18 07:24 ./user_script_openssl
-rw-rw-r-- 1 user user 70945kB Sad  8 22:02 ./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
-rwxrw-r-- 1 user user 1kB Hag 21 07:22 ./create-user
...

Finally, we can also use the ls command to sort the file sizes in ascending or descending order:

$ find /path/to/directory -type f -print0 | xargs -0 ls -lS --block-size=KB
-rw-rw-r-- 1 user user 70945kB Sad  8 22:02 ./master-pdf-editor-5.9.70-qt5.9.x86_64.deb
-rw-rw-r-- 1 user user    72kB Sad  7 17:36 ./company_final_invoice.pdf
-rw-rw-r-- 1 user user    67kB Sad 30  2022 ./doc.pdf
-rwxrw-r-- 1 user user     2kB Hag  8 07:32 ./user_script
-rwxrw-r-- 1 user user     1kB Hag 21 07:22 ./create-user

Here, we’re sorting the results in descending order. We pipe the output to the xargs command to handle the execution of the ls command more efficiently. This is effective when dealing with large directories.

We’re using the -print0 and -0 options to separate the file names using null characters. This ensures filenames with spaces or other special characters are handled correctly.

By default, it’ll list the files in descending order for each subdirectory found. Every time it encounters a new subdirectory, it starts with the largest file in that subdirectory.

However, we can also restrict the search depth by employing the -maxdepth option:

$ find /path/to/directory -maxdepth 1 -type f  -print0 | xargs -0 ls -lS --block-size=KB
-rw-rw-r-- 1 user user 60kB Ful 27 11:48 ./harvard_first_page.jpg
-rw-rw-r-- 1 user user 43kB Hag 16 08:17 ./installed_packages_with_dates.txt
-rw-rw-r-- 1 user user 40kB Hag 16 08:16 ./installed_packages.txt
-rw-rw-r-- 1 user user  2kB Ful 27 11:52 ./harvard_first_page_text.txt
...

Here, we’re only processing files in the current directory and ignoring any subdirectories.

5. Incorporating the grep Command and Advanced Filtering

The grep command searches a file or files for a specific pattern of characters and then displays all the lines that contain the specified pattern.

We can employ the grep command to add a layer of filtering to the find command results.

For instance, let’s use the grep command to display only files whose sizes are specified in megabytes. Thus, the command only works for files above one megabyte:

$ find /path/to/directory -maxdepth 1 -type f -exec du -h {} + | grep -E '\b[0-9]+M\b'
68M	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb

In this case, after executing du to obtain human-readable file sizes, we’re using grep to filter and display files whose sizes are specified in megabytes. We’re using \b as a word boundary. This helps us ensure the letter M is matched as part of the size and not another word. Moreover, since the results are displayed in columns, we can focus on only the matches highlighted in the first column.

On the other hand, we can also use the grep command to narrow our results even further and only display files with a specific keyword:

$ find /path/to/directory -type f -exec du -h {} + | grep 'specific_keyword'

Here, we use the find command to get all files using the -type f option.

Next, we’re executing the du -h command to get the file sizes in human-readable format. We then execute the grep command to search for the specified keyword in their names. We can adjust the path and keyword according to our requirements.

5.1. Sorting and Filtering Results

We can also employ the sort command to arrange the files according to their sizes:

$ find /path/to/directory -maxdepth 1 -type f -exec du -h {} + | sort -h -k1
68K	./doc.pdf
72K	./company_final_invoice.pdf
68M	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb

We’re using the find command to get all files in the current directory, and executing the du command to get human-readable file sizes for each file.

We then pipe the output to the sort command with the -h option, which performs a human-readable sort. The -k1 option specifies that the sorting should be based on the first field of each line.

By default, the file sizes are arranged in ascending order, however, we can employ the -r option to sort in descending order:

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

Finally, we can also use the find command’s -size option to filter the results and only search for files that are above, below, or exactly a specific file size.

For example, let’s filter the results to only process files above 10 megabytes:

$ find /path/to/directory -maxdepth 1 -type f -size +10M -exec du -h {} +
68M	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb

We’re using the -size +10M option to find files that are above 10 megabytes. We can change the positive sign to negative to find files that are below 10 megabytes or remove the sign to find files that are exactly the size specified.

We can also pipe the output to the sort command that outputs an ascending or descending file size order:

$ find /path/to/directory -maxdepth 1 -type f -size +10k -exec du -h {} + | sort -h -k1
68K	./doc.pdf
72K	./company_final_invoice.pdf
68M	./master-pdf-editor-5.9.70-qt5.9.x86_64.deb

Here, we’re only processing files above 10 kilobytes.

6. Conclusion

In this article, we looked at the different methods to display the find command results in a human-readable format. By default, the find command displays file sizes in bytes using the -printf action, which can be hard to interpret.

The flexibility of the find command, when combined with various commands such as du, ls, grep, and their respective options, allows for tailored and customizable output. We can gain better insights and facilitate easier file handling by transforming raw, machine-friendly file size outputs into a more user-friendly format.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.