The “xargs” Command in Linux [5 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The xargs command in Linux is a helpful tool for processing large lists of inputs and executing commands for each item in the list. It allows you to efficiently process inputs from other commands and automate repetitive tasks. In this article, I will explore the basic usage of the xargs command, as well as some of its options and features, with some practical examples.

A. Description

The xargs command in Linux is a powerful utility for processing and executing inputs from standard input or a file. It is used to construct and run commands based on inputs, which can make repetitive and complex tasks much simpler. Moreover, the xargs command takes items from standard input, separated by newlines, and runs the specified commands with each item as a separate argument.

B. Syntax

The syntax of the xargs command in Linux is simple, as shown below.

xargs [OPTION]... [COMMAND [INITIAL-ARGUMENTS]]

Note: In the syntax above, the contents enclosed in square brackets mean they are not mandatory and three dots after the square brackets mean multiple OPTIONS can be used simultaneously after the xargs command.

C. Options

The xargs command has multiple options available. Here, I have listed a few of them. However, you can learn more about the xargs command, its options and their uses by checking the man page.

man xargs

Useful Options

  • -a, –arg-file=FILE: Reads input from a file instead of standard input.
  • -I, –replace[=STRING]: Replaces occurrences of a string with the arguments passed to xargs.
  • -L, –max-lines=NUM: Passes no more than NUM arguments to each invocation of the command.
  • -n, –max-args=NUM: Passes no more than NUM arguments to each invocation of the command.
  • -p, –interactive: Prompts the user for confirmation before executing each command.
  • -r, –no-run-if-empty: Does not run the command if the input is empty.
  • -s, –max-chars=NUM: Passes no more than NUM characters to each invocation of the command.
  • -t, –verbose: Prints the command line before executing it.
  • -h, –help: Displays the help page.
  • -V, –version: Displays version information.

Note: Commands and their options are case-sensitive in Linux. So, be careful while using them.

Practical Examples of the “xargs” Command in Linux

In Linux, the xargs command is a helpful tool to automate and simplify tasks that involve processing inputs from standard input or a file. In the section below, I will show you some of the most useful applications for the xargs command in Linux.

Example 1: Read the Contents of a File Using “xargs” Command in Linux

You can read the contents of any file using the xargs command with option -a followed by the filename, that you want to read. Here, I will show you the contents of a file named OSInfo.txt using the xargs command. You can read any file using this command by following the steps given below.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Then, execute the following command.

xargs -a OSInfo.txt

➌ Finally, press the ENTER key.

Output >

In the following image, you can see the contents of the OSInfo.txt file.Showing the contents of the OSInfo.txt file using the xargs command in linux.


Similar Readings


Example 2: Print Commands that are Executed by the “xargs” Command

The “xargs command in Linux with the -t option is used to print the command line that is executed before running it. This can be useful for debugging or for getting a better understanding of how xargs is processing the input and constructing the command line. There is a file in my machine named list.txt that contains two file names, i.e., OSInfo.txt and time.txt. Now, I will execute the ls -l command with the contents of the list.txt file using the xargs command, and at the same time, I want to display the command that is executed through the xargs command. You can do the same by following the steps given below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Then, execute the following command to see the contents of the list.txt file.

cat list.txt

➌ After that, press the ENTER key.

➍ Now, execute the command below

cat list.txt | xargs -t ls -l

➎ Press the ENTER button again.

Output >

In the following image, you can see that I have displayed the command that is executed due to running the xargs command.Showing the command that is executed due to running the xargs command.

Example 3: Find & Remove a File Using the “xargs” Command in Linux

To remove any file after finding it, you can use the xargs command. Here, in this example, I will find the file named time.txt using the find command and remove that file from my system using the xargs command by following the below syntax.

find -name "filename" | xargs rm -f

Note: You can add option -t after the xargs command to see the command that will execute due to running the line above.

You can do the same by following the steps mentioned below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Then, execute the following command.

find -name "time.txt" | xargs -t rm -f

➌ Finally, press the ENTER key.

Output >

In the following image, you can see that I have removed the file named time.txt after finding it.The file named time.txt has been removed after finding it using the xargs command in linux.

Example 4: Find and Move Files Using the “xargs” Command in Linux

To move any file after finding it, you can use the xargs command. In this example, I will find the file named list.txt and move that file to the directory named “MF” using the following syntax

find -name "filename" | xargs -I{} mv {} destination_folder

Note: The {}, placeholder, is used to represent the standard output passed from the previous command.

You can do the same by following the steps below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

 Then, execute the following command.

find -name "list.txt" | xargs -tI {} mv {} MF

➌ After that, press the ENTER key.

➍ Now, check the contents of the MF directory by copying the command given below.

tree MF

➎ Press the ENTER button.

Output >

In the following image, you can see that I have moved the file named tlist.txt to the directory MF after finding it.Showing that I have moved the file named tlist.txt to the directory MF after finding it.

Example 5: Find a File & Copy it to Multiple Directories Using “xargs” Command in Linux

You can copy a file to multiple directories after finding it with the help of the xargs command. To do this you have to follow the beow syntax.

find -name "filename" | xargs -I {} sh -c 'cp {} destination_folder1/; cp {} destination_folder2/; cp {} destination_folder3/ …'

In this example, I will find the file OSInfo.txt and then copy it to the directories named dir1, dir2 and dir3. You can do the same by following the below steps.

Steps to Follow >

➊ First, open the Ubuntu Terminal.

 Next, run the command to see the amount of time the system has been running.

tree dir{1..3}

➌ Now, tap the ENTER button.

➍Then, execute the command below.

find -name "time.txt" | xargs -I {} sh -c 'cp {} dir1/; cp {} dir2/; cp {} dir3/'

➎ Press the ENTER button.

➏ Execute the command to see the current status of dir1, dir2 and dir3.

tree dir{1..3}

➐ Lastly, hit the ENTER key.

Output >

As you can see in the image below, I have copied the file OSInfo.txt to multiple directories.The file OSInfo.txt have been copied to multiple directories.

Conclusion

The xargs command in Linux is a simple and powerful tool for executing commands based on input from standard input or a file. By utilizing its various options, you can automate complex and repetitive tasks and achieve improved efficiency and productivity in the Linux environment. In simple words, I have designed this article aiming to provide a comprehensive guide to the xargs command, with practical examples to help you get started and become an advanced user.


Similar Readings

Rate this post
S. M. Amdadul Islam

Hello everyone. I am S. M. Amdadul Islam, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Naval Architecture and Marine Engineering (NAME) graduate from the Bangladesh University of Engineering and Technology. In addition to my regular work, I enjoy watching Youtube, discovering new things, gossiping with friends, visiting new places, and taking photos of landscapes. Read Full Bio

Leave a Comment