24 December 2021
In my daily work sometimes I need to execute commands that doesn't handle piped input,
fortunately there is a xargs
that could be used.
xargs
run COMMAND with arguments INITIAL-ARGS and more arguments read from input,
it is mainly created for use with commands not built to handle piped input or standard input such as cp
, rm
, du
, ... .
Note: all example below could be execute in a container:
docker run -it ubuntu /bin/bash
Example:
ls /etc/h* | xargs -I {} sh -c 'du -h {}'
The output is:
4.0K /etc/host.conf
4.0K /etc/hostname
4.0K /etc/hosts
Explanation about example:
ls /etc/h*
: list all files/folders that start with h
in etc
folder-I {}
replace INITIAL-ARGS with names read from standard inputsh -c 'du -h {}'
execute in a subshell the command du
(Disk Usage) to the input argumentxargs --help