Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command with parameters to xargs

Tags:

linux

bash

xargs

echo ls -l -a / | xargs sh -c

How to make above command work?

it only list current directory

seems only ls is passed to xargs

echo '"ls -l -a /"' | xargs sh -c would work though, but the input I got has no ""

like image 628
Sato Avatar asked Jan 08 '16 03:01

Sato


1 Answers

Source: http://www.unixmantra.com/2013/12/xargs-all-in-one-tutorial-guide.html

as mentioned by @darklion, -I denoted the argument list marker and -c denotes bash command to run on every input line to xargs.

Simply print the input to xargs:

ls -d */ | xargs echo
#One at a time
ls -d */ | xargs -n1 echo

More operations on every input:

ls -d */  | xargs -n1 -I {} /bin/bash -c ' echo {}; ls -l {}; '

You can replace {} with customized string as:

ls -d */  | xargs -n1 -I file /bin/bash -c ' echo file; ls -l file; '
like image 169
dhirajforyou Avatar answered Nov 15 '22 04:11

dhirajforyou