Courses/Command Line/Pipes and Redirection

    Pipes and Redirection

    Chain commands together with | and control where output goes with > and >>.

    What You'll Learn

    • Pipe operator (|) to chain commands
    • Output redirection (> and >>)
    • stdin, stdout, stderr streams
    • tee, here documents, and /dev/null

    The Pipe Operator |

    The pipe (|) is the most powerful concept in Unix. It takes the output of one command and feeds it as input to the next command.

    ๐Ÿ”ง Real-World Analogy: Think of pipes like a factory assembly line. Each worker (command) performs one specialised task and passes the result to the next worker. No single worker does everything โ€” they chain together to produce the final product.

    # Count files in current directory
    $ ls | wc -l
    
    # Find all .tsx files
    $ ls -R | grep '.tsx'
    
    # Top 10 most frequent errors
    $ cat server.log | grep ERROR | sort | uniq -c | sort -rn | head -10
    
    # Find largest files
    $ du -sh * | sort -rh | head -5

    ๐Ÿ’ก Pro Tip โ€” Unix Philosophy

    "Do one thing and do it well." Each Unix command is simple on its own, but when piped together, they become incredibly powerful. This is the Unix philosophy that makes the command line so effective.

    Pipes

    Chain commands together to filter and process data.

    Try it Yourself ยป
    JavaScript
    // Pipes โ€” Chain Commands Together
    console.log("=== The Pipe Operator | ===");
    console.log("Takes OUTPUT of one command and feeds it as INPUT to the next");
    console.log();
    
    // Simulated pipeline
    const files = [
      "README.md", "package.json", "tsconfig.json",
      "src/App.tsx", "src/main.tsx", "src/index.css",
      "src/components/Header.tsx", "src/components/Footer.tsx",
      "src/pages/Home.tsx", "src/pages/About.tsx",
      "public/index.html", "public/favicon.ico",
    ];
    
    console.log("$ ls -la | wc -l      
    ...

    Redirection

    Redirection controls where a command's input comes from and where its output goes:

    # Output redirection
    $ echo "Hello" > file.txt        # Write (overwrites!)
    $ echo "World" >> file.txt       # Append
    
    # Error redirection
    $ command 2> errors.log          # Errors to file
    $ command > output.log 2>&1      # Both stdout + stderr to file
    $ command &> all.log             # Shorthand for both
    
    # Discard output
    $ command > /dev/null 2>&1       # Silence everything

    โš ๏ธ Common Mistake

    Using > when you mean >>. A single > overwrites the file completely! Use >> to append. This is a common cause of data loss.

    Redirection

    Control where command output goes.

    Try it Yourself ยป
    JavaScript
    // Redirection โ€” Control Input/Output
    console.log("=== Output Redirection ===");
    console.log();
    console.log("$ echo 'Hello' > file.txt     (overwrite file)");
    console.log("$ echo 'World' >> file.txt    (append to file)");
    console.log();
    
    console.log("=== Standard Streams ===");
    console.log("Every command has 3 streams:");
    console.log("  stdin  (0) โ€” input  (keyboard by default)");
    console.log("  stdout (1) โ€” output (screen by default)");
    console.log("  stderr (2) โ€” errors (screen by default)");
    
    ...

    ๐Ÿ“‹ Quick Reference

    OperatorDescription
    cmd1 | cmd2Pipe output of cmd1 to cmd2
    > fileRedirect output (overwrite)
    >> fileRedirect output (append)
    2> fileRedirect errors to file
    &> fileRedirect all output to file
    | tee fileOutput to screen AND file

    ๐ŸŽ‰ Lesson Complete!

    You've mastered pipes and redirection โ€” the glue of Unix! Next: shell scripting to automate everything you've learned.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service