Redirect, Pipe, and Filter

Redirect, Pipe, and Filter

Bitnesia • Aug 16, 2026 235 •ID

In the previous chapter, we learned how to get help in Linux using man, --help, info, and tldr. Now it is time to wrap up the command line section with one of the Linux terminal's greatest strengths: its ability to chain commands together. Instead of running each command separately, we can redirect a command's output to a file, use a file's contents as input, or pipe a command's output directly into the next command. This concept is called redirection and pipe, and it forms the foundation of efficient terminal usage. This chapter also introduces text filters such as grep, sort, and sed, which will help us process text quickly. After understanding this chapter, we will be able to combine simple commands into a much more productive workflow.

18.1 Standard Input, Output, and Error

Every process in Linux has three default data streams known as standard streams. The terminal automatically connects these three streams as soon as a command runs. Each stream has an identification number called a file descriptor, which we will encounter again when learning redirection operators for stderr.

StreamNameFile DescriptorDescription
stdinStandard Input0Input stream read by the program, usually from the keyboard
stdoutStandard Output1Normal output stream from the program's work
stderrStandard Error2Output stream for error messages

When we run a command, the system prints its results to stdout and error messages to stderr. Under normal conditions, both appear on the terminal screen, appearing combined. However, they are actually separate streams and can be redirected independently. Understanding this is essential because redirection operates on these streams.

18.2 Output Redirection

Output redirection allows us to redirect the output of a command from the terminal screen to a file. The operator used is the greater-than sign (>).

18.2.1 >: Writing to a File (Overwrite)

The > operator writes command output to a file. If the file does not exist, the system creates it. If the file already exists, this operator overwrites all of its content.

ls -l > daftar.txt

The above command writes the result of ls -l to the file daftar.txt. Nothing appears on the screen because all output has been redirected to the file. To view its contents, run cat daftar.txt.

Because > overwrites existing files, be careful of the risk of accidentally losing important file contents. A common trap for beginners is running commands like sort daftar.txt > daftar.txt to "sort" the file itself. The shell opens and truncates the destination file first before sort gets a chance to read its content, resulting in an empty file. If you want to sort a file and save the result back to the same file, use a temporary file first, for example sort daftar.txt > sementara.txt && mv sementara.txt daftar.txt.

18.2.2 >>: Appending to a File (Append)

The >> operator works almost the same as >, but instead of overwriting, it appends output to the end of the file. If the file does not exist, the system creates it first.

echo "Baris baru" >> daftar.txt

Every time the command above runs, the text Baris baru is added to the end of daftar.txt without deleting previous contents. The >> operator is very useful for logging or aggregating output from multiple commands into a single file. Sysadmins frequently use this pattern to collect output from multiple backup processes or cron jobs into a single daily log file.

18.2.3 Redirecting Standard Error

Besides stdout, we can also redirect stderr separately using the 2> operator, corresponding to the stderr file descriptor number discussed earlier. For example:

perintah-yang-salah 2> error.txt

The above command saves error messages to error.txt, while normal output (if any) still appears on screen. Just like >> for stdout, we can also use 2>> to append error messages to the end of a file without overwriting it.

Sometimes we want to combine both stdout and stderr into the same file. To do this, use the 2>&1 operator after the stdout redirection:

perintah > semua-output.txt 2>&1

The order here is important: 2>&1 means "redirect stderr to the same location as current stdout", so this line must be written after > semua-output.txt. As a shorter shortcut, modern Bash shells provide the &> operator, which redirects stdout and stderr simultaneously in one step:

perintah &> semua-output.txt

Finally, if we want to discard certain output completely without saving it anywhere, Linux provides the special file /dev/null. Writing to /dev/null is like throwing data into a "black hole"; the data disappears instantly and consumes no disk space. This pattern is commonly found in scripts or service configurations to hide unnecessary messages:

perintah > /dev/null 2>&1

The above example discards all normal output and error messages from perintah, so nothing appears in the terminal.

18.3 Input Redirection

Input redirection is the opposite of output redirection. The operator used is the less-than sign (<). Instead of reading input from the keyboard, the command reads input from a file.

18.3.1 <: Reading from a File

sort < daftar.txt

The above command makes sort read the content of daftar.txt as its input, then prints the sorted result to the screen. Functionally, this is equivalent to sort daftar.txt because sort accepts filenames as arguments. However, not all commands accept filenames, which is where input redirection becomes useful.

