Looking at this command, it finds all .txt files, sorts them by size, but then strips out the size information, showing only the file paths.
To modify it to include human-readable file sizes, here's the updated command:
find . -type f -name "*.txt" -printf "%s %p\n" | sort -n | while read size path; do
echo "$(numfmt --to=iec-i --suffix=B $size) $path"
done
This modification:
- Keeps the original find and sort operations unchanged
- Replaces the
awk command with a while read loop that:
- Reads the size and path from each line
- Uses
numfmt to convert bytes to human-readable format (e.g., 1.2KiB, 15.3MiB)
- Outputs both the human-readable size and the file path
The output will look like:
512B ./small.txt
1.5KiB ./medium.txt
2.3MiB ./large.txt
Alternative solution using only awk (if numfmt isn't available):
find . -type f -name "*.txt" -printf "%s %p\n" | sort -n | awk '{
size=$1;
path=substr($0, length($1)+2);
if (size < 1024) hr=size"B"
else if (size < 1048576) hr=sprintf("%.1fKiB", size/1024)
else if (size < 1073741824) hr=sprintf("%.1fMiB", size/1048576)
else hr=sprintf("%.1fGiB", size/1073741824)
print hr " " path
}'