Pipes and Redirection in Linux

Pipes and redirection are fundamental mechanisms in Linux that enable efficient command-line operations. The pipe operator (|) allows the output of one command to serve as input to another command, creating powerful command chains. Redirection operators (>, >>, <) direct command output to files or use file content as command input, providing flexible data flow control in the terminal.

These features are essential for Linux system administration, automation, and efficient data processing workflows.

Redirection Operators

Linux Redirection Operators > >> < 2> Redirect output (overwrite) Redirect output (append) Redirect input (from file) Redirect errors (STDERR) Output Redirection ls > file.txt echo "text" >> file.txt Input Redirection grep "text" < file.txt Error Redirection ls nofile 2> error.txt

Output Redirection Examples

Redirecting to a New File

The > operator redirects command output to a file, creating it if it doesn't exist or overwriting existing content

$ ls -lhrt
total 16K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

$ ls -lhrt > redirection.txt

$ cat redirection.txt
total 16K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
-rw-rw-r-- 1 rian rian    0 Feb 11 22:43 redirection.txt
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Appending to an Existing File

The >> operator appends output to an existing file without destroying previous content

$ cat 123.txt
123

$ ls -lhrt >> 123.txt

$ cat 123.txt
123
total 24K
drwxrwxr-x 3 rian rian 4.0K Feb 11 22:22 zip-unzip
drwxrwxr-x 2 rian rian 4.0K Feb 11 22:37 bash
-rw-rw-r-- 1 rian rian  269 Feb 11 22:43 redirection.txt
-rw-rw-r-- 1 rian rian    4 Feb 11 22:53 123.txt
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cat-more-less
drwxrwxr-x 2 rian rian 4.0K Feb 11  2016 cmd-bash

Input Redirection

The < operator redirects file content as input to a command

$ grep -i "123" < 123.txt
123
-rw-rw-r-- 1 rian rian    4 Feb 11 22:53 123.txt

Error Redirection

Use 2> to redirect only error messages (STDERR) to a file

$ ls nofile.txt 2> error.txt

$ cat error.txt
ls: cannot access 'nofile.txt': No such file or directory

To redirect both STDOUT and STDERR to the same file, use > file.txt 2>&1

$ ls nofile.txt bash > output.txt 2>&1

$ cat output.txt
ls: cannot access 'nofile.txt': No such file or directory
bash:
bash_function.sh
bash_pass_arg.sh
bash_return.sh

Pipe Operations

The pipe operator (|) connects commands, passing the output of one as input to the next

$ ls | grep -i "bash"
bash
cmd-bash

Multiple pipes can be chained together for complex operations

$ ls | grep -i "bash" | wc -l
2

Common Use Cases

Operation Command Example Description
Save command output ps aux > processes.txt Store process list in file
Log file analysis grep "ERROR" < app.log Search errors in log file
Count filtered results ps aux | grep nginx | wc -l Count nginx processes
Sort and save ls -la | sort > sorted_files.txt Sort directory listing

Conclusion

Pipes and redirection are powerful Linux tools that enable efficient command chaining and flexible data flow management. The pipe operator facilitates command composition, while redirection operators provide precise control over input/output streams, making complex operations simple and automation tasks more effective.

Updated on: 2026-03-17T09:01:38+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements