Questions tagged [xargs]

xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input.

xargs is a command on Unix and most Unix-like operating systems used to build and execute command lines from standard input. Under the Linux kernel before version 2.6.23, arbitrarily long lists of parameters could not be passed to a command, so xargs breaks the list of arguments into sublists small enough to be acceptable.

xargs often covers the same functionality as the backquote(`) feature of many shells, but is more flexible and often also safer, especially if there are blanks or special characters in the input. It is a good companion for commands that output long lists of files like find, locate and grep, but only if you use -0, since xargs without -0 deals badly with file names containing ', " and space.

1303 questions
13
votes
1 answer

How can I avoid "no input files" error from sed, when run from xargs?

I have this shell script to update IP addresses in my configuration files (any that match $old_address_pattern must be changed to $new_address): grep -rl "$old_address_pattern" /etc \ | xargs sed -i "s/$old_address_pattern/$new_address/g" If the…
qurat
  • 439
  • 1
  • 7
  • 14
13
votes
2 answers

Golang: How to run "go test" repeatedly without recompiling?

Is there any way for me to easily run a Go test many times, halting the first time it fails? I can of course do something like this: for i in {1..1000}; do go test ./mypkg && done but that causes a recompile every time, which is very slow compared…
jacobsa
  • 5,719
  • 1
  • 28
  • 60
13
votes
1 answer

how to get exit code when using xargs (parallel)

I'd made a script for launching parallel rsync process: #! /bin/bash LIST=$1 DEST_DIR=$2 RSYNC_OPTS=$3 #echo "rsyncing From=$SRC_DIR To=$DEST_DIR RSYNC_OPTS=$RSYNC_OPTS" echo $LIST|xargs -n1 -d, echo|xargs -n1 -P 0 -I% rsync --rsync-path='sudo…
bLuEdDy
  • 193
  • 1
  • 7
13
votes
2 answers

reading in from stdin from a command executed with xargs

Using xargs did something I didn't quite expect, though I guess it sort of makes sense. This is not what I did, but this is an example which should show what happened. fn.sh #!/usr/bin/bash index=1 for arg in "$@"; do echo "Arg #$index = '$arg'";…
Adrian
  • 10,246
  • 4
  • 44
  • 110
12
votes
2 answers

How do I execute multiple commands in parallel on an array of parameters with bash, and fail if at least one of them failed

I have a bash script with a function that needs to run in parallel with different arguments. I need to know if at least one of the executions failed (returned non-zero) - doesn't matter how many failed. The command accepts an array of parameters for…
Avia Eyal
  • 123
  • 7
12
votes
3 answers

Shell Scripting: Using xargs to execute parallel instances of a shell function

I'm trying to use xargs in a shell script to run parallel instances of a function I've defined in the same script. The function times the fetching of a page, and so it's important that the pages are actually fetched concurrently in parallel…
Gnats
  • 273
  • 1
  • 3
  • 12
12
votes
7 answers

loop with more than one item at a time

We can iterate over a set of items, considering one at a time, like this: #!/bin/bash for i in $( ls ); do echo item: $i done How can we process several items at a time in a similar loop? Something like: #!/bin/bash for i,j,k in $( ls ); do …
Sten Kin
  • 2,485
  • 3
  • 23
  • 29
12
votes
3 answers

Get argument from pipe

Consider having the results from the pipe: find . Now I would like to access in the second command behind the pipe what is actually piped (inputed) and then for example to print it twice. find . | printf $arg$arg\n #each filename would be printed…
Dávid Natingga
  • 839
  • 3
  • 13
  • 30
11
votes
3 answers

xargs jar tvf - does not work

Objective: to list files in all jars. This works: for f in `find . -name "*.jar"`; do jar tvf $f; done This works too: find . -name "*.jar" -exec jar tvf {} \; This does not (it does not print any output): find . -name "*.jar" | xargs jar tvf Why…
Synesso
  • 37,610
  • 35
  • 136
  • 207
11
votes
5 answers

can xargs separate parameters?

echo "'param 1' 'param 2'" | xargs -n2 -I@ echo [@] [@] This command outputs: [param 1 param 2] [param 1 param 2] However, I would like it to output: [param 1] [param 2] Is there a way to do this with xargs? I plan to use this with -L1 so…
jcalfee314
  • 4,642
  • 8
  • 43
  • 75
11
votes
3 answers

Removing files interactively with find and xargs

I'm trying to pipe some files from the find command to the interactive remove command, so that I can double check the files I'm removing, but I've run into some trouble. find -name '#*#' -print0 | xargs -0 rm -i I thought the above would work, but…
CopOnTheRun
  • 1,117
  • 2
  • 12
  • 21
11
votes
7 answers

Shell Scripting: Using bash with xargs

I'm trying to write a bash command that will delete all files matching a specific pattern - in this case, it's all of the old vmware log files that have built up. I've tried this command: find . -name vmware-*.log | xargs rm However, when I run the…
Dan Monego
  • 9,637
  • 6
  • 37
  • 72
10
votes
3 answers

Why is xargs not replacing the second {}

I'm using xargs to try to echo the name of a file, followed by its contents. Here is the command find output/ -type f | xargs -I {} sh -c "echo {} ; cat {}" However for some reason, the second replace after cat is not being replaced. Only for some…
Carlos Bribiescas
  • 4,197
  • 9
  • 35
  • 66
10
votes
2 answers

Saving arguments passed from xargs to bash as a variable for processing

I am trying to run many commands in parallel, and need to do some string manipulation on the input first. How can I make the below example work? find . -mindepth 1 -type d | xargs -n 1 -P 20 -i sh -c "v={}; echo $v" When I use this, $v is null. Why…
Ian Fiddes
  • 2,821
  • 5
  • 29
  • 49
10
votes
2 answers

xargs (or something else) without space before parameter

I would like to execute something like this (git squash): git rebase -i HEAD~3 extracting the 3 from git log: git log | blabla | xargs git rebase -i HEAD~ This does not work because xargs inserts a space after HEAD~. The problem is that I want to…
Gismo Ranas
  • 6,043
  • 3
  • 27
  • 39