Pass the Output of a Command as an Argument for Another on Linux


Introduction

When working with the Linux command line, we often use multiple commands that take important data as arguments. In this tutorial, we'll explore various scenarios on how to use the output of one command as an argument to another. We will create an environment to test our strategies and use examples to illustrate each scenario.

Preparing Sample Files

To get started, we need to create a directory called "dir_example" and another called "target". These directories will be used to store the files that will be used as examples in this tutorial.

$ mkdir dir_example target

Next, we will create two sets of files, one with a size of 10 bytes and one with a size of 4 bytes.

$ truncate -s 10 dir_example/file{1..3}.this
$ truncate -s 4 dir_example/file{1..2}.not

We used the truncate command to create these files. The files were named “file1.this”, “file2.this”, and so on, with the suffix “.this”, to differentiate them from the other set of files. These files will be used as examples in this tutorial.

Using the built-in Read Command

Some shells like Bash and Zsh have a built-in command called read, which reads the contents of the input and stores it in a variable. In the following example, we will copy the files whose size is greater than five bytes from the “Dir_Example” folder to the destination folder −

$ find dir_example/ -type f -size +5c | while read file; do cp "$file" target/; done

In this example, we have used the search command with the “-type f” argument to search for regular files. The argument “-size +5c” is used to find only files with sizes greater than five bytes. Next, we are using the output of the search command to feed the while loop. The built-in read then reads from the input and assigns the first word to the file variable. Finally, using the file variable as a parameter to the cp command, we can copy the file with the same name to the destination folder.

Command Replacement

Several shells have a mechanism called command substitution that allows the output of a command to replace the name of the command. In the following example, we will use this feature to copy files larger than five bytes to the destination folder −

$ cp $(find dir_example/ -type f -size +5c) target/

In this scenario, we are using command substitution to feed the source file list as parameters to the cp command. Then, we copy them to the destination folder given in the last argument.

Process Replacement

Similar to the previous scenario, several shells have another mechanism called process substitution, which binds the input or output of a list to a FIFO. The commands then use the name of this FIFO. In the following example, we'll use this functionality within an awk script to count the size of our two sets of files −

awk '{hist[ARGIND]++}
END {
   for (i in hist)
      printf "%s records in process substitution %s
", hist[i] , i }' \ <(find dir_example/ -type f -size +5c) \ <(find dir_example/ -type f -size -5c)

In this example, we list the largest files separately and less than five bytes in two FIFOs. Then, inside the awk script, we format the output to show the file count.

The xargs Command

xargs is a powerful tool that creates and executes command lines using standard input. In the next example, we'll create another sample file and see what we can do with the xargs command −

$ cat - << __EOF > dir_example/file4.this.xargs
This file example
will be check by
the following line:
"choose me"
__EOF

In this example, we've created a file called file4.this. Next, we will use the xargs command to copy the files with the word "choose" in their contents to the target folder −

$ find dir_example/ -type f | xargs grep -l "choose" | xargs -I {} cp {} target/

In this example, we are using the find command to list all the files in the dir_example folder. We are then using the xargs command to feed the grep command, which searches for the word "choose" in the files. The topic -l is used to show only the names of the files that contain the word. So, we are using xargs again to power the cp command to copy the files that contain the word "choose" in the target folder.

Conclusion

In this tutorial, we've explored several ways to pass the output of one command as an argument to another in Linux. We discussed the use of the pipe operator, the redirect operator, backticks, the read command, command substitution, process substitution, and the xargs command. By understanding these different methods, you can chain commands together and perform complex operations with just a few simple commands. This can greatly improve your workflow and automate tasks, making you more efficient when working with the Linux command line.

Updated on: 20-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements