Why sort -n Picks the Wrong Folder After du -sh
Zusammenfassung
Pixel’s announcement demonstrates how du -sh | sort -rn can choose the wrong camera folder because sort -n reads the leading digits and ignores K and M suffixes. In a test with four folders, it picked porch at 884K instead of garage at 12M, highlighting a practical pitfall for cleanup scripts.
DEV Community
du -sh | sort -rn Picked an 884K Camera Folder Over a 12M One: sort -n Reads the Digits, Not the Unit
A cleanup job for a phone camera needs to know which folder of clips is using the most space, so it can prune that one first. The usual one-liner is du -sh piped through sort -rn and head -n 1. I built four camera folders of known size and ran it. It named the second-smallest folder. The setup Four folders, one clip each. The bytes are random so nothing compresses, and the sizes are chosen so du -sh has to print both K and M: #!/bin/bash
Four camera folders, one clip each, random bytes so nothing compresses or dedupes.
set -eu
rm -rf clips && mkdir clips && cd clips
mkdir driveway porch garage nursery
head -c 1500000 /dev/urandom > driveway/clip_0001.mp4
head -c 900000 /dev/urandom > porch/clip_0001.mp4
head -c 12000000 /dev/urandom > garage/clip_0001.mp4
head -c 300000 /dev/urandom > nursery/clip_0001.mp4
The script under test is the one-liner, wrapped so a cleanup job could call it: #!/bin/bash
Name the camera folder using the most space, so a cleanup job knows where to prune.
set -euo pipefail
cd clips
du -sh -- * | sort -rn | head -n 1
What it said #!/bin/bash
./mk.sh
./biggest.sh
echo "--- every folder, du -sh"
(cd clips && du -sh -- *)
884K porch
- -- every folder, du -sh
1.5M driveway
12M garage
300K nursery
884K porch
The garage folder holds the largest clip by far, and du -sh reports it correctly as 12M. The one-liner still picked porch at 884K. The sizes are right. The ranking is wrong. Why sort -n reads 884K as bigger than 12M sort -n compares a number at the start of each line, and the GNU manual spells out what that number is: "The number begins each line and consists of optional blanks, an optional ‘-’ sign, and zero or more digits possibly separated by thousands separators, optionally followed by a decimal-point character and zero or more digits." There is no letter in that list. The K and the M are not part of the number, so sort -n stops reading right before them. It ranks 884 above 300, above 12, above 1.5, with no way to know that one of those means megabytes. The manual's own advice for a surprising result is "try the --debug option to see what happened." It underlines the part of each line that the sort key used: #!/bin/bash
cd clips
du -sh -- * | sort -n --debug
sort: text ordering performed using ‘C.UTF-8’ sorting rules
1.5M>driveway
12M>garage
__
300K>nursery
884K>porch
The first underline on each line is the key, and it stops before the unit every time: three characters for 1.5, two for 12, three for 300 and 884. The second underline is the whole line, which sort falls back on only when two keys tie. Nothing errors here. du exits 0, sort exits 0, head exits 0, and pipefail has nothing to report. A job that trusts the first line would prune the porch camera and leave the garage folder to keep growing. sort -h fixes this case GNU sort has an option made for this output. -h (--human-numeric-sort) sorts "first by numeric sign (negative, zero, or positive); then by SI suffix", and only after that by the number. A value with M outranks any value with K, whatever the digits say: #!/bin/bash
cd clips
echo "--- sort -h"
du -sh -- * | sort -h
echo "--- sort -rh | head -n 1"
du -sh -- * | sort -rh | head -n 1
- -- sort -h
300K nursery
884K porch
1.5M driveway
12M garage
- -- sort -rh | head -n 1
12M garage
That is the correct order, and sort -rh | head -n 1 names the garage folder. For a single du -sh call, this is the whole fix. Where sort -h stops being enough The manual is specific about when -h works. It sorts values that are "consistently scaled to the nearest suffix", which is why it handles "the output of any single invocation of the df, du, or ls commands" run with their human-readable options. Those conditions matter once size lines come from more than one place, for example a log that collects sizes over time from a mix of commands. du -s -BK prints every size in kilobytes, however large, so its 11724K is...

Powered by Readybot.io