Pipes and Redirection in Command-Line Tools Quiz

Explore essential concepts of pipes and redirection in CLI tools with this quiz, designed for users aiming to improve their command-line workflow. Understand correct syntax, practical usage scenarios, and common pitfalls in managing input and output streams.

  1. Understanding Pipe Usage

    If you want to pass the sorted output of list.txt to a command that counts unique lines, which of the following is the correct command sequence?

    1. sort list.txt | uniq
    2. sort list.txt > uniq
    3. sort | uniq list.txt
    4. sort > list.txt | uniq

    Explanation: The correct answer uses the pipe operator to pass the output of 'sort list.txt' directly into 'uniq,' counting unique lines. Using '>' in 'sort list.txt > uniq' attempts to write the file 'uniq,' not execute a command. 'sort | uniq list.txt' incorrectly places the filename argument. 'sort > list.txt | uniq' saves the sorted output to the file and then pipes an empty stream to 'uniq.' Only the first option properly pipelines the commands.

  2. Redirecting Standard Output

    Which command correctly redirects the standard output of 'echo Hello' into a file named greetings.txt, replacing any existing content?

    1. echo Hello > greetings.txt
    2. echo Hello >> greetings.txt
    3. echo Hello | greetings.txt
    4. echo Hello < greetings.txt

    Explanation: The '>' operator redirects the output of 'echo Hello' to write into greetings.txt, replacing any existing content. '>>' appends rather than replaces content. The pipe symbol '|' is used to connect commands, not files. '<' is used for redirecting input from a file, not output into a file. Therefore, only '>' is correct in this context.

  3. Combining Pipes and Redirection

    How can you count the number of lines containing the word 'error' in logs.txt and save the result to result.txt?

    1. grep 'error' logs.txt | wc -l > result.txt
    2. grep 'error' logs.txt > wc -l result.txt
    3. grep 'error' | wc -l > logs.txt result.txt
    4. wc -l 'error' logs.txt | > result.txt

    Explanation: The correct sequence pipes the filtered lines from 'grep' into 'wc -l', and then uses '>' to redirect the count to result.txt. The second option incorrectly redirects the output of 'grep' to a command. Option three places the file and output in the wrong order. Option four is not valid syntax, as '>' must follow a command before specifying a file.

  4. Stderr Redirection Syntax

    Which command correctly redirects only the error messages from 'ls missingfile.txt' to error.log, leaving standard output untouched?

    1. ls missingfile.txt 2> error.log
    2. ls missingfile.txt > error.log
    3. ls missingfile.txt &> error.log
    4. ls missingfile.txt 1> error.log

    Explanation: '2>' targets standard error (stderr) for redirection, which ensures only error messages are written to error.log. Using '>' redirects standard output (stdout), not errors. The combined '&>' redirects both stdout and stderr. '1>' just redirects stdout. Thus, only '2>' achieves redirection of errors alone.

  5. Appending Output with Redirection

    Given the need to preserve previous entries, what is the correct command to add the output of 'date' to the end of a file called time-stamps.log?

    1. date >> time-stamps.log
    2. date > time-stamps.log
    3. date | time-stamps.log
    4. date < time-stamps.log

    Explanation: The '>>' operator appends output to the end of a file, rather than replacing it, which is crucial for logs. The single '>' operator overwrites the file. The pipe operator '|' works between commands, not files. The '<' operator is for redirecting input, not writing or appending output. For this reason, only the append operator '>>' is suitable here.