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.
[ You might also like: 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 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.
<code>$ command1 args | command2 args OR # command1 args | command2 args | command3 args ...</code>
Below is an example of using a pipeline to pass the output of the dmesg command to the head command.
<code>$ dmesg | head</code>
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.
<code>$ ls -1 *.sh $ ls -1 *.sh | xargs</code>
To count the number of lines/words/characters in each file in a list, use the commands below.
<code>$ 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</code>
The command below finds and recursively deletes the directory named All
in the current directory.
<code>$ find . -name "<strong>All</strong>" -type d -print0 | xargs -0 /bin/rm -rf "{}"</code>
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:
- How to Copy a File to Multiple Directories in Linux
- Rename All Files and Directory Names to Lowercase in Linux
- 4 Ways to Batch Convert Your PNG to JPG and Vice-Versa
- 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions
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.
<code>$ ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head | tee topprocs.txt $ cat topprocs.txt</code>
To append data in an existing file(s), pass the -a
flag.
<code>$ ps -eo cmd,pid,ppid,%mem,%cpu --sort=-%mem | head | tee -a topprocs.txt </code>
You can find more information on the tee and xargs man pages.
<code>$ man xargs $ man tee</code>
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.
The above is the detailed content of How to Pipe One Command Output to Other Command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

In this article, we will learn how to install, update, remove, find packages, manage packages and repositories on Linux systems using YUM (Yellowdog Updater Modified) tool developed by RedHat. The example commands shown in this article are practicall

Written in C, MySQL is an open-source, cross-platform, and one of the most widely used Relational Database Management Systems (RDMS). It’s an integral part of the LAMP stack and is a popular database management system in web hosting, data analytics,
