กำลังค้นหาไดเรกทอรี
จากบทสรุปที่แสดงใน man page ฉันจะบอกว่าใช่มันสามารถประมวลผลไดเรคทอรีได้ แต่การดูสวิตช์มันไม่สามารถมองหาไฟล์ที่มีพื้นฐานมาจากแพทเทิร์น findสำหรับสิ่งที่คุณจะต้องขอความช่วยเหลือ คำสั่งackมีตัวเลือก--files-from=FILEเพื่อให้สามารถป้อนรายการไฟล์findได้
สรุป
ack [options] PATTERN [FILE...]
ack -f [options] [DIRECTORY...]
การใช้
--files-from=FILE
The list of files to be searched is specified in FILE. The list of
files are separated by newlines. If FILE is "-", the list is
loaded from standard input.
มี--ignore-file=ตัวเลือกที่อาจให้สิ่งที่คุณต้องการ แต่ดูเหมือนว่าจะใช้ความเจ็บปวดเล็กน้อย
--ignore-file=FILTERTYPE:FILTERARGS
Ignore files matching FILTERTYPE:FILTERARGS. The filters are
specified identically to file type filters as seen in "Defining
your own types".
การค้นหาไฟล์เฉพาะประเภท
อีกวิธีเดียวที่ฉันสามารถจินตนาการถึงการทำสิ่งนี้ผ่านทางackคือการใช้--typeสวิตช์:
--type=[no]TYPE
Specify the types of files to include or exclude from a search.
TYPE is a filetype, like perl or xml. --type=perl can also be
specified as --perl, and --type=noperl can be done as --noperl.
If a file is of both type "foo" and "bar", specifying --foo and
--nobar will exclude the file, because an exclusion takes
precedence over an inclusion.
หากต้องการดูว่ามีประเภทใดให้บ้าง:
$ ack --help-types | grep -E "perl|cpp"
รูป ตัวอย่างเช่นทั้ง --type = perl และ --perl ทำงาน - [ไม่] cpp .cpp .cc .cxx .m .hpp .hh .h .h .hxx - [ไม่] objcpp .mm .h. - [ไม่] perl .pl .pm .pod .t .psgi; การจับคู่บรรทัดแรก /^#!.*\bperl/ - [ไม่] perltest .t
ตัวอย่าง
ค้นหาไฟล์ Perl ทั้งหมดตามทั้งชื่อไฟล์ (* .pm, * .pl, * .t และ * .pod) และบรรทัด shebang
$ ack -f --type perl
examples/iwatch/iwatch/iwatch
examples/nytprof_perl/bad.pl
ค้นหาไฟล์ C ++ ทั้งหมด:
$ ack -f --type=cpp
Shared/Scanner.h
Shared/Sorter.h
Shared/DicomHelper.cpp
Shared/DicomDeviationWriter.h
ค้นหา foo ใน bar * .c
ดังนั้นคุณจะบรรลุสิ่งที่คุณต้องการได้อย่างไร คุณจะต้องใช้findวิธีนี้:
$ find adir -iname "bar*.c" | ack --files-from=- foo
adir/bar1.c
1:foo
2:foo
adir/dir1/bar1.c
1:foo
2:foo
นอกจากนี้คุณยังสามารถใช้ackความสามารถในการค้นหาไฟล์ที่ตรงกับรูปแบบที่กำหนดในชื่อไฟล์ของพวกเขา (โดยใช้-g <pattern>) จากนั้นส่งผ่านรายการนี้ไปยังการเรียกackใช้ครั้งที่สอง-xหรือ--files-from=-..
การใช้-x:
$ ack -g '\bbar.*.c$' | ack -x foo
bar1.c
1:foo
2:foo
dir1/bar1.c
1:foo
2:foo
การใช้-files-from=-:
$ ack -g '\bbar.*.c$' | ack --files-from=- foo
bar1.c
1:foo
2:foo
dir1/bar1.c
1:foo
2:foo
ไม่ว่าในกรณีใดเราจะจับคู่ชื่อไฟล์ที่คุณต้องการใช้ regex นี้:
\bbar.*.c$
ตรงนี้ไฟล์ที่มีชื่อเป็นbar.*cและสิ้นสุดหลังจากการ.cใช้จุดสิ้นสุดของเส้นยึด, $. \bนอกจากนี้เรายังมองเพื่อให้แน่ใจว่าชื่อตัวละครที่มีขอบเขตการใช้ สิ่งนี้จะล้มเหลวสำหรับไฟล์ที่มีอักขระขอบเขตเช่น$bar.cหรือ%bar.cตัวอย่าง
find . -name "bar*.c" -exec ack foo {} \;? ไม่มีอะไรพิเศษเกี่ยวกับเรื่องgrepคุณสามารถใช้ใด ๆ-execคำสั่งด้วยหาของ