Linux

Follow log files in real-time

tail -f log.txt &

echo 'New log entry' >> log.txt

echo 'Another entry' >> log.txt

echo 'ERROR: Connection failed' >> log.txt

jobs

kill %1

What This Does

Pro Tips

  1. tail -f monitors logs continuously (Ctrl+C to stop)
  2. & runs command in background
  3. jobs shows background processes
  4. kill %1 stops job #1
  5. Use for: tail -f /var/log/syslog (system logs)
  6. tail -F follows even if file is rotated/recreated

Common Mistakes to Avoid

Search file contents with grep

grep ERROR log.txt

grep berry fruits.txt

grep -i error log.txt

grep -n ERROR log.txt

grep -v ERROR log.txt

What This Does

Pro Tips

  1. grep is case-sensitive by default
  2. -i for case-insensitive search
  3. -n shows line numbers
  4. -v inverts match (show what doesn't match)
  5. -r searches directories recursively
  6. grep ‘pattern’ file.txt (quotes if pattern has spaces)

Common Mistakes to Avoid

Find files by name

find . -name '*.txt'

find . -name 'log*'

find ~ -name 'fruits.txt'

find . -type f

find . -type d

What This Does

Pro Tips

  1. find . starts search in current directory
  2. find ~ searches entire home directory
  3. -name is case-sensitive
  4. -iname for case-insensitive
  5. -type f = files only
  6. -type d = directories only
  7. ALWAYS quote patterns: ‘*.txt’ not *.txt

Common Mistakes to Avoid

Find files by name

find . -name '*.txt'

find . -name 'log*'

find ~ -name 'fruits.txt'

find . -type f

find . -type d

What This Does

Pro Tips

  1. find . starts search in current directory
  2. find ~ searches entire home directory
  3. -name is case-sensitive
  4. -iname for case-insensitive
  5. -type f = files only
  6. -type d = directories only
  7. ALWAYS quote patterns: ‘*.txt’ not *.txt

Common Mistakes to Avoid

Combine grep with other commands

cat log.txt | grep ERROR

ls -l | grep txt

find . -name '*.txt' -exec grep ERROR {} \;

What This Does

Pro Tips

  1. chains commands together
  2. Output of left becomes input of right
  3. Common: ls grep pattern
  4. Common: cat file grep search
  5. find -exec is powerful but complex (use carefully)
  6. Can chain multiple pipes: cat file grep ERROR grep -v DEBUG

Common Mistakes to Avoid

Practice real-world scenarios

echo -e 'DEBUG: Starting\nINFO: Processing\nERROR: Failed\nWARN: Retrying\nINFO: Success' > app.log

grep ERROR app.log

grep -E 'ERROR|WARN' app.log

tail -n 5 app.log | grep -v DEBUG

What This Does

Pro Tips

  1. grep -E ‘pattern1 pattern2’ matches either pattern
  2. Pipe tail and grep for recent errors only
  3. grep -v excludes unwanted lines
  4. Real-world: tail -f /var/log/app.log grep ERROR (monitor for errors)

Common Mistakes to Avoid

Clean up practice files

cd ~

rm -r search-practice

ls

What This Does

Pro Tips

  1. Always pwd before rm -r
  2. Consider keeping practice directories for reference

Common Mistakes to Avoid