Xargs Command

Xargs Command in Linux

GNU/Linux has many utilities that are unprecedentedly powerful and useful, but not very popular. These include xargs command. Its main purpose is to work with the output of other terminal commands. This article describes the syntax of this utility, as well as how to use it.

Syntax and Options of Xargs Command

Xargs accepts the outgoing data of other programs (it may be file names, process numbers, whatever) and performs some manipulations with them. However, the utility can also look at the standard output stream when typing in the terminal. And, if it is launched without any parameters, then the default echo command will be executed relative to the whole typed text (completion of the entry is done by pressing Ctrl + d).

xargs command

Here is the general syntax of the utility:

[SOME_COMMANDS |] xargs [params] [command [initial-argument]]

As SOME_COMMANDS (an optional parameter for the program to function which is often used when working), there can be any commands that transmit some data through the pipe at the output. Then xargs performs the operation specified as a parameter on them.

By default, data is inserted at the very end after the name of the operating utility, but this can be changed by specifying curly braces in the place where they need to be inserted.

Below is a list of commonly used options:

Short (BSD) form Long (GNU) form Value
-0 –null replaces the space and line feed characters with the NULL separator character (“\ 0”); goes well with the find command
-l [max-lines] –maxlines[=max-lines] executes a command for each group from a given number of non-empty lines of arguments read from standard input; unlike -L, the max-lines argument is optional
-I replace indicates a closing pair of characters (usually use {} or []), instead of which the data transmitted to the left will be substituted for the subsequent operation
-n max-args –max-args=max-args executes a command using all read arguments from standard input, the number of which does not exceed the specified maximum-args
-p –interactive turns on the invitation mode: before each call of the command confirmation is requested
-r –no-run-if-empty do not call the utility if the input list is empty
-t –verbose turns on trace mode, showing the full name of the command being executed before running in the standard error stream

However, the main task of xargs is to be a pipeline for the data on the left, used in the utility on the right.

Xargs Examples

Most often, the utility receives data from find. Despite the fact that the latter has the ability to perform operations through exec, this is a rather limited and slow way when working with many objects. So, the most of the examples will be related to find command.

It is worth remembering that xargs does not process the names of objects containing a space. To exit the situation, use the -0 parameter.

Copy Files with Xargs

Find all files with the extension .sh and in trace mode copy them to another directory. The usual use of xargs cp destination/ will not help, since by default these files will be inserted at the end of this command, which means that an attempt will be made to copy the destination/ directory to them, which is absurd. Therefore, you should add the -I option with curly brackets, and then specify in cp the place where the copied files will be substituted:

find . -name "*.sh" | xargs --verbose -I {} cp {} scripts/

xargs example

Column Output

You can display outgoing data in several columns. To do this, use the -n option:

ls | xargs -n 2 | column -t

linux xargs

Archiving and Compressing Files with Xargs Command

The find utility finds all the necessary files, and xargs sends them for archiving and compression:

find . -name “*.sh” | xargs tar jcf sh.tar.bz


Delete Old Files

A more complicated operation that deletes objects, for example, in the ~/tmp directory, if they were created 7 days ago or more:

find ~/tmp -type f -name ‘*’ -mtime +7 -print0 | xargs -0 rm -f

Block Specific IP

A very convenient operation that allows you to block the IP pool, for example, from a file:

cat ip_blacklist | xargs -I IP iptables -A INPUT -s IP -j DROP

Removing Old Kernel Packages

Useful operation when many unused kernel packages have already accumulated (applicable on systems based on Debian and Ubuntu):

dpkg -l linux-* | gawk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt -y purge

Add Your Comment