#!/usr/bin/env bash # Verifies every factual claim made in ls-command-linux.html. # Self-contained: builds its own fixture under mktemp -d, cleans up on exit. # Claims that require GNU coreutils on a machine that does not have it are # reported as SKIP, never as PASS. set -u FAILED=0 pass() { printf 'PASS %s\n' "$1"; } fail() { printf 'FAIL %s\n' "$1"; FAILED=1; } skip() { printf 'SKIP %s\n' "$1"; } SCRATCH="$(mktemp -d)" trap 'rm -rf "$SCRATCH"' EXIT # ---------------------------------------------------------------- flavour --- LS_FLAVOUR=bsd if ls --version >/dev/null 2>&1; then case "$(ls --version 2>&1 | head -1)" in *"GNU coreutils"*) LS_FLAVOUR=gnu ;; esac fi echo "== ls-command-linux verification ==" echo "uname : $(uname -srm)" echo "ls : $LS_FLAVOUR ($(command -v ls))" echo cd "$SCRATCH" || exit 1 # --- Claim 1: ls -l shows MODIFICATION time, not creation time --------------- mkdir -p times && cd times || exit 1 touch born.txt sleep 2 echo "later" >> born.txt if stat -f '%SB' -t '%H:%M:%S' born.txt >/dev/null 2>&1; then BIRTH="$(stat -f '%SB' -t '%H:%M:%S' born.txt)" MTIME="$(stat -f '%Sm' -t '%H:%M:%S' born.txt)" elif stat -c '%W' born.txt >/dev/null 2>&1; then W="$(stat -c '%W' born.txt)" if [ "$W" = "0" ] || [ "$W" = "-" ]; then BIRTH="unavailable"; else BIRTH="$(date -d "@$W" +%H:%M:%S)"; fi MTIME="$(stat -c '%y' born.txt | cut -d' ' -f2 | cut -d. -f1)" else BIRTH="unavailable"; MTIME="unavailable" fi LS_SHOWN="$(ls -l -T born.txt 2>/dev/null | grep -oE '[0-9]{2}:[0-9]{2}:[0-9]{2}' | head -1)" [ -n "$LS_SHOWN" ] || LS_SHOWN="$(ls -l --time-style=+%H:%M:%S born.txt 2>/dev/null | grep -oE '[0-9]{2}:[0-9]{2}:[0-9]{2}' | head -1)" echo " birth time (stat) : $BIRTH" echo " mtime (stat) : $MTIME" echo " ls -l shows : $LS_SHOWN" if [ "$BIRTH" = "unavailable" ]; then skip "C1 birth time unavailable on this filesystem; cannot contrast with mtime" elif [ "$BIRTH" != "$MTIME" ] && [ "$LS_SHOWN" = "$MTIME" ]; then pass "C1 ls -l shows mtime ($MTIME), NOT the creation time ($BIRTH)" else fail "C1 expected ls -l to show mtime $MTIME (birth $BIRTH), it showed $LS_SHOWN" fi cd "$SCRATCH" || exit 1 # --- Claim 2: ls -1 still sorts; -f is the option that stops sorting --------- mkdir -p sortdir && cd sortdir || exit 1 # Create in deliberately non-alphabetical order. for n in zebra mango apple; do touch "$n.txt"; sleep 0.05; done touch .hidden ONE="$(ls -1 | tr '\n' ' ')" if [ "$ONE" = "apple.txt mango.txt zebra.txt " ]; then pass "C2a ls -1 output is alphabetical -- -1 changes layout only, it still sorts" echo " ls -1 -> $ONE" else fail "C2a ls -1 returned '$ONE', expected alphabetical order" fi F="$(ls -f | tr '\n' ' ')" if [ "$F" != "apple.txt mango.txt zebra.txt " ]; then pass "C2b ls -f returns unsorted directory order (and implies -a)" echo " ls -f -> $F" else fail "C2b ls -f produced sorted output: $F" fi case "$LS_FLAVOUR" in gnu) U="$(ls -U | tr '\n' ' ')" if [ "$U" != "apple.txt mango.txt zebra.txt " ]; then pass "C2c GNU ls -U does not sort" else fail "C2c GNU ls -U produced sorted output: $U" fi ;; *) if man ls 2>/dev/null | col -b | grep -q 'Use time when file was created for sorting'; then pass "C2c PORTABILITY TRAP: BSD ls -U means 'sort by creation time', not 'do not sort'" echo " BSD man ls: $(man ls 2>/dev/null | col -b | grep -m1 'Use time when file was created')" else fail "C2c could not confirm BSD -U meaning from man page" fi skip "C2d GNU 'ls -U = do not sort' and 'ls -f = -a -U': needs GNU coreutils; taken from the GNU ls(1) man page." ;; esac cd "$SCRATCH" || exit 1 # --- Claim 3: `ls | wc -l` is wrong on three axes ---------------------------- mkdir -p counting && cd counting || exit 1 touch a.txt b.txt c.txt # 3 visible regular files touch .env .secret # 2 hidden entries mkdir sub # 1 directory (not a file) touch "we ird.txt" # 1 file whose NAME contains a newline NAIVE="$(ls | wc -l | tr -d ' ')" CORRECT="$(ls -Aq | wc -l | tr -d ' ')" echo " real entries incl. hidden : 7 (a,b,c, .env,.secret, sub, 'weird.txt')" echo " ls | wc -l : $NAIVE" echo " ls -Aq | wc -l : $CORRECT" if [ "$NAIVE" = "6" ]; then pass "C3a ls | wc -l reports 6 for 5 visible entries (the newline filename is double-counted)" else fail "C3a ls | wc -l returned $NAIVE, expected 6" fi if [ "$CORRECT" = "7" ]; then pass "C3b ls -Aq | wc -l reports the true entry count of 7" else fail "C3b ls -Aq | wc -l returned $CORRECT, expected 7" fi if ls | grep -q '^sub$'; then pass "C3c the count includes the directory 'sub' -- it is not a count of files" else fail "C3c expected 'sub' in plain ls output" fi if ls | grep -q '^\.env$'; then fail "C3d .env unexpectedly listed by plain ls" else pass "C3d plain ls omits the 2 hidden entries entirely" fi cd "$SCRATCH" || exit 1 # --- Claim 4: `ls -d */` omits hidden dirs, includes symlinks, errors if none - mkdir -p globdir && cd globdir || exit 1 mkdir visible realdir .secretdir ln -s realdir linkdir touch notadir.txt GLOB="$(ls -d */ 2>/dev/null | tr '\n' ' ')" if [ "$GLOB" = "linkdir/ realdir/ visible/ " ]; then pass "C4a ls -d */ lists the symlink-to-directory and omits the hidden directory" echo " ls -d */ -> $GLOB" else fail "C4a ls -d */ returned '$GLOB'" fi mkdir -p emptydir && cd emptydir || exit 1 err="$(ls -d */ 2>&1)"; rc=$? if [ $rc -ne 0 ]; then pass "C4b ls -d */ fails (non-zero exit) when the directory contains no subdirectories" echo " rc=$rc $(printf '%s' "$err" | head -1)" else fail "C4b ls -d */ unexpectedly succeeded in an empty directory" fi cd "$SCRATCH" || exit 1 # --- Claim 5: --color portability -------------------------------------------- mkdir -p colordir && cd colordir || exit 1 touch f.txt if ls --color=auto . >/dev/null 2>&1; then if [ "$LS_FLAVOUR" = gnu ]; then pass "C5a GNU ls accepts --color=auto" else pass "C5a NOTE: this non-GNU ls ALSO accepts --color=auto (see article caveat)" echo " $(command -v ls) accepted --color=auto; usage line advertises it:" echo " $(ls --nosuchopt 2>&1 | grep -o '\[--color=when\]' | head -1)" fi else pass "C5a this ls REJECTS --color=auto (GNU-only option here)" fi if ls -G . >/dev/null 2>&1; then pass "C5b ls -G (BSD colour flag) is accepted here" else skip "C5b ls -G not accepted (this is GNU ls, where -G means --no-group)" fi cd "$SCRATCH" || exit 1 # --- Claim 6: -h uses powers of 1024 ----------------------------------------- mkdir -p sizes && cd sizes || exit 1 # One million bytes is the clean discriminator: 1000000/1048576 = 0.95 MiB, so a # 1024-based -h must show it in K. A 1000-based (SI) -h would show "1.0M". head -c 1000000 /dev/zero > one_million_bytes head -c 1048576 /dev/zero > one_mebibyte H1M="$(ls -lh one_million_bytes | awk '{print $5}')" HMI="$(ls -lh one_mebibyte | awk '{print $5}')" echo " 1,000,000 bytes -> $H1M | 1,048,576 bytes -> $HMI" case "$H1M" in *M*) fail "C6a 1,000,000 bytes shown as $H1M -- that would mean -h uses powers of 1000" ;; *K*) pass "C6a ls -lh uses powers of 1024: 1,000,000 bytes shows as $H1M, not 1.0M" ;; *) fail "C6a unexpected -h output for 1,000,000 bytes: $H1M" ;; esac case "$HMI" in 1.0M|1M) pass "C6b 1,048,576 bytes (one mebibyte) is what actually displays as $HMI" ;; *) fail "C6b 1,048,576 bytes displayed as $HMI, expected 1.0M" ;; esac if ls -l --si one_million_bytes >/dev/null 2>&1; then SI="$(ls -l --si one_million_bytes | awk '{print $5}')" pass "C6c --si is available and shows 1,000,000 bytes as $SI (true SI)" else skip "C6c --si (true SI, powers of 1000): needs GNU coreutils; not available here." fi cd "$SCRATCH" || exit 1 # --- Claim 7: --time=birth is GNU-only --------------------------------------- mkdir -p birth && cd birth || exit 1 touch b.txt if ls -l --time=birth b.txt >/dev/null 2>&1; then pass "C7 ls --time=birth is available here" else skip "C7 ls --time=birth (creation time via ls): needs GNU coreutils; use stat instead." fi cd "$SCRATCH" || exit 1 # --- Claim 8: don't parse ls -- the safe alternatives ------------------------- mkdir -p parsing && cd parsing || exit 1 touch "spaced name.txt" "we ird2.txt" plain.txt N_FIND="$(find . -maxdepth 1 -type f -print0 | tr -dc '\0' | wc -c | tr -d ' ')" if [ "$N_FIND" = "3" ]; then pass "C8a find -print0 counts all 3 files correctly regardless of spaces or newlines" else fail "C8a find -print0 counted $N_FIND, expected 3" fi N_GLOB=0 for f in ./*; do [ -f "$f" ] && N_GLOB=$((N_GLOB+1)); done if [ "$N_GLOB" = "3" ]; then pass "C8b a plain shell glob loop also counts all 3 correctly" else fail "C8b glob loop counted $N_GLOB, expected 3" fi cd "$SCRATCH" || exit 1 echo if [ "$FAILED" -eq 0 ]; then echo "RESULT: all executed checks passed." else echo "RESULT: FAILURES PRESENT." fi exit "$FAILED"