18.4 Pipe |: Chaining Commands

The pipe is the feature that makes the Linux terminal so powerful. The | operator takes stdout from one command and feeds it as stdin to the next command, without using an intermediary file. Note that by default, a pipe only redirects stdout, not stderr, from the first command.

ls -l | sort

The above command runs ls -l, and then sort immediately sorts its result. No temporary file is created in between. We can chain multiple pipes in a single line to build complex data processing pipelines.

cat daftar.txt | grep "Dokumen" | wc -l

This example displays file contents, filters lines containing the word Dokumen, and then counts the number of resulting lines. Each command does one simple thing, and pipes combine them into a larger solution. Developers often build patterns like this when inspecting application logs to count how many times a specific error occurs.

18.5 Common Text Filters

A filter is a command that reads input, processes it, and writes the output. Filters are ideal for chaining with pipes. Here are some of the most commonly used text filters.

18.5.1 grep: Searching Text

grep searches for lines matching a specific pattern in a file or command output. It is one of the most frequently used commands in Linux.

grep "error" log.txt
grep -i "warn" log.txt

The -i option makes the search case-insensitive. We can also invert results with -v to display lines that do not match the pattern.

18.5.2 sort: Sorting Output

sort arranges text lines alphabetically or numerically.

sort nama.txt
sort -n angka.txt

The -n option sorts numerically, while -r reverses the sorting order. Without extra options, sort arranges alphabetically.

18.5.3 uniq: Removing Duplicates

uniq removes consecutive duplicate lines. Because it only works on adjacent lines, uniq is usually chained after sort so all duplicates are grouped together.

sort nama.txt | uniq

To count the occurrence frequency of each line, add the -c option.

18.5.4 wc: Counting Lines, Words, and Characters

wc stands for word count. It counts the number of lines, words, and bytes in a file or input.

wc laporan.txt
wc -l laporan.txt

The -l option displays line count only, -w for word count, and -c for byte count. For text containing multi-byte characters like emojis or accented letters, the byte count may differ from the actual character count; in that case, use the -m option to count characters instead of bytes.

18.5.5 cut: Cutting Columns

cut extracts specific parts from each line, such as columns or character ranges. It is very useful for processing column-structured data.

cut -d ":" -f 1 /etc/passwd
cut -c 1-5 nama.txt

The first example cuts the /etc/passwd file using a colon delimiter and extracts the first column, which is the user list. The second example extracts the first five characters of each line.

18.5.6 awk: Simple Text Processing

awk is a compact yet powerful text processing language. For simple tasks, awk is frequently used to extract specific columns.

awk '{print $1}' data.txt

The command above prints the first column of every line in data.txt. Although it looks similar to cut, awk is far more flexible because it can perform calculations and process data conditionally.

18.5.7 sed: Text Substitution

sed stands for stream editor. It edits text streams line by line. The most common usage is text substitution with the s/old/new/ command.

sed 's/lama/baru/' file.txt
sed -i 's/lama/baru/' file.txt

The first example displays substitution results to the screen without changing the file, making it safe for previews. When the -i option is used, sed directly writes changes to the file and provides no undo mechanism. Before running sed -i on important files, create an automatic backup by adding a backup extension, such as sed -i.bak 's/lama/baru/' file.txt, keeping the original file preserved as file.txt.bak.

18.6 Operator Summary

The following table summarizes all redirection and pipe operators discussed in this chapter as a quick reference you can revisit anytime:

OperatorFunction
>Writes stdout to a file (overwrites old content)
>>Appends stdout to the end of a file
<Reads stdin from a file
2>Writes stderr to a file (overwrites old content)
2>>Appends stderr to the end of a file
2>&1Redirects stderr to the same destination as stdout
&>Redirects stdout and stderr simultaneously to a file
|Pipes stdout of the first command as stdin to the next command

Redirection, pipes, and filters are three complementary skills that form the core of productive terminal usage. Once you get used to chaining commands with pipes, you will realize that many seemingly complex tasks can actually be solved with a single line of command built from simple parts. Practice this skill regularly, as almost all advanced material in this series builds upon it.

This chapter concludes all basic command line material in Part V. In the next chapter, we will move to Part VI and start learning how to edit files directly from the terminal using nano, the most beginner-friendly terminal-based text editor.