Skip to main content

xargs Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for building commands from standard input with xargs in Linux

The `xargs` command builds and runs commands from standard input. This cheatsheet covers common `xargs` patterns for batching arguments, handling spaces safely, previewing commands, and combining `xargs` with tools like `find`.

Basic Syntax

Core xargs command forms.

CommandDescription
xargs commandRead stdin and pass items to a command
printf '%s\n' a b c | xargs commandPass newline-separated items to a command
cat list.txt | xargs commandRead arguments from a file through stdin
xargsUse /bin/echo as the default command
xargs --helpShow available options

Limit Arguments

Control how many items xargs passes at a time.

CommandDescription
printf '%s\n' a b c | xargs -n 1 echoPass one argument per command run
printf '%s\n' a b c d | xargs -n 2 echoPass two arguments per command run
printf '%s\n' a b c | xargs -L 1 echoRead one input line per command run
printf '%s\n' a b c | xargs -P 4 echoRun up to four commands in parallel
printf '%s\n' a b c | xargs -n 100 rmBatch large argument lists

Replace Input

Use placeholders when each input item must appear in a specific position.

CommandDescription
printf '%s\n' file1 file2 | xargs -I {} touch {}Replace {} with each input item
printf '%s\n' file1 file2 | xargs -I % sh -c 'echo %; ls -l %'Run multiple commands per item
printf '%s\n' img1 img2 | xargs -I {} mv {} {}.bakReuse the same item twice
printf '%s\n' user1 user2 | xargs -I {} id {}Insert input into a fixed command pattern
printf '%s\n' src1 src2 | xargs -I {} cp {} /backup/Copy each input item to a directory

Safe File Handling

Use null-delimited input when paths may contain spaces or special characters.

CommandDescription
find . -type f -print0 | xargs -0 rm -fRemove found files safely
find . -name '*.log' -print0 | xargs -0 ls -lhList matching files safely
find . -type f -print0 | xargs -0 -n 1 basenameProcess one safe path at a time
printf '%s\0' 'file one' 'file two' | xargs -0 -n 1 echoFeed null-delimited names directly
find /var/www -type f -print0 | xargs -0 chmod 644Apply permissions to many files safely

Preview and Confirm

Check generated commands before running them.

CommandDescription
printf '%s\n' a b c | xargs -t touchPrint each command before execution
printf '%s\n' a b c | xargs -p rmPrompt before running the command
find . -type f -print0 | xargs -0 -t rm -fPreview destructive file removals
find . -type f -print0 | xargs -0 echo rm -fDry run by replacing rm with echo
printf '%s\n' a b c | xargs -r echoDo nothing if stdin is empty

Read from Files

Load items from a file instead of a pipeline.

CommandDescription
xargs -a list.txt echoRead arguments from list.txt
xargs -a ips.txt -L 1 ping -c 1Read one IP per line and ping it
xargs -a packages.txt sudo apt installInstall packages listed in a file
xargs -a dirs.txt mkdir -pCreate directories from a file
xargs -a users.txt -n 1 idCheck users listed in a file

Troubleshooting

Quick checks for common xargs issues.

IssueCheck
Filenames split at spacesUse find -print0 | xargs -0
Too many arguments at onceAdd -n N to batch input
Command order looks wrongAdd -t to print generated commands
Empty input still runs commandUse -r to skip empty stdin
Need item in the middle of a commandUse -I {} with a placeholder

Use these guides for full command workflows.

GuideDescription
xargs Command in LinuxFull xargs tutorial with practical examples
find Files in LinuxBuild file lists to pass into xargs
rm Command in LinuxRemove files safely in bulk operations
grep Command in LinuxFilter text before passing results to xargs
Bash CheatsheetShell patterns for scripts and pipelines