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
485
votes
13 answers

Make xargs execute the command once for each line of input

How can I make xargs execute the command exactly once for each line of input given? It's default behavior is to chunk the lines and execute the command once, passing multiple lines to each instance. From http://en.wikipedia.org/wiki/Xargs: find…
readonly
  • 343,444
  • 107
  • 203
  • 205
432
votes
11 answers

Running multiple commands with xargs

cat a.txt | xargs -I % echo % In the example above, xargs takes echo % as the command argument. But in some cases, I need multiple commands to process the argument instead of one. For example: cat a.txt | xargs -I % {command1; command2; ... } But…
Dagang
  • 24,586
  • 26
  • 88
  • 133
405
votes
15 answers

Make xargs handle filenames that contain spaces

$ ls *mp3 | xargs mplayer Playing Lemon. File not found: 'Lemon' Playing Tree.mp3. File not found: 'Tree.mp3' Exiting... (End of file) My command fails because the file "Lemon Tree.mp3" contains spaces and so xargs thinks it's two…
showkey
  • 482
  • 42
  • 140
  • 295
275
votes
7 answers

How to ignore xargs commands if stdin input is empty?

Consider this command: ls /mydir/*.txt | xargs chown root The intention is to change owners of all text files in mydir to root The issue is that if there are no .txt files in mydir then xargs thows an error saying there is no path specified. This…
HyderA
  • 20,651
  • 42
  • 112
  • 180
256
votes
4 answers

Calling shell functions with xargs

I am trying to use xargs to call a more complex function in parallel. #!/bin/bash echo_var(){ echo $1 return 0 } seq -f "n%04g" 1 100 |xargs -n 1 -P 10 -i echo_var {} exit 0 This returns the error xargs: echo_var: No such file or…
fac3
  • 2,681
  • 2
  • 14
  • 5
252
votes
22 answers

How can I use xargs to copy files that have spaces and quotes in their names?

I'm trying to copy a bunch of files below a directory and a number of the files have spaces and single-quotes in their names. When I try to string together find and grep with xargs, I get the following error: find .|grep "FooBar"|xargs -I{} cp "{}"…
Drew Stephens
  • 17,207
  • 15
  • 66
  • 82
194
votes
4 answers

How to use > in an xargs command?

I want to find a bash command that will let me grep every file in a directory and write the output of that grep to a separate file. My guess would have been to do something like this ls -1 | xargs -I{} "grep ABC '{}' > '{}'.out" but, as far as I…
Jesse Shieh
  • 4,660
  • 5
  • 34
  • 49
122
votes
3 answers

Running programs in parallel using xargs

I currently have the current script. #!/bin/bash # script.sh for i in {0..99}; do script-to-run.sh input/ output/ $i done I wish to run it in parallel using xargs. I have tried script.sh | xargs -P8 But doing the above only executed once at…
Olivier
  • 1,981
  • 7
  • 24
  • 29
85
votes
17 answers

xargs with multiple arguments

I have a source input, input.txt a.txt b.txt c.txt I want to feed these input into a program as the following: my-program --file=a.txt --file=b.txt --file=c.txt So I try to use xargs, but with no luck. cat input.txt | xargs -i echo "my-program…
Howard
  • 19,215
  • 35
  • 112
  • 184
77
votes
10 answers

How to delete many 0 byte files in linux?

I've a directory with many number of 0 byte files in it. I can't even see the files when I use the ls command. I'm using a small script to delete these files but sometimes that does not even delete these files. Here is the script: i=100 while [ $i…
small_ticket
  • 1,910
  • 5
  • 22
  • 30
77
votes
8 answers

Modifying replace string in xargs

When I am using xargs sometimes I do not need to explicitly use the replacing string: find . -name "*.txt" | xargs rm -rf In other cases, I want to specify the replacing string in order to do things like: find . -name "*.txt" | xargs -I '{}' mv…
betabandido
  • 18,946
  • 11
  • 62
  • 76
72
votes
4 answers

What's the equivalent of xargs in PowerShell?

The POSIX-defined xargs command takes all of the items it receives from standard input and passes them as command-line arguments to the command it receives on it's own command line. E.g: grep -rn "String" | xargs rm. What's the equivalent in…
simonwo
  • 3,171
  • 2
  • 19
  • 28
71
votes
6 answers

xargs doesn't recognize bash aliases

I'm trying to run the following command: find . -iname '.#*' -print0 | xargs -0 -L 1 foobar where "foobar" is an alias or function defined in my .bashrc file (in my case, it's a function that takes one parameter). Apparently xargs doesn't…
Ian Greenleaf Young
  • 1,888
  • 2
  • 15
  • 23
60
votes
1 answer

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…
Dotan
  • 6,602
  • 10
  • 34
  • 47
59
votes
10 answers

Tarballing without Git metadata

My source tree contains several directories which are using Git source control, and I need to tarball the whole tree excluding any references to the Git metadata or custom log files. I thought I'd have a go using a combination of…
zaf
  • 22,776
  • 12
  • 65
  • 95
1
2 3
86 87