IntroToLinux

9. Performance and Best Practices

Efficient Pipelines:

# Less efficient
cat file.txt | grep pattern | head -10

## Watch out! Next two commands will have a different output!

# Returns the first 10 lines from file.txt containing the pattern
grep pattern file.txt | head -10

# Return only the line containing the pattern in the first 10 lines of file.txt
head -10 file.txt | grep pattern  # If pattern is common

ℹ️ Memory Considerations:

ℹ️ Error Handling:

# Exit on pipeline failure
set -o pipefail

# Check for command existence
if command -v somecommand > /dev/null; then
    somecommand | process_data
else
    echo "somecommand not found"
fi

Next: → Troubleshooting
Previous: ← Advanced Pipe Concepts
Lesson Home: ↑ Lesson 4: Redirects & Pipes Course Home: ⌂ Introduction to Linux