xargs

shell utilitiesLinux/Unix
The xargs command is one of the most frequently used commands in Linux/Unix-like operating systems. xargs Build and execute command lines from standard input

Quick Reference

Command Name:

xargs

Category:

shell utilities

Platform:

Linux/Unix

Basic Usage:

xargs [options] [arguments]

Common Use Cases

    Syntax

    xargs [options] [command [initial-arguments]]

    Options

    Option Description
    -0, --null Input items are terminated by a null character instead of whitespace
    -a file, --arg-file=file Read items from file instead of standard input
    -d delim, --delimiter=delim Input items are terminated by the specified delimiter instead of whitespace
    -E eof-str, --eof=eof-str Set the end of file string to eof-str
    -I replace-str, --replace[=replace-str] Replace occurrences of replace-str in the initial arguments with names read from standard input
    -L max-lines, --max-lines=max-lines Use at most max-lines non-blank input lines per command line
    -n max-args, --max-args=max-args Use at most max-args arguments per command line
    -P max-procs, --max-procs=max-procs Run at most max-procs processes at a time
    -p, --interactive Prompt the user before executing each command line
    -r, --no-run-if-empty Do not run command if standard input is empty
    -s max-chars, --max-chars=max-chars Limit commands to max-chars characters
    -t, --verbose Print the command line on standard error before executing it
    -x, --exit Exit if the size (see -s) is exceeded
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the xargs command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Remove all .txt files listed by find find . -name "*.txt" | xargs rm -f
    # Create a file with a list of filenames find . -name "*.jpg" | xargs ls -l
    # Count lines in all text files find . -name "*.txt" | xargs wc -l
    # Advanced Examples Advanced
    # Handle filenames with spaces or special characters find . -name "*.txt" -print0 | xargs -0 rm -f
    # Run command with one file per command line find . -name "*.log" | xargs -n 1 gzip # Prompt before execution of each command find . -name "*.bak" | xargs -p rm # Use placeholder to specify where arguments should be substituted find . -name "*.txt" | xargs -I {} cp {} ./backup/ # Run multiple processes in parallel find . -name "*.jpg" | xargs -P 4 -n 1 convert -resize 50% # Replace strings in multiple files find . -name "*.html" | xargs sed -i 's/oldtext/newtext/g' # Move files to directories based on their extensions find . -type f | xargs -n 1 bash -c 'mkdir -p ./sorted/${1##*.} && mv "$1" ./sorted/${1##*.}/' --

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `xargs` command is a powerful utility in Unix-like operating systems that builds and executes commands from standard input. It bridges the gap between commands that output lists and commands that can't accept standard input directly, making it an essential tool for complex command line operations and batch processing. **Core Functionality:** 1. **Command Construction**: xargs reads items (typically lines or filenames) from standard input and uses them to construct and execute commands. 2. **Batch Processing**: It can efficiently handle large numbers of input items by bundling them into fewer command executions, optimizing performance. 3. **Input Transformation**: xargs can transform input in various ways, including handling special characters, delimiting by custom characters, and replacing tokens in command templates. 4. **Parallel Execution**: With the -P option, xargs can run multiple command instances simultaneously, taking advantage of multi-core systems for faster processing. **Common Use Cases:** 1. **Mass File Operations**: xargs excels at performing operations on large sets of files: ```bash find /path -type f -name "*.log" | xargs grep "error" ``` 2. **Resource Management**: When dealing with commands that might cause resource issues if given too many arguments at once: ```bash find /large_directory -type f | xargs -n 100 rm ``` 3. **Parallel Processing**: Speed up operations by running multiple processes simultaneously: ```bash find . -name "*.jpg" | xargs -P 4 -n 1 convert -resize 800x600 ``` 4. **Custom Command Construction**: Using placeholders to create complex commands: ```bash find . -name "*.txt" | xargs -I {} sh -c 'echo {}; wc -l {}' ``` 5. **System Administration**: Performing actions on specific processes or users: ```bash ps aux | grep defunct | awk '{print $2}' | xargs kill ``` **Technical Details:** 1. **Argument Handling**: By default, xargs breaks input by whitespace and quotes, but it can be configured to use null characters (-0) or custom delimiters (-d). 2. **Command Size Limits**: xargs automatically limits command line length to avoid exceeding system limits, but this can be customized with the -s option. 3. **Exit Behavior**: When using -P for parallel execution, xargs waits for all commands to complete before exiting, and returns the maximum of all exit codes unless interrupted. 4. **Placeholder Replacement**: The -I option allows specifying a placeholder string that will be replaced by each input item, enabling more complex command structures. 5. **Empty Input Handling**: By default, xargs runs the command once with no arguments if no input is provided. The -r option changes this behavior to not run the command at all with empty input. **Performance Considerations:** 1. **Reducing Process Creation**: xargs improves performance by reducing the number of processes created compared to using a shell loop: ```bash # Less efficient (creates one process per file): for file in $(find . -name "*.txt"); do grep "pattern" "$file"; done # More efficient (creates fewer processes): find . -name "*.txt" | xargs grep "pattern" ``` 2. **Parallel Execution**: The -P option can significantly speed up operations on multi-core systems, especially for CPU-intensive tasks. 3. **Command Batching**: xargs automatically optimizes command execution by batching arguments, but this can be fine-tuned with the -n and -L options. **Security Considerations:** 1. **Handling Special Characters**: Without proper options, filenames with spaces or special characters can cause issues. Always use -0 with find -print0 for safe filename handling: ```bash find . -name "*.txt" -print0 | xargs -0 rm ``` 2. **Command Injection**: Be cautious when using user-provided input with xargs, as it could lead to command injection vulnerabilities. 3. **Interactive Confirmation**: For destructive operations, consider using the -p option to prompt before execution: ```bash find . -name "*.bak" | xargs -p rm ``` **Compatibility Notes:** 1. **GNU vs. BSD Differences**: GNU xargs (common on Linux) and BSD xargs (on macOS) have some option differences. For cross-platform scripts, stick to common options or explicitly check the environment. 2. **Shell Integration**: When complex shell operations are needed with each argument, use `sh -c` with careful quoting: ```bash find . -name "*.log" | xargs -I {} sh -c 'echo "Processing {}"; gzip {}' ``` **Best Practices:** 1. **Safer File Handling**: Always use -0 with find -print0 when dealing with filenames that might contain spaces or special characters. 2. **Preview Commands**: Use -t to see commands before they execute, helpful for debugging complex xargs pipelines. 3. **Empty Input Awareness**: Include -r when you don't want commands to run if there's no input. 4. **Parallel Execution Caution**: When using -P for parallel processing, be aware of resource constraints and potential race conditions. 5. **Testing Complex Commands**: For complex operations, test with a small subset of data and use -p (interactive mode) before running on the full dataset. xargs remains one of the most versatile and powerful command-line tools in the Unix/Linux toolkit, enabling complex operations that would be difficult or inefficient to achieve with other utilities.

    Related Commands

    These commands are frequently used alongside xargs or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the xargs command works in different scenarios.

    $ xargs
    View All Commands