Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Pass the Output of a Command as an Argument for Another on Linux
When working with the Linux command line, we often need to use the output of one command as input or arguments for another command. This tutorial explores various methods to achieve this, including command substitution, process substitution, pipes with read, and the powerful xargs utility.
Preparing Sample Files
Let's create test directories and sample files to demonstrate each method:
mkdir dir_example target
truncate -s 10 dir_example/file{1..3}.this
truncate -s 4 dir_example/file{1..2}.not
This creates two directories and two sets of files: three files with 10 bytes (*.this) and two files with 4 bytes (*.not).
Using Pipes with the Read Command
The read command in shells like Bash and Zsh can process command output line by line. Here's how to copy files larger than 5 bytes:
find dir_example/ -type f -size +5c | while read file; do
cp "$file" target/
done
The find command locates files larger than 5 bytes (-size +5c), and the while read loop processes each filename, copying it to the target directory.
Command Substitution
Command substitution using $() or backticks allows command output to replace the command itself:
cp $(find dir_example/ -type f -size +5c) target/
This method executes the find command first, then substitutes its output as arguments to the cp command. All matching files are copied in a single operation.
Process Substitution
Process substitution creates temporary FIFO files that commands can read from. Here's an example using awk to count files in two categories:
awk '{hist[ARGIND]++}
END {
for (i in hist)
printf "%s records in process substitution %s<br>", hist[i], i
}' \
<(find dir_example/ -type f -size +5c) \
<(find dir_example/ -type f -size -5c)
The <() syntax creates two separate input streams: one for files larger than 5 bytes and another for files smaller than 5 bytes.
The xargs Command
xargs builds and executes command lines from standard input. Let's create a test file first:
cat - << __EOF > dir_example/file4.this This file example will be checked by the following line: "choose me" __EOF
Now use xargs to find and copy files containing specific text:
find dir_example/ -type f | xargs grep -l "choose" | xargs -I {} cp {} target/
This command chain: finds all files, searches for "choose" in their contents using grep -l (list matching filenames only), then copies matching files using the -I {} placeholder.
Comparison of Methods
| Method | Best Use Case | Handles Spaces | Memory Usage |
|---|---|---|---|
Pipe with read
|
Large file lists, complex processing | Yes (with quotes) | Low |
| Command substitution | Small to medium output | Problematic | High |
| Process substitution | Multiple input streams | Yes | Low |
xargs |
Batch operations, argument limits | Yes (with -0 option) | Medium |
Conclusion
These methods provide flexible ways to chain commands in Linux. Use xargs for robust batch operations, command substitution for simple cases, and pipes with read for complex processing. Understanding these techniques enables efficient command-line workflows and automation.
