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
8
votes
4 answers

How to use substitution in xargs?

What I want to do is find all file with .txt extension cp them to .dat file it could do like this: for f in `find . -type f -name "*.txt"`; do cp $f ${f%.txt}.dat; done I want to do this with xargs, I have tried this: find . -type f -name…
roger
  • 9,063
  • 20
  • 72
  • 119
8
votes
2 answers

Run a specifiable number of commands in parallel - contrasting xargs -P, GNU parallel, and "moreutils" parallel

I'm trying to run multiple mongodump's on 26 servers in a bash script. I can run 3 commands like mongodump -h staging .... & mongodump -h production .... & mongodump -h web ... & at the same time, and when one finishes I want to start another…
basante
  • 515
  • 3
  • 9
  • 20
8
votes
2 answers

xargs sh -c skipping the first argument

I'm trying to write a script that uses find and xargs to archive old files in a large directory. Here is the line: find /tmp/messages/ -mtime +9 -print0 | xargs -x -t -0 -n 1000 sh -c 'tar rPf /tmp/backup.tar "$@" && rm -f "$@"; sleep 1 && echo…
Guillermo Mirandes
  • 435
  • 1
  • 5
  • 8
8
votes
3 answers

xargs command length limits

I am using jsonlint to lint a bunch of files in a directory (recursively). I wrote the following command: find ./config/pages -name '*.json' -print0 | xargs -0I % sh -c 'echo Linting: %; jsonlint -V ./config/schema.json -q %;' It works for most…
Alec Fenichel
  • 1,257
  • 1
  • 13
  • 27
8
votes
2 answers

How to pass command with parameters to xargs

echo ls -l -a / | xargs sh -c How to make above command work? it only list current directory seems only ls is passed to xargs echo '"ls -l -a /"' | xargs sh -c would work though, but the input I got has no ""
Sato
  • 8,192
  • 17
  • 60
  • 115
8
votes
2 answers

Redirect output of xargs to file

I want to delete the first line of every files of a directory and save the corresponding output by appending a '.tmp' at the end of each of the filename. For example, if there is a file named input.txt with following content: line 1 line 2 I want…
Rafi Kamal
  • 4,522
  • 8
  • 36
  • 50
8
votes
4 answers

xargs: how to have literal double-quotes in replacement?

I have a source file with JSON-Objects one per line like this: source: {"_id":"1","name":"one"} {"_id":"2","name":"two"} {"_id":"3","name":"three"} I want to send each line to a curl -X POST -H "application/json" myURL -d '' The…
tschloss
  • 367
  • 3
  • 11
8
votes
2 answers

What is the PowerShell equivalent to this Bash command?

I'm trying to create a CLI command to have TFS check out all files that have a particular string in them. I primarily use Cygwin, but the tf command has trouble resolving the path when run within the Cygwin environment. I figure PowerShell should be…
Herms
  • 37,540
  • 12
  • 78
  • 101
8
votes
7 answers

how to ping each ip in a file?

I have a file named "ips" containing all ips I need to ping. In order to ping those IPs, I use the following code: cat ips|xargs ping -c 2 but the console show me the usage of ping, I don't know how to do it correctly. I'm using Mac os
user1687717
  • 3,375
  • 7
  • 26
  • 29
8
votes
2 answers

how to prompt a user from a script run with xargs

I'm using xargs to populate the arguments to a script in which I want to stop the script, waiting for user input. Something like: echo a b c | xargs bash -c 'for a in "$@"; do echo $a; read; done' but the read gets ignored. It seems that the second…
drevicko
  • 14,382
  • 15
  • 75
  • 97
7
votes
5 answers

Add wait between parallel processes in bash

I have a bash script to upload data to a site. I was getting slow upload speeds, so I started running it in parallel, 5 at the same time, using xargs and -N1. However, the problem is that the server asks me to solve a captcha if I run it 5 at a…
Amir
  • 2,082
  • 4
  • 22
  • 28
7
votes
1 answer

How do xargs and gnu parallel differ when parallelizing code?

Here's a basic question. I'm curious as to how do xargs and gnu parallel differ when parallelizing code? And are there use cases in which you'd use one over the other? I ask this because I have seen answers to parallelization questions where using…
Kleber Noel
  • 303
  • 3
  • 9
7
votes
2 answers

Cannot use env variables in xargs

Team, I cannot see variable substitution while performing xargs command. any hint, how to replace $hostname? on my system hostname is a command and not a variable defined in env. But i can set it to variable also.. hostname="$(hostname)" ls -ltr…
user10724620
7
votes
2 answers

multi process bash within fzf --preview feature

I am trying to use fzf in the following manner, I would like to be able to search for a term within my codebase and then with the preview window be able to see the file which contains the string I am searching for at the line where the string is…
Reginald Marr
  • 387
  • 2
  • 16
7
votes
1 answer

Is "xargs" on MacOS not the same as linux?

For the following command: docker ps -a -q | xargs -r docker kill I get this error: xargs: illegal option -- r What would be the MacOS equivalent of the above command?
Winker
  • 805
  • 2
  • 7
  • 9