• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

How to Run Commands from Standard Input Using Tee and Xargs in Linux

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
10
VIEWS
Share on FacebookShare on Twitter

Contents

  1. How to Use xargs to Run Commands
  2. How to Use Tee with 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.

Read Also: Learn The Basics of How Linux I/O (Input/Output) Redirection Works

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.

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 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 Run Commands from Standard Input Using Tee and Xargs in Linux

Pass Command Output to Another Command

How to Use xargs to Run Commands

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

$ ls -1 *.sh
$ ls -1 *.sh | xargs
How to Run Commands from Standard Input Using Tee and Xargs in Linux

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
How to Run Commands from Standard Input Using Tee and Xargs in Linux

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

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

  1. How to Copy a File to Multiple Directories in Linux
  2. Rename All Files and Directory Names to Lowercase in Linux
  3. 4 Ways to Batch Convert Your PNG to JPG and Vice-Versa
  4. 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions

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
How to Run Commands from Standard Input Using Tee and Xargs in Linux

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

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

Understanding Shutdown, Poweroff, Halt and Reboot Commands in Linux

Next Post

Kurly – An Alternative to Most Widely Used Curl Program

Next Post

Kurly – An Alternative to Most Widely Used Curl Program

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet