Skip to content

2.3 Pipes, Redirections, and Filters

Control where output goes.

  • stdin (0): Input.
  • stdout (1): Output.
  • stderr (2): Errors.
  • >: Overwrite file with output.
    echo "Hello" > file.txt
  • >>: Append output to end of file.
    echo "World" >> file.txt
  • 2>: Redirect errors only.
    ls /nonexistent 2> errors.log

Take the stdout of the command on the left and pass it as stdin to the command on the right.

command1 | command2 | command3

Commands designed to process text streams.

CommandFunctionExample
grepSearch for patterns`cat log.txt
`lsgrep`List specific files
lessPager (scrollable view)`cat hugefile.txt
headFirst 10 lineshead -n 5 file.txt
tailLast 10 linestail -f /var/log/syslog (Follow live!)
sortSort linessort names.txt
uniqRemove adjacent duplicates`sort names.txt
wcWord Countwc -l (Count lines)

Count how many running processes are owned by “root”:

ps aux | grep "^root" | wc -l

Commands to protect, sort, merge, and transform text.

CommandFunctionExample
cutRemove sections from each line of filescut -d: -f1 /etc/passwd (Get usernames)
trTranslate or delete characters`echo “hello”
sortSort lines of text filessort file.txt
uniqReport or omit repeated lines`sort file.txt
wcPrint newline, word, and byte countswc -l file.txt

sed is a stream editor for filtering and transforming text.

  • Substitute (Search & Replace):

    # Replace the first occurrence of 'foo' with 'bar' in each line
    sed 's/foo/bar/' file.txt
    
    # Replace ALL occurrences ('g' for global)
    sed 's/foo/bar/g' file.txt
    
    # Edit the file in-place ('-i')
    sed -i 's/foo/bar/g' file.txt
  • Delete Lines:

    # Delete the 3rd line
    sed '3d' file.txt
    
    # Delete lines containing "error"
    sed '/error/d' file.txt