Intermediate · 10 min · practise every command in the built-in terminal

Pipes and Redirection

This is where Linux becomes truly powerful. You can chain commands together.

Output Redirection

command > file.txt      # Write output to file (overwrite)
command >> file.txt     # Append output to file
command 2> error.txt    # Redirect errors to file
command &> all.txt      # Redirect both output and errors

Input Redirection

command < file.txt      # Read input from file

/dev/null — The black hole

command > /dev/null          # Discard output
command 2>/dev/null          # Discard errors
command &>/dev/null          # Discard everything

Pipes — Chaining commands

The | character sends output of one command as input to the next:

ls -la | grep '.txt'                  # List only .txt files
cat /etc/passwd | grep '/bin/bash'    # Find bash users
ps aux | grep nginx | wc -l           # Count nginx processes
history | tail -20                    # Last 20 commands
ls -la | sort -k5 -n                  # Sort by file size

tee — Write and pass through

command | tee output.txt    # Save output to file AND print it

Practical pipeline example

cat /etc/passwd | cut -d: -f1 | sort | head -10

This: reads passwd → extracts usernames → sorts them → shows first 10

Practice tasks

  1. Write the output of 'ls -la' to a file called filelist.txt
  2. List files in /etc and pipe through grep to find .conf files
  3. Count how many .conf files are in /etc using ls, grep, and wc
  4. Append a line to filelist.txt without overwriting it
  5. Save a command's output to a file while also seeing it on screen

Commands in this lesson

Text Processing