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).

692 questions
142
votes
7 answers

When is xargs needed?

The xargs command always confuses me. Is there a general rule for it? Consider the two examples below: $ \ls | grep Cases | less prints the files that match 'Cases', but changing the command to touch will require xargs: $ \ls | grep Cases |…
Zaid
  • 10,442
  • 13
  • 38
  • 36
134
votes
6 answers

Sorting the output of "find -print0" by piping to the "sort" command

I need to be able to alphabetically sort the output of find before piping it to a command. Entering | sort | between didn't work, so what could I do? find folder1 folder2 -name "*.txt" -print0 | xargs -0 myCommand
Industrial
  • 1,771
  • 4
  • 13
  • 12
101
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,163
  • 2
  • 9
  • 8
88
votes
3 answers

Piping commands after a piped xargs

HP-UX ***** B.11.23 U ia64 **** unlimited-user license find . -type d -name *log* | xargs ls -la gives me the directory names (the ones which contain log in the directory name) followed by all files within that directory. The directories …
anotherperson1
  • 1,199
  • 3
  • 11
  • 20
61
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,075
  • 1
  • 9
  • 13
60
votes
5 answers

How can I find files and then use xargs to move them?

I want to find some files and then move them. I can find the file with: $ find /tmp/ -ctime -1 -name x* I tried to move them to my ~/play directory with: $ find /tmp/ -ctime -1 -name x* | xargs mv ~/play/ but that didn't work. Obviously mv needs…
Michael Durrant
  • 41,213
  • 69
  • 165
  • 232
56
votes
8 answers

Using xargs with input from a file

Say I have a file with the following bob john sue Now these directly corrospond to (in this case) URL pattern such as http://example.com/persons/bob.tar, john.tar, sue.tar. I would like to take these lines and run them through xargs. I don't know…
Josh K
  • 3,866
  • 4
  • 22
  • 15
54
votes
5 answers

How to quote arguments with xargs

Suppose that I want to delete all files in a folder that are greater than 1 MB. $ find . -size +1M | xargs -0 rm This will not delete files that have space in their names. So I want it to quote all arguments it sends to rm. If find gives it Some…
Kshitiz Sharma
  • 8,585
  • 21
  • 59
  • 75
51
votes
5 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
  • 713
  • 1
  • 5
  • 6
50
votes
1 answer

GNU parallel vs & (I mean background) vs xargs -P

I'm confused about the difference or advantage (if any) of running a set of tasks in a .sh script using GNU parallel E.g. Ole Tange's answer: parallel ./pngout -s0 {} R{} ::: *.png rather than say looping through them putting them in the background…
46
votes
2 answers

How to use defined function with xargs

This is my code #!/bin/bash showword() { echo $1 } echo This is a sample message | xargs -d' ' -t -n1 -P2 showword So I have a function showword which echoes whatever string you pass as a parameter to the function. Then I have xargs trying to…
GMaster
  • 5,992
  • 3
  • 28
  • 32
45
votes
4 answers

"Argument list too long": How do I deal with it, without changing my command?

When I run a command like ls */*/*/*/*.jpg, I get the error -bash: /bin/ls: Argument list too long I know why this happens: it is because there is a kernel limit on the amount of space for arguments to a command. The standard advice is to change…
D.W.
  • 3,921
  • 5
  • 33
  • 41
43
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
40
votes
4 answers

find -exec + vs find | xargs: which one to choose?

I understand that the -exec can take a + option to mimic the behaviour of xargs. Is there any situation where you'd prefer one form over the other? I personally tend to prefer the first form, if only to avoid using a pipe. I figure surely the…
rahmu
  • 19,673
  • 28
  • 87
  • 128
38
votes
3 answers

GNU find and masking the {} for some shells - which?

The man page for GNU find states: -exec command ; [...] The string `{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as…
user unknown
  • 10,267
  • 3
  • 35
  • 58
1
2 3
46 47