[บันทึกกันลืม] วิธีหาไฟล์ที่มีคำที่ต้องการ โดยแสดงเฉพาะ ชื่อไฟล์เท่านั้น

โจทย์ ต้องการหาไฟล์ที่มีคำว่า text_pattern จากทุกไฟล์ในไดเรคทอรี่ /path/to/ ที่มีนามสกุล .ipynb แต่ต้องไม่มีคำว่า checkpoint ใน path หรือชื่อไฟล์

find /path/to -name "*.ipynb" ! -name "*checkpoint*" -type f -print0 | while read -r -d '' i; do
    if grep -q "text_pattern" "$i"; then
         echo "$i"  # Output only the filename if the pattern is found
    fi;
done

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.