Running multiple commands with xargs
Last Updated :
08 Jan, 2024
Xargs is a command utility in Unix and Unix-like operating systems. It is used to build and execute commands from standard input which can be the output of the other command or data files. In this article, we will see how to run multiple commands with xargs.
Syntax of `xargs` multiple command in Linux
command1 | xargs [options] command2
Understanding of xargs
Xargs is the versatile command-line utility that reads data from the standard input (stdin) and executes a specified command with the input as arguments. It is very useful for scenarios where a list of items needs to be processed by another command.
There are many uses of "xargs" with multiple commands. Here, we are demonstrating some examples of xargs.
Creating Directories from a "echo" command
We can generate multiple folders or files using the echo command with xargs command. There are the following steps for making the folders using the xargs command.
- Print the name of the folder using the echo command and serve as input for the subsequent commands.
- Pipe the output of the echo command to the xargs command with mkdir command. For example
echo "one two three" | xargs mkdir
Output

Deleting files with xargs Command:
Xargs command also deletes the specific file extension files. We can find the specific file using the find command and pass the find command output to the xargs command using the pipes. See the following steps:
- Before deleting the files using the xargs. You must find the specific files using the find command with "-name" option.
- Find output will pass to the xargs command with rm command using the pipes. For example:
find . -name "*.txt" | xargs rm
In this example, first we will filter the fils using the find command then passed those file as arguments passed to the 'rm' command. The below image demonstrates how it works.
Output

Renaming Files with xargs
Suppose you wan to rename all files with a ".txt" extension to have a ".php" extension. You use find command with xargs and mv for this task. Follow the following steps.
find . -name "*.txt" | xargs -I {} sh -c 'mv {} $(basename {} .txt).php'
This command uses '-I {}' to specify a placeholder,and the 'sh -c' command allows the use of shell syntax for more complex operation. The 'mv' command renames each file by replacing the '.txt' extension with '.php'. The following image demonstrates.
Output

Xargs with three arguments
We can also used xargs with three arguments. In this example, we will see, how we can do this. See the following steps
- Use echo command with three arguments arg1 arg2 arg3 passed to the pipes.
- The echo output passed to the xargs command using pipes.
- In xargs command use '-n' Option with echo command
Command:
echo "arg1 arg2 arg3" | xargs -n 3 echo "Custom command:"
Output

Remove Delimeter
We can also remove delimiter using the xargs command. In this example, we are going to remove delimiter in the command using xargs command. Follow the following steps.
- Use echo command with this string text "arg1:arg2:arg3" for passing to the pipes.
- After pass to the xargs command use "-d" option for specify delimter with "-n" option.
Command:
echo "arg1:arg2:arg3" | xargs -d: -n 3 echo "Delimited command:"
Output

Conclusion
Xargs is very useful command line utility that can used day to day task. In this article , we have almost explained various multiple command runs with xargs. There are more example of this amazing tool. This is useful for processing multiple items in a list with a single command.
Similar Reads
Running multiple commands with xargs
Xargs is a command utility in Unix and Unix-like operating systems. It is used to build and execute commands from standard input which can be the output of the other command or data files. In this article, we will see how to run multiple commands with xargs. Table of Content Syntax of `xargs` multip
3 min read
xargs command in Linux with examples
xargs is a Unix command which can be used to build and execute commands from standard input. Importance: Some commands like grep can accept input as parameters, but some commands accept arguments, this is a place where xargs came into the picture. Syntax of `xargs` command in Linuxxargs [options] [c
5 min read
yes command in Linux with Examples
yes, the command in Linux is used to print a continuous output stream of a given STRING. If STRING is not mentioned then it prints 'y'; Syntax: of `yes` command in Linuxyes [STRING] Note: To stop printing please press Ctrl + C. Use of `yes` commandlet us say that we want to delete all the `.txt` fil
2 min read
xdg-open command in Linux with Examples
xdg-open command in the Linux system is used to open a file or URL in the userâs preferred application. The URL will be opened in the userâs preferred web browser if a URL is provided. The file will be opened in the preferred application for files of that type if a file is provided. xdg-open support
2 min read
for command in Linux with Examples
IntroductionThe for command in linux used in shell scripting to iterate over a set of values or perform set of tasks repeatedly. The for loop allows users to automate operations efficiently which is easier to maintain. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; doneHere NAME takes the value of
2 min read
expr command in Linux with examples
The expr command in Unix is a versatile tool used for evaluating expressions in the shell environment. It performs a wide range of functions, including arithmetic calculations, string operations, and comparisons based on regular expressions. What is the 'expr' Command?expr stands for "expression" an
3 min read
Copy Multiple Directories with One Command
System administration and development must have their files and directories well administered. One such way of doing this that is really effective is the task of copying multiple directories together, which actually saves one from consuming a huge amount of time and decreases the possibility of erro
7 min read
fg command in Linux with examples
The fg command in Linux is used to bring a background job into the foreground. It allows you to resume a suspended job or a background process directly in the terminal window, so you can interact with it. Syntaxfg [job_spec]The job_spec is a way to refer to the background jobs that are currently run
3 min read
bg command in Linux with Examples
In Linux, the bg command is a useful tool that allows you to manage and move processes between the foreground and background. It's especially helpful when you want to multitask in the terminal by placing a process in the background, enabling you to continue using the terminal for other commands whil
3 min read
ctags command in Linux with examples
The ctags command in Linux is commonly used with classic editors such as vi or vim to create an index or tags file for source code. This allows quick navigation across files, enabling users to jump to function definitions or references. ctags generates a cross-reference file that lists various sourc
4 min read