brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -o needs parentheses, or -print only applies to the last name

Find
~/tips/tip-find-or-needs-parentheses

find . -name '*.txt' -o -name '*.log' -print

Explains why find with two -name tests and -print shows only one kind of file.

Two name tests joined with -o, then an action at the end. It looks right, but it only shows half the files.

find . -name '*.txt' -o -name '*.log' -print

This prints only the .log files. find joins tests with an implicit “and”, and “and” binds stronger than -o. So the command reads as: name is .txt, or (name is .log and print). The .txt files match the first part, but nothing tells find to print them. Without any action, find adds -print for the whole expression, which is why the version without -print does seem to work.

Group the alternatives with escaped parentheses:

find . \( -name '*.txt' -o -name '*.log' \) -print

The backslashes keep the shell from treating the parentheses as its own syntax, and there must be spaces around them.

This matters most with real actions. -name a -o -name b -delete deletes only the b files, and -exec behaves the same way. With -delete, getting the grouping wrong in the other direction can delete more than you wanted, so I’d always run the command with -print first and look at the list.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link