60

Let's say I want to write a script that does ls with a prefix for each filename. I tried this

ls | xargs -n 1 echo prefix_

But the result was

prefix_ first_file
prefix_ second_file
...

How can I remove the space between the prefix and the filename? I.e. how to I make xargs put the variable after the command, without space? (Or in any other place for that matter)

Dotan
  • 6,602
  • 10
  • 34
  • 47

1 Answers1

100

The solution: -I

-I lets you name your argument and put it anywhere you like. E.g.

ls | xargs -n 1 -I {} echo prefix_{}

(replace {} with any string)

Dotan
  • 6,602
  • 10
  • 34
  • 47