Tuesday, October 17, 2023

"Find" without deeper hits

Sometimes I want find to ignore any deeper laying hits. Basically I want to find all the directories and files containing `subject` unless a high directory is also a hit.

Say I would want to find all my projects touching on subject I often use the command below and get the following result.

 $ find . -iname '*subject*'
./some_project/subject.h
./another_project/file_with_subject.txt
./subject_project ./subject_project/subject_main.c
./subject_project/subject_other.c
./subject_project/subject_data.bin
./subject_project/file_with_subject.txt
./last_project/touching_subject.yaml

Todat I finally looked into this and found the answer:

$ find . -iname '*subject*' -prune
./some_project/subject.h
./another_project/file_with_subject.txt
./subject_project
./last_project/touching_subject.yaml

From the man page on -prune: ¨if the file is a directory, do not descend into it."

No comments:

Post a Comment