Cheatsheet & Examples: xargs

HongHong
4 min read

xargs is a command-line utility that builds and executes commands from standard input. It reads items from stdin and executes a command with those items as arguments.

Run a command on each input item

Example Usage:
xargs -I {} rm {}
What it does:
Executes the rm command for each input item, replacing {} with each item individually.
Command-line Arguments Explained:

  • -I {}: Specifies a placeholder ({}) to be replaced by each input item.
  • rm: The command to execute with the input item as its argument.
  • {}: The placeholder that represents each input item.

Limit number of arguments per command

Example Usage:
xargs -n 5 ls
What it does:
Runs the ls command in batches of 5 input arguments at a time.
Command-line Arguments Explained:

  • -n 5: Specifies the maximum number of arguments to pass to ls per invocation.
  • ls: The command to execute with the input arguments as filenames.

Handle filenames with spaces or special characters

Example Usage:
find . -name "*.txt" -print0 | xargs -0 grep "pattern"
What it does:
Finds .txt files and runs grep "pattern" on them, safely handling filenames with spaces or newlines using null-terminated strings.
Command-line Arguments Explained:

  • find . -name "*.txt" -print0: Outputs filenames separated by null bytes to avoid issues with spaces.
  • -0: Tells xargs to expect null bytes as input delimiters.
  • grep "pattern": The command to execute on each found file.

Echo the command before execution

Example Usage:
xargs -t ls
What it does:
Prints the ls command line with arguments to the terminal before running it, useful for debugging.
Command-line Arguments Explained:

  • -t: Displays the command and arguments to stdout before execution.
  • ls: The command to execute with input items as arguments.

Prompt for confirmation before execution

Example Usage:
xargs -p rm
What it does:
Prompts the user for confirmation before executing each command line built from input.
Command-line Arguments Explained:

  • -p: Prompts the user to confirm each command before execution.
  • rm: The command to execute on input items (e.g., filenames).

Execute commands in parallel

Example Usage:
xargs -P 4 -I {} cp {} /backup
What it does:
Copies each input item to /backup concurrently using up to 4 processes.
Command-line Arguments Explained:

  • -P 4: Runs up to 4 processes in parallel.
  • -I {}: Defines a placeholder for each input item.
  • cp: The command to execute (copies files).
  • {}: Placeholder for the input item.
  • /backup: Destination directory for cp.

Process input line by line

Example Usage:
xargs -L 1 echo
What it does:
Executes the echo command once per input line, regardless of word count.
Command-line Arguments Explained:

  • -L 1: Processes input line by line, passing one line to the command per invocation.
  • echo: The command to execute on each line.

Replace standard input with a command's argument list

Example Usage:
xargs -I {} sh -c "echo Processing: {}"
What it does:
Runs a shell command (sh -c) for each input item, using {} as the argument.
Command-line Arguments Explained:

  • -I {}: Replaces {} with each input item.
  • sh -c "echo Processing: {}": Executes a shell command with the input item as an argument.

Avoid executing command with empty input

Example Usage:
xargs -r rm
What it does:
Prevents xargs from invoking rm if there is no input (avoids "rm: missing operand" errors).
Command-line Arguments Explained:

  • -r: Skips execution if no input is provided.
  • rm: The command to execute on input items.

Pass arguments to a command that accepts multiple inputs

Example Usage:
xargs grep "error"
What it does:
Runs grep "error" on all input items as arguments (e.g., files or text).
Command-line Arguments Explained:

  • grep "error": The command to execute with all input items as arguments.

Pass arguments to a command that modifies each item

Example Usage:
xargs -I {} sed -i 's/foo/bar/' {}
What it does:
Replaces all occurrences of foo with bar in each input file.
Command-line Arguments Explained:

  • -I {}: Replaces {} with each input file.
  • sed -i 's/foo/bar/': Edits files in-place, substituting foo with bar.
  • {}: Placeholder for the input file.

Combine with stdin for multi-step processing

Example Usage:
cat files.txt | xargs -I {} cat {}
What it does:
Reads filenames from files.txt and outputs their contents using cat.
Command-line Arguments Explained:

  • cat files.txt: Provides a list of filenames to xargs.
  • -I {}: Replaces {} with each filename.
  • cat {}: Displays the content of each file.
0
Subscribe to my newsletter

Read articles from Hong directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hong
Hong

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)