Finding files with find · Поиск файлов с помощью find
Searching the whole tree
lsshows one folder.findsearches a folder and everything inside it, however deep:
.means "start in the current folder".findlists every file and folder it can reach.
Поиск по всему дереву
lsпоказывает одну папку.findищет папку и всё внутри неё, независимо от глубины:
find .
.означает «начать с текущей папки».findперечислит все файлы и папки, до которых может добраться.
Find by name
- Usually you want files matching a pattern. Use
-name:
- This finds every
.txtfile in the tree — even ones buried three folders down. - Quote the pattern (
"*.txt") so the shell passes it tofinduntouched.
Поиск по имени
- Обычно вам нужны файлы, соответствующие шаблону. Используйте
-name:
find . -name "*.txt"
- Это найдет каждый файл
.txtв дереве — даже те, что закопаны глубоко. - Оберните шаблон в кавычки (
"*.txt"), чтобы оболочка передала его вfindбез изменений.
Why it is handy
- "Where did I put that file?" —
findanswers it in one line. - Combined with pipes, you can act on the results — for example count them with
wc -l.
Почему это удобно
- "Где я положил тот файл?" —
findотвечает на этот вопрос в одну строку. - В сочетании с конвейерами можно выполнить действия над результатами — например, подсчитать их с помощью
wc -l.
Common mistakes
find . -name "*.txt"searches by name from the current folder down.- Quote the pattern so the shell does not expand it first.
Распространенные ошибки
find . -name "*.txt"ищет по имени от текущей папки вниз.- Оберните шаблон в кавычки, чтобы оболочка не расширила его заранее.
find walks the tree · find обходит дерево
find searches the whole tree below a directory. · find ищет по всему дереву ниже указанной директории.
Find every .txt file, even deep inside subfolders, with find . -name "*.txt". (. means "start from here".) · Найдите все файлы .txt, даже глубоко вложенные в подпапки, с помощью команды find . -name "*.txt". (. означает «начать поиск отсюда».)
You can also start the search from a subfolder. Find just the .txt files inside docs (and below) with find docs -name "*.txt". · Вы также можете начать поиск из подпапки. Найдите только файлы .txt внутри каталога docs (и ниже) с помощью команды find docs -name "*.txt".