Two of the most important command line utilities 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.

Syntax for using a pipe:

$ command1 args | command2 args 
OR
$ command1 args | command2 args | command3 args ...

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 "{}"

Where,

-print0 action enables printing of the full directory path on the standard output, followed by a null character

-0 xargs flag deals with space in filenames.

How to Use Tee with Commands in Linux

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 


That's it !!

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket