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 -fprint writes several result lists in one pass

Find
~/tips/tip-find-fprint-several-lists

find /srv/archive \( -name '*.txt' -fprint txt.list \) -o \( -name '*.gz' -fprint gz.list \)

Sorts search results into separate files without walking the folder tree twice.

Walking a big tree is the slow part of find. When you want two lists, say all text files and all archives, running find twice reads every folder twice.

-fprint writes the matches to a file instead of the screen, and you can use it more than once in one command:

find /srv/archive \( -name '*.txt' -fprint txt.list \) -o \( -name '*.gz' -fprint gz.list \)

Every file is tested once. If it ends in .txt, its path goes into txt.list, otherwise find tries the second test and puts .gz files in gz.list. The parentheses keep each name test together with its own output file.

There’s also -fprint0 for NUL-separated output, which I find handy if you want to feed the list to xargs -0 later, and -fprintf, which works like -printf with a file. So one pass can give you a size list and a name list at once.

The output files are created as soon as find starts, even if nothing matches, and existing files with those names get emptied first. So pick names that don’t exist yet. And don’t write the lists inside the folder you’re searching: they become files that the next run of the same command will find.

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