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 -not -path excludes a folder, and -name only sees the last part

Find
~/tips/tip-find-not-path-exclude-folder

find . -type f -name '*.php' -not -path '*/vendor/*' -not -path './.git/*'

Filters out whole folders from the result by matching the full path.

-name matches the file name only, so it cannot say anything about the folder that holds the file.

find . -type f -name '*.php' -not -path '*/vendor/*' -not -path './.git/*'

-path matches the whole path with shell patterns, and the star also matches slashes. That lets you drop whole subtrees from the result. It still walks those folders, so for large trees -prune is faster. A slash in a -name pattern never matches anything, and GNU find warns about it. Quote the pattern, otherwise the shell expands the star before find sees it. Put -not -path before the expensive tests, and remember to add a leading ./ if the starting point is a dot.

Further reading: find manual page

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