KKAIROS
Practical guideIllustrative3 min read

find: -maxdepth, -type f and -size +1c narrow the result

find can limit depth, filter by type and filter by size. The run shows -maxdepth 1, -type f and -size +1c over three files.

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-31-A: find options compose: -maxdepth, -type and -size narrow the result

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

mkdir -p d/sub; printf '12345\n' > d/big.txt; printf '1' > d/small.txt; printf 'x\n' > d/sub/deep.txt

Command:

echo all=$(find d -type f | wc -l); echo depth1=$(find d -maxdepth 1 -type f | wc -l); echo size=$(find d -type f -size +1c | wc -l)

Recorded output:

all=3
depth1=2
size=2

TE-31-B: -maxdepth 1, -type f and -size +1c together keep only the big file in the top folder

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

mkdir -p d/sub; printf '12345\n' > d/big.txt; printf '1' > d/small.txt; printf 'x\n' > d/sub/deep.txt

Command:

find d -maxdepth 1 -type f -size +1c

Recorded output:

d/big.txt

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

TE-31-C: expected non-zero exit: a negative -maxdepth value is rejected

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

mkdir -p d

Command:

find d -maxdepth -1

Recorded output: none, standard output was empty.

Standard error from the same run:

find: Expected a positive decimal integer argument to -maxdepth, but got ‘-1’

Exit status: 1. This status matches what was written before the run. The expected error wording was corrected after the first run, because find words its -maxdepth error differently than the first prediction. The wording of this find error is specific to GNU find 4.8.0.

TE-31-B: by the setup, d/big.txt has 6 bytes, d/small.txt has 1 byte and d/sub/deep.txt has 2 bytes. With -maxdepth 1, -type f and -size +1c, only d/big.txt was printed. TE-31-C: find d -maxdepth -1 exited with status 1 and printed find: Expected a positive decimal integer argument to -maxdepth, but got ‘-1’ to standard error.

Limits

Other size units, other type values and actions such as -exec are not tested here. Not tested on this page: the positional options -daystart, -follow and -regextype, the options -depth, -mindepth, -mount, -noleaf and -xdev, the tests -name, -iname, -path, -regex, -empty, -newer, -mtime, -perm, -user and -group, the actions -delete, -print0, -printf, -prune, -quit and -ls, and the operators -o, -not and parentheses. The names come from find --help in GNU findutils 4.8.0, which lists them under those headings. No run here used them.

Ad

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-31-A | find options compose: -maxdepth, -type and -size narrow the result | 0 | PASS | 86ed3ad51c9d | | TE-31-B | -maxdepth 1, -type f and -size +1c together keep only the big file in the top folder | 0 | PASS | 69a6f07b04c1 | | TE-31-C | expected non-zero exit: a negative -maxdepth value is rejected | 1 | PASS | 0f7b7ec1e517 |

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