Linux Piping, Redirection, tee and xargs Commands Explained

One of the most powerful features of the Linux command line is the ability to combine multiple commands to perform complex tasks. Linux follows a simple philosophy: each command does one specific job. Instead of creating one command that does everything, Linux lets you connect smaller commands and pass data between them. That is where Linux piping comes in. By connecting commands through pipes, you can build powerful command-line pipelines that process data efficiently, automate repetitive tasks, and solve problems with just a few lines of code. This tutorial explains how Linux piping and redirection work. Explore practical examples of the pipe operator, tee, and xargs commands to build powerful Linux command-line pipelines.

In this tutorial, we'll explore:

  • What piping is and how it works in Linux.
  • How to connect commands using the pipe (|) operator.
  • How to combine piping with output redirection.
  • How the tee command sends output to multiple destinations.
  • How to use xargs with commands that don't accept standard input.

1. What Is Piping in Linux?

In Linux, piping is the process of connecting the standard output (stdout) of one command to the standard input (stdin) of another command. Normally, command output appears on the terminal. However, instead of displaying that output, you can send it directly to another command for further processing. This is accomplished using the pipe operator (|).

command1 | command2

Here's what happens:

  1. command1 executes and generates output.
  2. The pipe operator captures its standard output.
  3. That output becomes the standard input of command2.
  4. The second command processes the received data.

This allows you to combine simple commands into more advanced operations without creating unnecessary intermediate files.

2. Extracting Data Using the cut Command

Linux provides several commands for processing text. One of the most useful is cut. It extracts specific sections of text from files or standard input.

cut [OPTIONS] [FILE]

Let's begin with the date command:

date

Example output:

Mon Nov 2 20:08:00 IST 2026

Suppose we want to extract only the day of the week. We can first redirect the date command's output into a file.

date > date.txt

Now examine the file:

cat date.txt

To extract the first field, use the following command:

cut --delimiter=" " --fields=1 date.txt

Output:

Mon

Let's understand the options:

OptionPurpose
cutExtracts selected fields from text.
--delimiter=" "Uses a space as the field separator.
--fields=1Extracts the first field.
date.txtSpecifies the input file.

Although this method works, it creates an unnecessary intermediate file. We can achieve the same result more efficiently using piping.

3. Using the Pipe (|) Operator in Linux

Instead of saving command output into a file, we can connect commands directly using the pipe operator.

date | cut --delimiter=" " --fields=1

Output:

Mon

Here's how the pipeline works:

  • The date command generates the current date and time.
  • The pipe operator sends its output to cut.
  • The cut command receives the data through standard input.
  • The cut command extracts the first field.
Remember: A pipe connects the standard output of the command on the left to the standard input of the command on the right.

Can We Pipe More Than Two Commands?

Yes! Linux allows you to combine multiple commands into a single pipeline.

command1 | command2 | command3 | command4

Each command processes the data received from the previous command and passes its output to the next one.

4. Combining Linux Piping with Output Redirection

We can also combine piping with output redirection. Instead of displaying the final output on the terminal, we can save it into a file using the greater-than operator (>).

date | cut --delimiter=" " --fields=1 > day.txt

To verify the result, execute:

cat day.txt

Output:

Mon

The final output is saved in day.txt instead of appearing on the terminal.

Important: The > operator creates a file if it doesn't exist and overwrites existing contents. Use >> when you want to append output.
date >> date.txt

5. Understanding the Limitations of Standard Piping

By default, a command's standard output is directed to one destination. It can be displayed on the terminal, redirected into a file, or passed to another command. But what if we want to save command output into a file while simultaneously passing it to another command?

Consider this command:

date > date.txt | cut --delimiter=" " --fields=1

This doesn't work as expected. The output is saved into date.txt, but the cut command receives no date information. The reason is that the output redirection operator sends the date command's standard output directly to the file. Therefore, the pipe receives no standard output from that command. To send the same data to both destinations, we need the tee command.

6. The tee Command in Linux: Send Output to Multiple Destinations

The tee command allows you to copy standard input to a file while simultaneously sending the same data to standard output. Think of tee as a T-junction in a pipeline. One copy of the data is saved into a file, while the original data continues through the pipeline.

Basic Syntax of the tee Command

command1 | tee filename | command2

Practical Example: Using tee with date and cut

Let's save the date command's complete output into date.txt while passing the same output to cut.

date | tee date.txt | cut --delimiter=" " --fields=1

Output:

Mon

Here's what happens:

  1. date generates the current date and time.
  2. tee receives the output.
  3. tee saves a copy into date.txt.
  4. The same data continues to the cut command.
  5. cut extracts the first field.

Why Is the tee Command Useful?

The tee command is particularly useful when troubleshooting complex pipelines. You can insert tee between commands to capture intermediate output and examine what happens at different stages.

