รูปแบบขั้นสูงของ `grep` และ `find`
ใช้ regex ที่ซับซ้อนร่วมกับ `grep` และเกณฑ์ขั้นสูงร่วมกับ `find` เพื่อการค้นหาที่แม่นยำ
รูปแบบขั้นสูงของ `grep` และ `find` เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Basic Searches
Welcome back! In previous lessons, you've learned the basics of grep for filtering text and find for locating files.
Today, we'll dive into advanced patterns. This means using more powerful regular expressions with grep and sophisticated criteria with find to achieve highly precise searches.
Enhanced `grep` with ERE
The standard grep command uses Basic Regular Expressions (BRE). For more power, we often switch to Extended Regular Expressions (ERE).
- Use the
-Eoption (oregrep) to enable ERE. - ERE allows features like
+(one or more),?(zero or one),|(OR), and()for grouping without needing to escape them.
ERE Example: One or More (`+`)
The + metacharacter matches one or more occurrences of the preceding character or group. For example, colou+r would match "colour" and "colouur" but not "color".
Try running this example:
#!/bin/bash
# Create a test file
echo "color" > my_text.txt
echo "colour" >> my_text.txt
echo "colouur" >> my_text.txt
echo "Searching for 'colou+r':"
grep -E "colou+r" my_text.txt
# Clean up
rm my_text.txtERE Example: Zero or One (`?`)
The ? metacharacter matches zero or one occurrence of the preceding character or group. It's useful for optional characters.
For instance, colou?r would match "color" (zero 'u') and "colour" (one 'u').
Run this to see it in action:
#!/bin/bash
# Create a test file
echo "color" > my_text.txt
echo "colour" >> my_text.txt
echo "colouur" >> my_text.txt
echo "Searching for 'colou?r':"
grep -E "colou?r" my_text.txt
# Clean up
rm my_text.txtERE Example: OR Logic (`|`)
The | metacharacter acts as an OR operator, allowing you to match one pattern or another. Parentheses () are used for grouping patterns.
For example, (cat|dog)s matches "cats" or "dogs".
Here's a quick demo:
#!/bin/bash
# Create a test file
echo "apple" > fruits.txt
echo "banana" >> fruits.txt
echo "orange" >> fruits.txt
echo "grape" >> fruits.txt
echo "Searching for 'apple' OR 'orange':"
grep -E "apple|orange" fruits.txt
# Clean up
rm fruits.txtAdvanced `find`: Modification Time (`-mtime`)
Beyond just names, find can locate files based on when they were last modified. The -mtime option is key here.
-mtime N: Files modified exactly N*24 hours ago.-mtime +N: Files modified more than N*24 hours ago.-mtime -N: Files modified less than N*24 hours ago.
For example, find . -mtime -7 finds files modified in the last 7 days.
Advanced `find`: Access Time & Size
You can also search by access time (when a file was last read) and file size:
-atime N: Files last accessed N*24 hours ago.-size N[cwbkMG]: Files of a specific size.
Size units: c (bytes), w (two-byte words), b (512-byte blocks, default), k (KB), M (MB), G (GB).
Example: find . -size +1M finds files larger than 1MB.
#!/bin/bash
# Create a test file (1.5MB approx)
head -c 1500K /dev/urandom > large_file.bin
touch -a large_file.bin # Access now for -atime
echo "Finding files larger than 1MB:"
find . -size +1M -name "large_file.bin"
# Clean up
rm large_file.binCombining `find` Criteria
find commands are powerful because you can combine multiple criteria. By default, criteria are combined with an implicit AND.
You can also use:
-a: Explicit AND (often omitted).-o: OR operator.( ): Parentheses for grouping (must be escaped or quoted in shell).
Example: find . -type f -name "*.txt" -o -name "*.log" finds all .txt OR .log files.
#!/bin/bash
# Create test files
touch file1.txt
touch file2.log
touch file3.md
echo "Finding .txt OR .log files:"
find . \( -name "*.txt" -o -name "*.log" \)
# Clean up
rm file1.txt file2.log file3.md`find` and `-exec` for Actions
One of find's most powerful features is -exec, which allows you to run a command on each file or directory found.
-exec COMMAND {} \;: RunsCOMMANDfor each found item, replacing{}with the item's path. The\;marks the end of the command.-exec COMMAND {} +: RunsCOMMANDonce with all found items as arguments. More efficient for many files.
#!/bin/bash
# Create some empty files
touch old_report.txt
touch current_data.log
touch temp_notes.txt
echo "Listing all .txt files found:"
find . -name "*.txt" -exec ls -l {} \;
# Clean up
rm old_report.txt current_data.log temp_notes.txtAdvanced Search Challenge
You need to find all .log files that are larger than 100KB and were modified in the last 3 days, then print their names.
Which combination of find options would achieve this?
Recap: Precision Searches
Great job! You've leveled up your text and file searching skills.
- We explored Extended Regular Expressions (ERE) with
grep -E, using+,?, and|for more flexible pattern matching. - You learned to use advanced
findcriteria like-mtime,-atime, and-size. - We also covered combining these criteria with AND/OR logic and performing actions on found files using the powerful
-execoption.
These tools are essential for efficient system administration and scripting!
คำถามที่พบบ่อย
บทเรียน “รูปแบบขั้นสูงของ `grep` และ `find`” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รูปแบบขั้นสูงของ `grep` และ `find`” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบขั้นสูงของ `grep` และ `find`”
ใช้ regex ที่ซับซ้อนร่วมกับ `grep` และเกณฑ์ขั้นสูงร่วมกับ `find` เพื่อการค้นหาที่แม่นยำ คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “รูปแบบขั้นสูงของ `grep` และ `find`” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม
ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เชี่ยวชาญนิพจน์ทั่วไป (Regex)
- รูปแบบขั้นสูงของ `grep` และ `find`
- การใช้ `xargs` เชื่อมคำสั่ง
- การแก้ไขสตรีมในระดับขนาดใหญ่ด้วย sed และ tr