How to Pipe Command Output to Other Commands in Linux

Introduction

The Linux command line is a powerful tool that allows you to quickly and easily perform tasks. One of the most useful features of the command line is the ability to pipe command output to other commands. This allows you to take the output of one command and use it as the input for another command. This can be used to quickly and easily perform complex tasks that would otherwise require multiple commands. In this article, we will discuss how to pipe command output to other commands in Linux.

How to Pipe Command Output to Other Commands in Linux

Piping is a powerful feature in Linux that allows you to send the output of one command to another command for further processing. This is done by using the pipe (|) symbol.

For example, if you want to list all the files in a directory and then sort them by size, you can use the following command:

ls -l | sort -n

The output of the ls -l command is sent to the sort -n command, which sorts the output by size.

You can also use the pipe symbol to send the output of one command to a file. For example, if you want to save the output of the ls -l command to a file called “filelist.txt”, you can use the following command:

ls -l > filelist.txt

This will save the output of the ls -l command to the file “filelist.txt”.

Piping is a very useful feature in Linux and can be used to combine multiple commands to perform complex tasks.

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 
OR
# 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:

Tree – 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.

How to Pipe Command Output to Other Commands in Linux

Piping is a powerful feature in Linux that allows you to redirect the output of one command to another command. This allows you to chain multiple commands together and create powerful scripts that can automate tasks. In this article, we will show you how to pipe command output to other commands in Linux.

What is Piping?

Piping is a feature in Linux that allows you to redirect the output of one command to another command. This is done by using the pipe (|) character. The pipe character takes the output of one command and passes it as input to another command.

How to Use Piping

Using piping is simple. All you need to do is type the command you want to run, followed by the pipe character, followed by the command you want to pipe the output to. For example, if you wanted to list all the files in a directory and then sort them by size, you could use the following command:

ls -l | sort -n

The first command (ls -l) will list all the files in the current directory and the second command (sort -n) will sort them by size. The output of the first command is then piped to the second command.

Examples of Piping

Here are some examples of how you can use piping to combine multiple commands:

  • To list all the files in a directory and then search for a specific file:
    ls -l | grep filename
  • To list all the files in a directory and then count the number of files:
    ls -l | wc -l
  • To list all the files in a directory and then delete them:
    ls -l | xargs rm

Conclusion

Piping is a powerful feature in Linux that allows you to combine multiple commands and automate tasks. We hope this article has helped you understand how to pipe command output to other commands in Linux.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?