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
34
votes
5 answers

Use xargs to mv a directory from find results into another directory

I have the following command: find . -type d -mtime 0 -exec mv {} /path/to/target-dir \; This will move the directory founded to another directory. How can I use xargs instead of exec to do the same thing.
zjhui
  • 779
  • 1
  • 8
  • 21
30
votes
6 answers

When should xargs be preferred over while-read loops?

xargs is widely used in shell scripting; it is usually easy to recast these uses in bash using while read -r; do ... done or while read -ar; do ... done loops. When should xargs be preferred, and when should while-read loops be preferred?
Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
28
votes
5 answers

Final arguments for xargs

I want to do something similar to this: find . -type f | xargs cp dest_dir But xargs will use dest_dir as initial argument, not as final argument. I would like to know: Is it possible to specify final arguments to xargs? Any other alternative to…
blueFast
  • 41,341
  • 63
  • 198
  • 344
27
votes
3 answers

Removing files with rm using find and xargs

When I do rm file.txt or rm *.txt I'm prompted for each file, since I didn't specify the -f option to rm. But when I do this: find . -type f -name '*.txt' | xargs rm the files are removed without the confirmation. What is the logics behind this?…
Martin G
  • 17,357
  • 9
  • 82
  • 98
27
votes
1 answer

Pass all elements in existing array to xargs

I am trying to pass an array of file paths to xargs to move them all to a new location. My script is currently working as follows: FILES=( /path/to/files/*identifier* ) if [ -f ${FILES[0]} ] then mv ${FILES[@]} /path/to/destination fi The…
Matt
  • 1,792
  • 5
  • 21
  • 33
25
votes
8 answers

How to execute multiple commands after xargs -0?

find . -name "filename including space" -print0 | xargs -0 ls -aldF > log.txt find . -name "filename including space" -print0 | xargs -0 rm -rdf Is it possible to combine these two commands into one so that only 1 find will be done instead of 2? I…
Richard Chen
  • 303
  • 1
  • 3
  • 10
25
votes
1 answer

how to pass "one" argument and use it twice in "xargs" command

I tried to use the xargs to pass the arguments to the echo: [usr@linux scripts]$ echo {0..4} | xargs -n 1 echo 0 1 2 3 4 the -n 1 insured that the xargs pass 1 arguments a time to the echo. Then I want to use this aruments twice, however the…
spring cc
  • 937
  • 1
  • 10
  • 19
25
votes
3 answers

How to sleep for 1 second between each xargs command?

For example, if I execute ps aux | awk '{print $1}' | xargs -I {} echo {} I want to let the shell sleep for 1 second between each echo. How can I change my shell command?
WDan
  • 533
  • 1
  • 4
  • 13
22
votes
3 answers

Can xargs execute a subshell command for each argument?

I have a command which is attempting to generate UUIDs for files: find -printf "%P\n"|sort|xargs -L 1 echo $(uuid) But in the result, xargs is only executing the $(uuid) subshell once: 8aa9e7cc-d3b2-11e4-83a6-1ff1acc22a7e…
adelphus
  • 10,116
  • 5
  • 36
  • 46
21
votes
4 answers

If xargs is map, what is filter?

I think of xargs as the map function of the UNIX shell. What is the filter function? EDIT: it looks like I'll have to be a bit more explicit. Let's say I have to hand a program which accepts a single string as a parameter and returns with an exit…
jameshfisher
  • 34,029
  • 31
  • 121
  • 167
21
votes
3 answers

Can xargs' default delimiter be changed?

I want the following behavior without having to explicitly specify it with options: xargs -d '\n' Unlike with most commands, you can't just use an alias because pipes don't recognize aliases (as a side-note, why is it designed this way?). I also…
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
21
votes
3 answers

piping seq to printf for number formatting

I'm trying to print the following pattern using printf and seq: 0000 0001 0002 0003 My problem is once I use: seq 0 10 | xargs printf %04d all my output is formatted into the same line likeso: 0000000100020003 I still can't get the hang of using…
Paolo
  • 1,557
  • 3
  • 18
  • 28
19
votes
4 answers

wget or curl from stdin

I'd like to download a web pages while supplying URLs from stdin. Essentially one process continuously produces URLs to stdout/file and I want to pipe them to wget or curl. (Think about it as simple web crawler if you want). This seems to work…
maximdim
  • 8,041
  • 3
  • 33
  • 48
19
votes
3 answers

Why does "locate filename | xargs vim" cause strange terminal behaviour?

When I do "locate 50local.policy | xargs vim", I get the error "Vim: Warnung: Die Eingabe kommt nicht von einem Terminal" (translation: Vim: Warning: The input does not come from a terminal). I can edit successfully with vim but after I close it my…
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
19
votes
3 answers

How to make xargs append trailing arguments with -n?

xargs is good at inserting initial arguments: seq 0 10 | xargs -n 3 echo foo produces this output: foo 0 1 2 foo 3 4 5 foo 6 7 8 foo 9 10 What about when I also want trailing arguments? That is, what command: seq 0 10 | xargs -n 3
Don Hatch
  • 5,041
  • 3
  • 31
  • 48
1 2
3
86 87