• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

xargs Command Examples in Linux

by admin

The xargs command reads from standard input and executes a command for each argument provided. Each argument must be separated by blanks. The pipe operator is used to make the output of the first command the input for the second command. The xargs command is commonly used with the find command to operate on each result that is found within the file or directory search.

Syntax

The general syntax of the xargs command is:

# command [options] [arguments] | xargs [options] {command}

Let’s say you want to delete all of the files in the /foo directory that have a .pdf extension. You can use xargs to automate the process:

# find /foo -type f -name "*.pdf" | xargs rm

The find command searches for all files in /foo that have a .pdf extension, then pipes the result to the xargs command. Because the results are delimited by a space, the xargs command will execute the rm command for each file in the results— removing all PDF files in the directory.

The xargs command has various options as shown below:

Option Description
-I {replacement string} Consider each line in the standard input as a single argument.
-L {number of lines} Read a specified number of lines from the standard input and concatenate them into one long string.
-p Prompt the user before each command.
-n {number of arguments} Read the maximum number of arguments from the standard input and insert them at the end of the command template.
-E {end of string} Represent the end of the standard input.
-t Write each command to the standard error output before executing the command.
-s {max size} Set the maximum allowable size of an argument list to a specified number of characters.

xargs Command Examples

1. Run a command using the input data as arguments:

# arguments_source | xargs command

2. Run multiple chained commands on the input data:

# arguments_source | xargs sh -c "command1 && command2 | command3"

3. Delete all files with a `.backup` extension (`-print0` uses a null character to split file names, and `-0` uses it as delimiter):

# find . -name '*.backup' -print0 | xargs -0 rm -v

4. Execute the command once for each input line, replacing any occurrences of the placeholder (here marked as `_`) with the input line:

# arguments_source | xargs -I _ command _ optional_extra_arguments

5. Parallel runs of up to `max-procs` processes at a time; the default is 1. If `max-procs` is 0, xargs will run as many processes as possible at a time:

# arguments_source | xargs -P max-procs command

Filed Under: Linux

Some more articles you might also be interested in …

  1. aurvote Command Examples
  2. UNIX / Linux : Examples of bash history command to repeat last commands
  3. a2query Command Examples in Linux
  4. How to allow ssh with empty passwords in Linux
  5. featureCounts: command not found
  6. Sample /etc/services file in Linux
  7. How to modify the iSCSI initiator ID in Linux
  8. mkfs: command not found
  9. Understanding SELinux Booleans
  10. “-bash: route: command not found” on CentOS/RHEL 8

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright