Questions tagged [xargs]

xargs is a command that executes another command and generates its arguments from stdin

xargs can be used as a concise and safe alternative to loop-constructs and as a tool to parallelize jobs.

In contrast to a simple command substitution, it makes sure that the maximal argument vector size is not exceeded.

From a programming language point of view xargs is comparable to a higher-order function (like map or curry in Haskell).

702 questions
103
votes
2 answers

Make xargs pass as first parameter

I'm trying to produce this behaviour: grep 192.168.1 *.txt By passing a string into grep via Xargs but it is going on the end instead of as the first parameter. echo 192.168.1 | xargs grep *.txt I need to tell xargs (or something similar) to put…
andy boot
  • 1,193
65
votes
3 answers

How do I get xargs to show me the command lines it's generating without running them?

A fair number of linux commands have a dry-run option that will show you what they're going to do without doing it. I see nothing in the xargs man page that does that and no obvious way to emulate it. (my specific use case is troubleshooting long…
Andrew
  • 1,115
52
votes
6 answers

Why does xargs strip quotes from input?

Why does xargs strip quotes from input text? Here is a simplified example: echo "/Place/='http://www.google.com'" | xargs echo outputs /Place/=http://www.google.com Is there any way to work-around this? (xargs -0 doesn't help me)
ddario
  • 723
47
votes
3 answers

How to repeat variables twice in xargs

How can I make the second echo to echo out test in this example as well: echo test | xargs -I {} echo {} && echo {}
defiler
28
votes
3 answers

Passing multiple parameters via xargs

I'd like to be able to use xargs to execute multiple parameters in different parts of a command. For example, the following: echo {1..8} | xargs -n2 | xargs -I v1 -I v2 echo the number v1 comes before v2 I would hope that it would return the…
21
votes
1 answer

xargs -I option

The xargs manual says : -I replace-str --replace[=replace-str] -i[replace-str] Replace occurrences of replace-str in the initial arguments with names read from standard input. I don't understand this part: with names read from standard…
sjsam
  • 1,594
  • 2
  • 14
  • 22
14
votes
1 answer

How can I prevent arguments to `xargs` from being prefixed with spaces?

I have extracted an archive whose contents are not stored in a sub-directory into a directory with filled with other files and is now cluttered and confusing. To fix this I have extracted the archive into a new empty directory and want to use the…
vfclists
  • 7,531
  • 14
  • 53
  • 79
12
votes
4 answers

Format output of xargs

I would like to change the format of the output xargs displays cat k.txt 1 2 3 And cat k.txt | xargs 1 2 3 However I would like to have 1, 2, 3 or 1|2|3. Any suggestions?
Avinash
  • 403
12
votes
4 answers

Are `-L1` and `-n 1` the same for `xargs`?

From Search for and remove files safely locate -i nohup.out | xargs -d '\n' -L1 -p rm Each line in the output of locate is treated as a argument by xargs, so are -L1 and -n 1 the same?
Tim
  • 101,790
11
votes
1 answer

xargs -r0 vs xargs -0

I have seen examples on the web of xargs with the argument -r0. What does this argument mean? My man pages don't show any entries for -r0 nor do most man pages I have seen (e.g. http://linux.die.net/man/1/xargs)
9
votes
1 answer

Preserve order of outputs of commands executed in parallel by xargs

When I run a command with xargs -n 1 -P 0 for parallel execution, the output is all jumbled. Is there a way to do parallel execution, but make sure that the entire output of the first execution is written to stdout before the output of the second…
UTF-8
  • 3,237
8
votes
2 answers

How does xargs know when a stdin input ends, so that it can start processing it?

After reading Stephen Kitt's reply, xargs waits for receiving the stdin input before processing any of the input, such as splitting it into arguments. How does xargs know when a stdin input ends, so that it can start processing it? Is -E used for…
Tim
  • 101,790
6
votes
1 answer

What does "Trailing blanks cause an input line to be logically continued on the next input line" mean?

From the manual of xargs: -L max-lines Use at most max-lines nonblank input lines per command line. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x. What does "Trailing blanks cause an input line…
Tim
  • 101,790
6
votes
2 answers

Why are "{}" and "%" commonly used as replstr in xargs?

I often see xargs -I {} or xargs -I % (less). Is there any reason why those two are commonly used? Why is {}, as longer and more likely to be bad-interpreted, preferred?
user103231
6
votes
1 answer

xargs: running command once with all arguments

My aim is to get a list of files that have been modified in git, then run rspec command passing each file in as an argument. Currently I have: $ git status -s | awk '{if ($1 == "M") print $2}' | tr "\\n" "\\0" | \ xargs -0 -I % rspec -f…
1
2 3 4