How to Pipe Command Output to Other Commands in Linux

While using the command line, you can directly pass the output of one program (for example a tool that generates some system information or statistics) as input for another program (such as text-filtering or pattern-searching tools like grep, sed, or awk, for further processing), using a pipeline.

Two of the most important command line utilities that can be used with pipelines to build command lines are:

  • xargs – reads streams of data from standard input, then generates and executes command lines.
  • tee – reads from standard input and writes simultaneously to standard output and one or many files. It’s more of a redirection command.

Sending Command Output to Another Command in Linux

In this simple article, we will describe how to build and execute multiple commands from standard input using pipes, tee, and xargs commands in Linux.

The simplest syntax for using a pipe, which you might have already seen in commands in many of our Linux tutorials, is as follows. But you can build a longer command line with several Linux commands.

$ command1 args | command2 args 
# command1 args | command2 args | command3 args ...

Below is an example of using a pipeline to pass the output of the dmesg command to the head command.

$ dmesg | head
Pass Command Output to Another Command
Pass Command Output to Another Command

Xargs – Pass Command Output to Other Command

In this example, the ls command output will pass to another command called xargs that concatenate multiple lines of output to one line as shown.

$ ls -1 *.sh
$ ls -1 *.sh | xargs
Run Commands Using Xargs
Run Commands Using Xargs

To count the number of lines/words/characters in each file in a list, use the commands below.

$ ls *.sh | xargs wc -l	    #count number of lines in each file
$ ls *.sh | xargs wc -w	    #count number of words in each file
$ ls *.sh | xargs wc -c	    #count number of characters in each file
$ ls *.sh | xargs wc	    #count lines, words, and characters in each file
Count File Words Using Xargs
Count File Words Using Xargs

The command below finds and recursively deletes the directory named All in the current directory.

$ find . -name "All" -type d -print0 | xargs -0 /bin/rm -rf "{}"

The find command with the option -print0 action enables printing of the full directory path on the standard output, followed by a null character and -0 xargs flag deals with space in filenames and an rm -rf command to delete a directory.

You can find other practical xargs command usage examples in these articles:

Tee – Send Command Output to Other Command and Save to File

This example shows how to send command output to standard output and save it to a file; the command below allows you to view the top running processes by highest memory and CPU usage in Linux.

$ ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head | tee topprocs.txt
$ cat  topprocs.txt
Save Command Output to File
Save Command Output to File

To append data in an existing file(s), pass the -a flag.

$ ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head | tee -a topprocs.txt 

You can find more information on the tee and xargs man pages.

$ man xargs
$ man tee

That’s all! Do not forget to check out our special article: A – Z Linux Commands – Overview with Examples.

In this article, we described how to generate command lines using pipelines; xargs, and tee commands. You can ask any questions or share any thoughts via the feedback form below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.


Leave a Reply
  1. “Tree – Send Command Output to Other Command and Save to File”

    I think that the first word in the title should read “Tee“, not “Tree“.


Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.