Dotan
Dotan

Reputation: 7622

xargs - place the argument in a different location in the command

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)

Upvotes: 75

Views: 36413

Answers (1)

Dotan
Dotan

Reputation: 7622

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)

Upvotes: 122

Related Questions