8

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 ""

Sato
  • 8,192
  • 17
  • 60
  • 115

2 Answers2

16

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; '
dhirajforyou
  • 432
  • 1
  • 4
  • 11
4

The -c flag to sh only accepts one argument while xargs is splitting the arguments on whitespace - that's why the double quoting works (one level to make it a single word for the shell, one for xargs).

If you use the -0 or null argument to xargs your particular case will work:

echo ls -l -a / | xargs -0 sh -c
darklion
  • 1,025
  • 7
  • 11