Lessons · Terminal · finding files by name
Finding files by name
find folder -name 'pattern' walks every folder below and prints the paths that match. ls looks at one folder; find looks at the tree.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Where is the config file in this project? Which of these two hundred folders holds the test data? find answers in one line.
How to think about it
Start from the narrowest folder you can, quote the pattern, and pipe into sort or wc -l when the list is long.
Worked example
mkdir -p src/deep && touch src/a.txt src/deep/b.txt src/c.mdA small tree.
find src -name '*.txt' | sortsrc/a.txt and src/deep/b.txt: the .md is left out.
find src -name '*.md'Only src/c.md: the pattern is what decides, and it looks at every level.
Your turn
Find every .log file below the current folder.
find . '*.log'
Try it at a real prompt
The trap
find . -name *.log without quotes: the shell expands the star first, and find is handed a list of names it did not ask for.
Practise finding files by name on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.