KKAIROS
Practical guideIllustrative3 min read

find: list files by name and sort the result

find lists files by name pattern; sort makes the order deterministic. The run shows only -name and the effect of sort.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-17-A: find output order can vary; sort makes it deterministic

Setup for this run, inside a throwaway directory (mktemp -d):

mkdir -p d; for n in c a b; do printf 'x\n' > d/$n.txt; done

Command:

find d -name '*.txt' | wc -l; find d -name '*.txt' | sort | head -3

Recorded output:

3
d/a.txt
d/b.txt
d/c.txt

TE-17-B: find with a name pattern lists only the matching path

Setup for this run, inside a throwaway directory (mktemp -d):

mkdir -p d; for n in c a b; do printf 'x\n' > d/$n.txt; done

Command:

find d -name 'a*.txt'

Recorded output:

d/a.txt

Exit status: 0. This status and the output match what was written before the run.

TE-17-C: expected non-zero exit: find on a folder that does not exist prints an error and exits with status 1

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

find nosuchdir -name x

Recorded output: none, standard output was empty.

Standard error from the same run:

find: ‘nosuchdir’: No such file or directory

Exit status: 1. This status and the output match what was written before the run.

TE-17-B: by the setup, d holds a.txt, b.txt and c.txt. find d -name 'a*.txt' printed d/a.txt only. TE-17-C: find nosuchdir -name x printed find: ‘nosuchdir’: No such file or directory to standard error and exited with status 1.

Limits

Only -name and the effect of sort are tested here. Filtering by type, size or depth, and actions such as -exec, are not tested on this page.

Ad

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-17-A | find output order can vary; sort makes it deterministic | 0 | PASS | 72851a287951 | | TE-17-B | find with a name pattern lists only the matching path | 0 | PASS | d7e9d1307492 | | TE-17-C | expected non-zero exit: find on a folder that does not exist prints an error and exits with status 1 | 1 | PASS | 92d833aeb344 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides