---
title: "Learning the Bash Xargs Command Through Examples"
description: "Bash Xargs is a complicated and useful command. In this ATA Learning tutorial learn to take advantage of its many uses!"
canonical: "https://adamtheautomator.com/bash-xargs/"
---

# Learning the Bash Xargs Command Through Examples

> Bash Xargs is a complicated and useful command. In this ATA Learning tutorial learn to take advantage of its many uses!

Source: https://adamtheautomator.com/bash-xargs/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Learning the Bash Xargs Command Through Examples](https://adamtheautomator.com/wp-content/uploads/2022/11/bash-xargs.jpg)

# Learning the Bash Xargs Command Through Examples

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)7 December 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Bash](/tag/bash/)[Linux](/tag/linux/)

Table of Contents

*   [Prerequisite](#prerequisite)
*   [Finding and Removing Files with the Bash xargs Command](#finding-and-removing-files-with-the-bash-xargs-command)
*   [Finding and Archiving Files](#finding-and-archiving-files)
*   [Running Multiple Commands with Bash xargs](#running-multiple-commands-with-bash-xargs)
*   [Enabling Prompting Before Execution](#enabling-prompting-before-execution)
*   [Reading Files Using the -a Option](#reading-files-using-the--a-option)
*   [Filtering Search Result with xargs and grep](#filtering-search-result-with-xargs-and-grep)
*   [Conclusion](#conclusion)

Streaming data is a common data processing problem. But luckily, the Bash `xargs` command, a versatile tool, can help process and manipulate streaming data more efficiently.

In this tutorial, you will learn how to use the Bash `xargs` command through practical examples.

Stay tuned and take control of command behaviors with the Bash `xargs` command!

## Prerequisite

This tutorial houses hands-on demonstrations, and you will need a Linux system with the Bash shell installed to follow along. This tutorial uses Ubuntu 20.04, but any recent Linux distribution should work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Finding and Removing Files with the Bash `xargs` Command

The Bash `xargs` command is a Linux utility used to build and execute commands from standard input. This command reads input from stdin, parses into arguments, and executes the specified command one or more times with the parsed arguments.

The general syntax for the `xargs` command is as below:

```bash
xargs <options> <command>
```

The most common use case for the `xargs` command is to find and remove files based on specific criteria. For example, you can use the `xargs` command to find all files older than a specified number of days and delete them.

Related:[Learn the Many Ways In Ubuntu to Delete Files](https://adamtheautomator.com/ubuntu-to-delete-file/)

Run the below command to `find` all files older than 30 days (`-mtime +30`) and pipe the output to the Bash `xargs` command. The `xargs` command then executes the `rm` command to forcefully (`-f`) delete each file without prompting for confirmation.

Related:[How to Find Files with Dozens of Criteria with the Bash Find Command](https://adamtheautomator.com/bash-find/)

The `-print0` option below prints the filenames to stdout, separated by a null character, since the `xargs` command expects its input to be null-delimited rather than newline-delimited.

```bash
find -mtime +30 -print0 | xargs -0 rm -f
```

This command does not provide output, but you will verify the deletion is successful in the following step.

> 💡 _The Bash `xargs` command is often used with pipes to process data that would otherwise be too long for a single command._

Now, run the command below to list (`ls`) all contents of the working directory, sorted by time (`t`) in reverse order (`r`).

```bash
ls -ltr
```

Related:[Using Bash Sort to Sort Files Like a Boss](https://adamtheautomator.com/bash-sort/)

All the files older than 30 days have been deleted, as shown below. Only new files remain in the current directory.

![Verifying that the old files have been deleted](https://adamtheautomator.com/wp-content/uploads/2022/11/image-629.png)

Verifying that the old files have been deleted

Related:[Linux Directory Commands : A Complete Guide](https://adamtheautomator.com/linux-directory-commands/#Listing_Files_and_Folders_With_the_ls_Command)

## Finding and Archiving Files

Another everyday use case for the `xargs` command is to find all files based on a particular pattern and execute a command on each. Suppose you wish to find all text files and compress them to an archive (ZIP file), which lets you save some disk space. If so, the Bash `xargs` command is up to the task.

Related:[Learn How to Zip and Unzip Files in Linux and be a Zip Master](https://adamtheautomator.com/unzip-files-in-linux/)

Run the below command to `find` all text files (`*.txt`) and pipe the output to the `xargs` command, which executes the tar command to archive the files to `*archive-txt.tar.gz*`.

The `-0` option below tells `xargs` to treat its input as null-delimited instead of the default newline-delimited, so filenames with spaces in them are handled correctly.

```bash
find -name "*.txt" -print0 | xargs -0 tar -czvf archive-txt.tar.gz
```

![Finding and archiving files](https://adamtheautomator.com/wp-content/uploads/2022/11/image-630.png)

Finding and archiving files

Now, run the following command to list all files containing the name `archive-txt.tar.gz`.

```bash
ls -la archive-txt.tar.gz
```

Below, you can verify the archive (`archive-txt.tar.gz`) exists and that the compression is a success.

![Verifying that the compression was successful](https://adamtheautomator.com/wp-content/uploads/2022/11/image-631.png)

Verifying that the compression was successful

## Running Multiple Commands with Bash `xargs`

Perhaps a file needs to be processed, and you wish to parallelize the processing by running multiple commands simultaneously.

For example, instead of running one `echo` command for each filename to insert to the text file, as shown below, let the `xargs` do the trick. The `xargs` command supports running multiple commands on its input.

![Demonstrating running echo commands, one for each file, to process](https://adamtheautomator.com/wp-content/uploads/2022/11/image-632.png)

Demonstrating running `echo` commands, one for each file, to process

Suppose you have to create directories for multiple files. If so, you just need a list of filenames and run an `xargs` command.

1\. Run the below command to create a text file named `bash-xargs-demo.txt` containing a list of filenames, one per line.

```bash
# Creates a bash-xargs-demo.txt containing a list of filenames, one per line.
cat > bash-xargs-demo.txt << EOF
One
Two
Three
EOF

# Viewing the bash-xargs-demo.txt content
cat bash-xargs-demo.txt
```

![Creating a new file containing file names](https://adamtheautomator.com/wp-content/uploads/2022/11/image-633.png)

Creating a new file containing file names

2\. Next, run the command below to read the filenames from the text file and create directories for each (`sh -c 'echo %; mkdir %'`).

The `-I %` option tells `xargs` to replace `%` with the input it reads from stdin, which is `*bash-xargs-demo.txt*`.

```bash
cat bash-xargs-demo.txt | xargs -I % sh -c 'echo %; mkdir %'
```

![Creating directories from filenames read in a text file ](https://adamtheautomator.com/wp-content/uploads/2022/11/image-634.png)

Creating directories from filenames read in a text file

> 💡 _Perhaps you want to see what happens in the background as you run the command. If so, append the `-t` option to the `xargs` command to get a verbose output, as shown below. `cat bash-xargs-demo.txt | xargs -t -I % sh -c 'echo %; mkdir %'`_

![Viewing verbose output of the xargs command](https://adamtheautomator.com/wp-content/uploads/2022/11/image-635.png)

Viewing verbose output of the `xargs` command

3\. Lastly, run the command below to list (`ls`) all contents of the working directory in a long list (`-la`).

```bash
ls -la
```

You can verify that the directories were created successfully by running the `ls` command below.

In the output below, you can see three new directories (**One**, **Two**, and **Three**), one for each input line in the text file.

![Verifying that the directories were created successfully](https://adamtheautomator.com/wp-content/uploads/2022/11/image-636.png)

Verifying that the directories were created successfully

## Enabling Prompting Before Execution

By default, the Bash `xargs` command executes specified commands for all input without asking for confirmation. This behavior can be dangerous if you are not careful, such as when deleting files with the `rm` command.

For example, the `rm` command below deletes all files in the current directory recursively without a confirmation prompt. You could delete many important files if you accidentally ran this command in the wrong directory.

```bash
rm -rf *
```

> 💡 _Perhaps you have mistakenly deleted important files. Luckily, Linux lets you [recover deleted files](https://adamtheautomator.com/linux-to-recover-deleted-files/)._

To avoid untoward actions, append the `-p` option to enable prompting before execution.

Run the below command to `find` all files (`*`) in the working directory and delete (`rm`) them recursively (`-rf`) with a confirmation prompt (`-p`).

```bash
find * | xargs -p rm -rf
```

Notice below that even though you used the force (`-f`) option in the `rm` command, you will still get a prompt since the `-p` option is appended to the `xargs` command. The prompt below lets you verify that the command will do what you expect before confirming.

Type Y and press Enter to confirm the action. Or type N and press Enter if you made a mistake to cancel the command, and no harm will be done.

![Delete with Prompt](https://adamtheautomator.com/wp-content/uploads/2022/11/image-637.png)

Delete with Prompt

## Reading Files Using the `-a` Option

So far, you have been reading input from stdin. But did you know that `xargs` can also read input from files? Yes! This feature can be useful if you have a large amount of data that needs to be processed and you do not want to keep the data all in your memory.

Run the below command to read and print input (`-a`) from the `foo.txt` file to stdout.

> 💡 _Note that for `xargs` to read the input from files, you must append the `-a` option before any other options._

```bash
xargs -a foo.txt
```

![Reading a file](https://adamtheautomator.com/wp-content/uploads/2022/11/image-638.png)

Reading a file

## Filtering Search Result with `xargs` and `grep`

The `grep` command is handy when searching texts in files. But one drawback is that the process may take a long time if you search through many files. The good news is that `xargs` can parallelize the grep command, speeding up the search significantly.

Run the following command to `find` all text files (`*`) in the working directory, filter, and print (`grep`) any files that contain the `Examples` string.

```bash
find . -name "*.txt" | xargs grep 'Examples'
```

As you can see below, two files contain the **Examples** string.

![Searching files containing a specific text](https://adamtheautomator.com/wp-content/uploads/2022/11/image-639.png)

Searching files containing a specific text

## Conclusion

Streaming input from stdin is a powerful feature of the Bash `xargs` command, but that is not all. In this tutorial, you have learned how to read input from files using the `-a` option. In addition, you touched on using `xargs` with other commands, such as `grep`, to speed up your workflows.

At this point, you should be comfortable with using the Bash `xargs` command. Experiment with different ways of using `xargs` to find the method that works best for you!

Now, why not try [running programs in parallel using xargs](https://stackoverflow.com/questions/28357997/running-programs-in-parallel-using-xargs)?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fbash-xargs%2F&text=Learning%20the%20Bash%20Xargs%20Command%20Through%20Examples)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fbash-xargs%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fbash-xargs%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/06/Understanding-Bash-If-Else-and-other-Conditional-Statements.jpg)

### [Understanding Bash If Else and other Conditional Statements](/bash-if-else/)

Discover the ins-and-outs of Bash If Else and Bash Case scripting in this example-driven tutorial by ATA Learning!

![](https://adamtheautomator.com/wp-content/uploads/2022/11/Echo-Command-in-Bash-Shell-Discover-the-Many-Uses.jpg)

### [Echo Command in Bash Shell : Discover the Many Uses](/echo-command-in-bash/)

Learn about all the ways to utilize the echo command in Bash to create better scripts and master the Bash shell!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/How-to-Update-Ubuntu-IP-and-Hostname-via-Bash.jpg)

### [How to Update Ubuntu IP and Hostname via Bash](/change-linux-ip-hostname-bash-script/)

Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
