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.

 

The simplest syntax for using a pipe, which you might have already seen in commands in many of out Linux tutorials, is as follows. But you can build a longer command line with several 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 dmesg command to head command.

$ dmesg | head

 

How to Use xargs to Run Commands

 

In this example, the second command converts multi-line output into a single line using xargs.

$ ls -1 *.sh
$ ls -1 *.sh | 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

 

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

 

How to Use Tee with Commands in Linux

 

This example shows how to send command output to standard output and save to a file; the command below allows you to view 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

 

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 in tee and xargs man pages.

$ man xargs
$ man tee

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)