To append output instead of overwriting a file, use -a:

date | tee -a date.txt

7. What Happens When a Command Doesn't Accept Standard Input?

Not every Linux command accepts information through standard input. Some commands primarily use command-line arguments.

Consider the echo command:

echo "Hello Linux"

Output:

Hello Linux

Now let's try piping the output of date into echo:

date | echo

The echo command does not display the date because it does not process the incoming standard input.

Let's provide an argument:

date | echo "Hello Linux"

Output:

Hello Linux

The echo command displays its supplied argument but still ignores the output coming from date.

Important: A command must read standard input to process data received directly through a pipe. Otherwise, you may need a tool such as xargs.

8. The xargs Command in Linux: Convert Piped Input into Arguments

The xargs command reads data from standard input and converts it into command-line arguments for another command. This makes it possible to work with commands that don't directly accept standard input.

Basic Syntax of xargs

command1 | xargs command2

Here's how it works:

  1. The first command generates output.
  2. The pipe sends the output to xargs.
  3. xargs reads the incoming data.
  4. It converts the data into command-line arguments.
  5. Those arguments are passed to the second command.

Practical Example: Using xargs with date and echo

We want to pass the output of date to echo. Since echo doesn't process standard input directly, we use xargs.

date | xargs echo

Example output:

Mon Nov 2 20:08:00 IST 2026

Here's what happens:

  • date generates the current date and time.
  • The pipe sends that output to xargs.
  • xargs converts it into command-line arguments.
  • echo receives those arguments and displays them.

In simple terms, xargs acts as a bridge between commands that produce standard output and commands that expect command-line arguments.

Why Is xargs Important in Linux?

The xargs command is widely used in Linux system administration and automation. It is particularly useful when working with filenames, directory names, and other values.

For example:

find . -name "*.log" | xargs ls -l

This searches for log files in the current directory and passes their names to ls -l. For filenames containing spaces or special characters, use:

find . -name "*.log" -print0 | xargs -0 ls -l

The null-delimited approach handles filenames more safely.

9. Linux Piping, tee, and xargs: Quick Command Reference

Command / OperatorPurposeExample
|Connects commandsdate | cut -d " " -f 1
>Overwrites a file with outputdate > date.txt
>>Appends output to a filedate >> date.txt
cutExtracts selected fieldscut -d " " -f 1 date.txt
teeCopies output to a file and standard outputdate | tee date.txt
xargsConverts input into command-line argumentsdate | xargs echo

10. Key Takeaways: What You Should Remember

1. Piping connects Linux commands

The pipe (|) operator sends the standard output of one command to the standard input of another.

2. Redirection changes the output destination

The > operator redirects standard output into a file. It can prevent a command's output from reaching the next command when used in a pipeline.

3. The tee command duplicates output

The tee command saves a copy of output into a file while simultaneously passing the data through the pipeline.

4. Not every command accepts standard input

Some commands, such as echo, primarily work with command-line arguments.

5. The xargs command converts input into arguments

The xargs command makes it possible to pass piped data as command-line arguments to another command.

Frequently Asked Questions (FAQs)

1. What is piping in Linux?

Piping in Linux connects two or more commands using the pipe (|) operator. It sends the standard output of one command to the standard input of another command.

2. What does the pipe (|) symbol do in Linux?

The pipe symbol connects commands and transfers data between them. For example:

date | cut -d " " -f 1
3. What is the difference between piping and redirection in Linux?

Piping sends command output directly to another command. Redirection sends command output to a file or reads input from a file.

4. What is the purpose of the tee command in Linux?

The tee command copies standard input to a file and standard output. It allows you to save command output while continuing to pass the same data through a pipeline.

5. What is the difference between tee and redirection?

Redirection sends output to a file instead of standard output. The tee command saves a copy to a file while also sending the data to standard output.

6. What does xargs do in Linux?

The xargs command reads data from standard input and converts it into command-line arguments for another command.

7. Can we use multiple pipes in a single Linux command?

Yes. Linux allows you to connect multiple commands using several pipe operators.

cat file.txt | sort | uniq | wc -l
8. Does every Linux command support piping?

No. A command must be capable of reading standard input to process data directly from a pipe. Commands that rely on command-line arguments can often be used with xargs.

Conclusion

Linux piping is much more than a way to connect commands. It is one of the fundamental concepts that makes the Linux command line flexible, efficient, and powerful. By understanding the pipe operator, output redirection, tee, and xargs, you can process information, manage command output, troubleshoot pipelines, and automate complex operations without writing lengthy scripts. The real power of Linux comes from combining simple commands to accomplish bigger tasks.

Keep practising, and see you in the next tutorial!

ComputerNetworkingNotes Ubuntu Tutorials Linux Piping, Redirection, tee and xargs Commands Explained

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com