Bird
Raised Fist0
Linux CLIscripting~15 mins

tail -f for live log monitoring in Linux CLI - Mini Project: Build & Apply

Choose your learning style10 modes available
Using tail -f for Live Log Monitoring
📖 Scenario: You are a system administrator who needs to watch the latest entries of a server log file as they happen. This helps you quickly spot errors or important events without opening the file repeatedly.
🎯 Goal: Learn how to use the tail -f command to monitor a log file live in the terminal.
📋 What You'll Learn
Create a sample log file with specific lines
Add a helper variable for the log file name
Use tail -f command to follow the log file
Display the live output of the log file
💡 Why This Matters
🌍 Real World
System administrators and developers often need to watch log files live to quickly detect errors or important events as they happen.
💼 Career
Knowing how to use <code>tail -f</code> is a basic but essential skill for monitoring servers, debugging applications, and maintaining system health.
Progress0 / 4 steps
1
Create a sample log file
Create a file called app.log with these exact three lines:
INFO: Application started
WARNING: Low disk space
ERROR: Failed to connect to database
Linux CLI
Hint

Use echo -e with newline characters \n and redirect output to app.log.

2
Set a variable for the log file name
Create a variable called logfile and set it to the string app.log.
Linux CLI
Hint

Use logfile="app.log" to assign the filename to a variable.

3
Use tail -f to follow the log file
Use the command tail -f $logfile to follow the log file live.
Linux CLI
Hint

Use tail -f "$logfile" to watch the file as it updates.

4
Display the live output
Run the script and observe the output showing the three log lines live.
Linux CLI
Hint

Run the script in a terminal. You should see the three log lines displayed and the command will keep running to show new lines if added.

Practice

(1/5)
1. What does the command tail -f /var/log/syslog do?
easy
A. Copies the syslog file to another location
B. Deletes the syslog file permanently
C. Shows the last lines of the syslog file and updates live as new lines are added
D. Shows the first lines of the syslog file only once

Solution

  1. Step 1: Understand the tail command

    The tail command shows the last part of a file.
  2. Step 2: Understand the -f option

    The -f option makes tail keep running and show new lines as they are added to the file.
  3. Final Answer:

    Shows the last lines of the syslog file and updates live as new lines are added -> Option C
  4. Quick Check:

    tail -f = live log monitoring [OK]
Hint: Remember: -f means follow new lines live [OK]
Common Mistakes:
  • Thinking tail -f deletes or copies files
  • Confusing tail with head
  • Expecting tail -f to show the start of the file
2. Which of the following is the correct syntax to continuously monitor the file /var/log/auth.log using tail?
easy
A. tail -n /var/log/auth.log
B. tail /var/log/auth.log -f
C. tail -follow /var/log/auth.log
D. tail -f /var/log/auth.log

Solution

  1. Step 1: Identify correct option placement

    The option -f must come immediately after tail before the filename.
  2. Step 2: Verify option correctness

    -f is the correct option to follow the file live; -follow is invalid and -n requires a number.
  3. Final Answer:

    tail -f /var/log/auth.log -> Option D
  4. Quick Check:

    Correct syntax = tail -f /var/log/auth.log [OK]
Hint: Options come before filename; -f not -follow [OK]
Common Mistakes:
  • Placing options after filename
  • Using invalid option -follow
  • Using -n without a number
3. You run tail -f /var/log/app.log and see the following output after new log entries are added:
ERROR: Connection failed
INFO: Retrying connection
SUCCESS: Connected

What does this output indicate?
medium
A. The application is logging connection attempts and success live
B. The log file is empty and no new entries are added
C. The command failed to read the log file
D. The log file is being deleted while monitoring

Solution

  1. Step 1: Understand tail -f output

    The command shows new lines added live to the log file.
  2. Step 2: Interpret the log messages

    The messages show connection failure, retry, and success, indicating live logging of connection status.
  3. Final Answer:

    The application is logging connection attempts and success live -> Option A
  4. Quick Check:

    Live log updates = The application is logging connection attempts and success live [OK]
Hint: New lines mean live updates from the app log [OK]
Common Mistakes:
  • Thinking the log file is empty
  • Assuming command failed without error message
  • Confusing deletion with normal log updates
4. You try to run tail -f /var/log/app.log but get the error: tail: cannot open '/var/log/app.log' for reading: Permission denied. What should you do to fix this?
medium
A. Delete the log file and try again
B. Run the command with sudo to get permission
C. Use head -f instead
D. Restart the system to fix permissions

Solution

  1. Step 1: Understand the error message

    The error says permission denied, meaning the current user cannot read the file.
  2. Step 2: Fix permission issue

    Running the command with sudo gives temporary admin rights to read the file.
  3. Final Answer:

    Run the command with sudo to get permission -> Option B
  4. Quick Check:

    Permission denied fix = use sudo [OK]
Hint: Permission denied? Try sudo before the command [OK]
Common Mistakes:
  • Deleting the log file unnecessarily
  • Using head -f which is invalid
  • Restarting system without checking permissions
5. You want to monitor two log files live at the same time: /var/log/nginx/access.log and /var/log/nginx/error.log. Which command will correctly do this?
hard
A. tail -f /var/log/nginx/access.log /var/log/nginx/error.log
B. tail -f /var/log/nginx/access.log && tail -f /var/log/nginx/error.log
C. tail -f /var/log/nginx/access.log | tail -f /var/log/nginx/error.log
D. tail -f /var/log/nginx/access.log; tail -f /var/log/nginx/error.log

Solution

  1. Step 1: Understand how to monitor multiple files

    tail -f accepts multiple filenames to follow them simultaneously in one terminal.
  2. Step 2: Analyze each option

    tail -f /var/log/nginx/access.log /var/log/nginx/error.log correctly lists both files after tail -f. Using && or ; runs commands sequentially, so the second starts after the first ends. Piping two tail -f commands with | is invalid for this purpose.
  3. Final Answer:

    tail -f /var/log/nginx/access.log /var/log/nginx/error.log -> Option A
  4. Quick Check:

    Multiple files = list all after tail -f [OK]
Hint: List all files after tail -f to watch together [OK]
Common Mistakes:
  • Using && or ; runs commands one after another, not together
  • Piping two tail -f commands is invalid
  • Trying to monitor files separately in one terminal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard