LINUX · Text tools · 5 min

Keep the error lines

Search a log and save only the relevant events.

Not completed

Your task

Write only the lines beginning ERROR from access.log into errors.log, in their original order. Keep access.log unchanged.

What you’re aiming for

  • errors.log contains exactly the two ERROR lines.
  • The original access.log is unchanged.

1. Prepare your practice folder

Use a Linux terminal, such as Ubuntu or WSL on Windows. These examples use Linux file permissions. In WSL, create your practice folder in your Linux home directory (~).

Create a new, empty folder for this exercise. If that name already exists, choose another new name. Continue only after you’re inside the new folder.

NEW PRACTICE FOLDER
mkdir opsrung-linux-filter-logs && cd opsrung-linux-filter-logs

Run these setup commands in that folder:

SETUP — RUN ONCE
printf '%s\n' 'INFO started' 'ERROR disk-full' 'WARN slow' 'ERROR permission-denied' > access.log

2. Try the task

Work toward the goal above. Start by inspecting the files or resources, then use a hint if you need one.

Hint 1

Read access.log before filtering it.

Hint 2

A regular expression beginning with ^ matches the start of a line.

Hint 3

Redirect grep's output to a different file, not back into its input.

3. Review your result

Use these commands to look at your result and compare it with the goals. The website does not run or verify them.

REVIEW IN YOUR TERMINAL
cat errors.log
cat access.log

What you should see

  • cat errors.log shows exactly two lines: ERROR disk-full, then ERROR permission-denied.
  • cat access.log still shows all four original lines, including INFO and WARN.

If your result differs: Write to errors.log, not access.log. Redirecting output back into the source log can truncate it before grep reads it.

Compare with one possible solution
ONE POSSIBLE SOLUTION
cat access.log
grep '^ERROR ' access.log > errors.log
cat errors.log

Other approaches are fine if you reach the same goal. Opening this solution does not mark the exercise complete.

Finished this exercise?

You decide when you’re done. Your choice is saved in this browser, and you can change it later.