Linux xargs Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful xargs command in Linux. The xargs command allows you to execute commands with arguments passed from standard input, enabling you to create efficient and versatile workflows. You will start by understanding the basics of xargs, such as how to use it to print a list of names and create directories based on that list. Then, you will explore more advanced use cases, such as executing the curl command on a list of URLs and combining xargs with other Linux commands for even more powerful operations. This lab provides practical examples and step-by-step guidance to help you master the xargs command and enhance your Linux command-line skills.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/xargs("`Command Building`") linux/PackagesandSoftwaresGroup -.-> linux/curl("`URL Data Transferring`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") linux/BasicFileOperationsGroup -.-> linux/rm("`File Removing`") subgraph Lab Skills linux/cat -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} linux/echo -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} linux/xargs -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} linux/curl -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} linux/cp -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} linux/rm -.-> lab-423012{{"`Linux xargs Command with Practical Examples`"}} end

Understand the Basics of xargs Command

In this step, you will learn the basics of the xargs command in Linux. The xargs command is a powerful tool that allows you to execute commands with arguments passed from standard input.

First, let's create a text file with a list of names:

echo "Alice Bob Charlie David" > names.txt

Now, let's use xargs to print each name from the file:

cat names.txt | xargs echo

Example output:

Alice Bob Charlie David

The xargs command takes the input from the previous command (in this case, cat names.txt) and passes it as arguments to the echo command.

Next, let's create a directory for each name in the file:

cat names.txt | xargs mkdir

Now, let's list the contents of the current directory to verify the directories were created:

ls

Example output:

Alice  Bob  Charlie  David  names.txt

In this example, xargs takes the input from cat names.txt and passes each name as an argument to the mkdir command, creating a directory for each name.

The xargs command is highly versatile and can be combined with various other Linux commands to create powerful workflows. In the next steps, you will explore more advanced use cases of xargs.

Use xargs to Execute Commands with Arguments

In this step, you will learn how to use xargs to execute commands with arguments passed from standard input.

Let's start by creating a file with a list of URLs:

echo "https://www.example.com https://www.google.com https://www.github.com" > urls.txt

Now, we can use xargs to execute the curl command on each URL:

cat urls.txt | xargs curl -s -o /dev/null -w '%{url_effective} -> %{http_code}\n'

Example output:

https://www.example.com -> 200
https://www.google.com -> 200
https://www.github.com -> 200

In this example, xargs takes the input from cat urls.txt and passes each URL as an argument to the curl command. The -s option suppresses the output, -o /dev/null redirects the output to the null device, and -w '%{url_effective} -> %{http_code}\n' prints the effective URL and the HTTP status code.

Next, let's use xargs to delete the directories we created in the previous step:

ls | grep -E 'Alice|Bob|Charlie|David' | xargs rm -rf

Example output:

In this example, ls lists all the directories in the current directory, grep -E 'Alice|Bob|Charlie|David' filters the output to only include the directories we created, and xargs rm -rf deletes each directory.

The xargs command is a versatile tool that can be combined with various other Linux commands to create powerful workflows. In the next step, you will explore more advanced use cases of xargs.

Combine xargs with Other Linux Commands for Powerful Workflows

In this final step, you will learn how to combine the xargs command with other Linux commands to create powerful workflows.

Let's start by creating a list of files in the current directory:

ls > files.txt

Now, we can use xargs to calculate the MD5 checksum of each file:

cat files.txt | xargs md5sum

Example output:

d41d8cd98f00b204e9800998ecf8427e  files.txt
e10adc3949ba59abbe56e057f20f883e  names.txt
e10adc3949ba59abbe56e057f20f883e  urls.txt

In this example, xargs takes the input from cat files.txt and passes each file name as an argument to the md5sum command.

Next, let's use xargs to search for a specific string in the files:

cat files.txt | xargs grep -l "example"

Example output:

urls.txt

In this example, xargs takes the input from cat files.txt and passes each file name as an argument to the grep -l "example" command, which searches for the string "example" in each file and prints the file name if a match is found.

Finally, let's use xargs to create a backup of the files:

cat files.txt | xargs -I {} cp {} backups/{}

Example output:

In this example, xargs -I {} allows us to use a placeholder {} to represent the input from cat files.txt. The cp {} backups/{} command copies each file to the backups/ directory.

The xargs command is a powerful tool that can be combined with various other Linux commands to create efficient and flexible workflows. By mastering the use of xargs, you can streamline your daily tasks and increase your productivity as a Linux user.

Summary

In this lab, you learned the basics of the xargs command in Linux, which allows you to execute commands with arguments passed from standard input. You created a text file with a list of names, used xargs to print each name, and created directories for each name. You also learned how to use xargs to execute the curl command on a list of URLs, suppressing the output and displaying the effective URL and HTTP status code. These examples demonstrate the versatility of xargs and how it can be combined with various other Linux commands to create powerful workflows.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like