Comprehensive Guide to Printing File Sizes with find Command

Nov 28, 2025 · Programming · 10 views · 7.8

Keywords: find command | file size output | Unix command line

Abstract: This technical article provides an in-depth analysis of various methods to output both filenames and file sizes using the find command in Unix/Linux systems. The primary focus is on the -exec parameter combined with ls command, which is recognized as the best practice. The paper compares alternative approaches including -printf and -ls options, supported by detailed code examples. It addresses compatibility issues across different systems and offers practical solutions for diverse output formatting requirements, enhancing readers' understanding of advanced find command usage.

Fundamentals of find Command and Problem Context

In Unix/Linux system administration, the find command serves as a core tool for file searching. Users often need to retrieve detailed information, particularly file sizes, while locating specific files. The basic command find . -name *.ear only outputs file paths, failing to meet the requirement of displaying file sizes.

Optimal Solution: Combining -exec with ls Command

According to the highest-rated community answer, using the -exec parameter with ls -lh command represents best practice:

find . -name '*.ear' -exec ls -lh "{}" \;

In this command, -exec executes ls -lh "{}" for each matched file, where "{}" is replaced with the current filename. ls -lh displays file details in human-readable format, including size (automatically converted to KB, MB, etc.). This method offers excellent compatibility across various Unix variants.

Comparative Analysis of Alternative Approaches

-printf Parameter Approach:

find . -name *.ear -printf "%p %k KB\n"

%p outputs the file path, while %k outputs the size in KB. The advantage lies in customizable output format, though note that -printf is not POSIX standard and may be unsupported on some systems.

-ls Option Approach:

find . -name \*.ear -ls

This outputs in a format similar to ls -dils, including inode, permissions, size, and other details. While concise, the output can be verbose and may contain unnecessary information.

In-Depth Technical Analysis

When using -exec, proper handling of escape characters and quoting is crucial. Single quotes in '*.ear' protect the pattern from shell expansion, ensuring correct processing by find. \; signifies the end of the command. Quoting the filename "{}" handles special filenames containing spaces.

The reference article highlights that incorrect usage like find -name "*.conf" | ls -l fails because ls does not read from standard input. Proper piping requires xargs:

find -name '*.conf' -print0 | xargs -0 ls -l

-print0 and -0 ensure safe handling of filenames with special characters.

Practical Applications and Extensions

For specific requirements, ls parameters can be adjusted: ls -l --block-size=KB forces size display in KB. Combined with other find conditions like -type f (files only) or -size filters, more precise searches can be achieved.

In scripting contexts, output can be redirected to files:

find . -name '*.ear' -exec ls -lh "{}" \; > file_sizes.txt

This facilitates subsequent processing or logging.

Conclusion

The find -exec ls -lh combination strikes an optimal balance between functionality, compatibility, and readability, making it the preferred choice for most scenarios. Developers should select appropriate methods based on specific environments, paying attention to parameter escaping and quoting to ensure stable command execution.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.