Skip to content

xargs Command

Summary

The xargs command builds and executes command lines from standard input. It's a powerful tool for processing lists of files or other data from the output of other commands.

Introduction

The xargs command reads items from standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes a specified command one or more times with the items as arguments. This is particularly useful when you need to process a large number of files or perform an action on each line of output from another command. It prevents "argument list too long" errors that can occur when trying to pass a huge number of arguments directly to a command.

Use case and Examples

Executing rm on a list of files

find . -name "*.tmp" -print0 | xargs -0 rm -f
This example first finds all files with the .tmp extension in the current directory and its subdirectories. The -print0 option outputs the filenames separated by null characters, which is then piped to xargs. The -0 option of xargs tells it to expect null-separated input, making it safe for filenames containing spaces or special characters. Finally, xargs executes rm -f for each file found, forcefully deleting them.

Creating directories based on a list

cat dirlist.txt | xargs mkdir
Assuming dirlist.txt contains a list of directory names, one per line, this command reads those names from the file and then uses xargs to create directories with those names using mkdir.

Compressing multiple files using gzip

ls *.txt | xargs gzip
This command lists all files with the .txt extension in the current directory and pipes that list to xargs. xargs then executes gzip on each of the files, compressing them.

Moving files to a specific directory

find . -type f -name "*.log" -print0 | xargs -0 -I {} mv {} /path/to/logs/
This command finds all files ending with .log in the current directory and subdirectories. The -print0 and -0 options ensure safe handling of filenames with spaces. The -I {} option tells xargs to replace each occurrence of {} in the command with the filename. The command then moves each log file to the /path/to/logs/ directory.

Commonly used flags

Flag Description Example
-0 Uses null characters as delimiters. Useful for filenames with spaces or special characters. find . -name "*.tmp" -print0 | xargs -0 rm -f
-n max-args Specifies the maximum number of arguments xargs will pass to the command at a time. ls | xargs -n 2 touch (Creates files, two at a time)
-I replace-str Replaces occurrences of replace-str in the command with the arguments from standard input. find . -name "*.txt" | xargs -I {} cp {} /tmp/ (Copies each found file to /tmp/)
-L max-lines Reads at most max-lines lines of input; if max-lines is 0, reads at least one line. Trailing blanks are not considered as end of line. cat filelist.txt | xargs -L 1 command (Executes command on each line of filelist.txt.)
-d delimiter Specifies the delimiter used to separate items in the input. echo "a,b,c" | xargs -d , echo (Outputs a b c)
-t Echoes the command before executing it. Useful for debugging. ls *.txt | xargs -t gzip
-p Prompts the user before executing each command line. ls *.txt | xargs -p rm


Share on Share on

Comments