Hot answers tagged

108 votes
Accepted

How do I get xargs to show me the command lines it's generating without running them?

You may benefit from the -p or -t flags. xargs -p or xargs --interactive will print out the command to be executed and then prompt for input (y/n) to confirm before executing the command. % cat list ...
Kevin Hencke's user avatar
  • 1,196
60 votes

cp after xargs not working

While you already know how you should solve your current problem, I'll still answer about xargs. xargs puts the string it got in the end of command, while in your case you need that string before the ...
aragaer's user avatar
  • 893
54 votes

How to repeat variables twice in xargs

Just write {} two times in your command. The following would work: $ echo test | xargs -I {} echo {} {} test test Your problem is how the commands are nested. Lets look at this: echo test | xargs -...
JojOatXGME's user avatar
53 votes
Accepted

Passing multiple parameters via xargs

I believe that you can’t use -I that way.  But you can get the effect / behavior you want by saying: echo {1..8} | xargs -n2 sh -c 'echo "the number $1 comes before $2"' sh This, essentially, ...
Scott - Слава Україні's user avatar
48 votes

How to quote arguments with xargs

I had a similar requirement and ended up using the -I switch to have a placeholder and I was able to quote it. find . -size +1M | xargs -I {} rm "{}"
dee-see's user avatar
  • 589
38 votes

Why does xargs strip quotes from input?

if you want xargs to ignore quotes one of the good soultion can be the use of xargs flag xargs -0 Directly from Man page OPTIONS OPTIONS -0, --null Input items are terminated by a ...
Balman Rawat's user avatar
34 votes
Accepted

xargs -I option

"with names read from standard input" means that xargs takes the data coming in on its standard input, splits it up, and uses it to run the command given in its arguments. By default, it splits on ...
Stephen Kitt's user avatar
33 votes
Accepted

Why does 'top' fail to run via 'xargs'?

Turns out that there is a special option --open-tty in xargs for interactive applications like top. From man xargs: -o, --open-tty Reopen stdin as /dev/tty in the child process before ...
ks1322's user avatar
  • 1,646
28 votes

Why does xargs strip quotes from input?

For GNU xargs, I found another solution in the manpage: explicitly specify delimiter to be '\n'. This turns off special handling for quotes: --delimiter=delim, -d delim Input items are terminated by ...
fungusakafungus's user avatar
26 votes

Have xargs use alias instead of binary

Use alias xargs='xargs ' alias: alias [-p] [name[=value] ... ] (snip) A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded.
1.61803's user avatar
  • 1,211
26 votes
Accepted

xargs - append each argument with a parameter

One way to do it: echo "a b c" | xargs printf -- '-f %s\n' | xargs mycommand This assumes a, b, and c don't contain blanks, newlines, quotes or backslashes. :) With GNU findutil you can handle the ...
Satō Katsura's user avatar
26 votes
Accepted

Copy multiple files using command `xargs`

As the cp error indicates, the target directory must come last. Since it looks like your cp doesn't have an equivalent of GNU cp's -t option, you have to get xargs to insert the filename between cp ...
muru's user avatar
  • 70.1k
24 votes

How do I get xargs to show me the command lines it's generating without running them?

Put an echo in front of the command to run? $ echo a b c d e | xargs -n2 echo rm rm a b rm c d rm e
ilkkachu's user avatar
  • 134k
24 votes
Accepted

How can I prevent arguments to `xargs` from being prefixed with spaces?

Use -I {}, and {} at the place where you want the argument to appear: xargs -I {} -n 1 echo rm /usr/src/packages/{} (You can use something other than {}, {} is just very common.) Without this, ...
muru's user avatar
  • 70.1k
24 votes

find . -print0 | xargs -0 cmd vs find . -exec cmd {} +

TL;DR There's no clear winner. My recommendation would be to use: find . -exec cmd {} + Wherever it's enough as it's more portable, uses less resource and has fewer issues, and one of: xargs -r0 -...
Stéphane Chazelas's user avatar
23 votes
Accepted

How can I get the index of the xargs "parallel processor"?

If you're using GNU xargs, there's --process-slot-var: --process-slot-var=environment-variable-name Set the environment variable environment-variable-name to a unique value in each running ...
muru's user avatar
  • 70.1k
22 votes

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

Perhaps this command is possible now and wasn't back in 2013, but this works perfectly for me: ls pattern* | xargs mv -t DESTINATION/ The -t key puts the destination folder first, freeing up mv ...
Nikhil VJ's user avatar
  • 331
22 votes
Accepted

How to give a comma-separated list as arguments to the next command

This should equally work as well: s1 | xargs -d "," -n1 s2 Test case: printf 1,2,3,4 | xargs -d ',' -n1 echo Result: 1 2 3 4 If s1 outputs that list followed by a newline character, you'd want ...
George Udosen's user avatar
21 votes

Why does 'top' fail to run via 'xargs'?

top is an interactive program, for instance you can type i to toggle showing idle processes. Whilst it could arrange to read from /dev/tty in practice it expects that stdin is connected to the ...
icarus's user avatar
  • 17.5k
20 votes
Accepted

How to make xargs handle spaces and special characters?

Use -d '\n' with your xargs command: cat file | xargs -d '\n' -l1 mkdir From manpage: -d delim Input items are terminated by the specified character. Quotes and backslash are not ...
Pandya's user avatar
  • 24k
20 votes
Accepted

find and sed string in docker got error ` Device or resource busy`

Yes, as you found, the file is mounted by docker, which means you are not allowed to change its inode from within docker container. But, what if you only change the content of file without touching ...
Bruce's user avatar
  • 1,191
20 votes
Accepted

If no input is given to xargs, don't let xargs run the utility

You are looking for the -r option. This is a non-standard option which may be implemented on your system (check your xargs manual). From the GNU xargs manual: -r, --no-run-if-empty If the standard ...
Kusalananda's user avatar
  • 322k
19 votes
Accepted

One liner to see if grep returned something?

Just tack the exit status check after grep, it will always get the exit status from the last command of the pipeline by default: sudo dmidecode | grep -q ThinkPad; echo $? Use -q to suppress any ...
heemayl's user avatar
  • 54.9k
18 votes
Accepted

Remove all duplicate word from string using shell script

One more awk, just for fun: $ a="aaa bbb aaa bbb ccc aaa ddd bbb ccc" $ echo "$a" | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}' aaa bbb ccc ddd By the way, even ...
George Vasiliou's user avatar
17 votes

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

The {} needed to be quoted in versions of the fish shell prior to 3.0.0. $ fish -c 'echo find -exec {} \;' find -exec ; And in the rc shell (also akanga based on rc, but not es): $ rc -c "echo ...
Stéphane Chazelas's user avatar
17 votes
Accepted

What is difference between results and effectiveness after adding -f in rm and -print in find?

-f tells rm to never prompt (e.g. when it encounters read-only files) and to ignore missing files it’s asked to delete (instead of indicating an error). It will also not complain if not passed any ...
Stephen Kitt's user avatar
17 votes
Accepted

How do I create directories named after each month?

With -I, xargs gets one argument per line as opposed to the default of one argument per (blank or newline delimited, possibly quoted) word without -I (and implies -n). So in your example date is ...
Stéphane Chazelas's user avatar
16 votes
Accepted

ssh-add add all private keys in .ssh directory

Slightly convoluted, but: for possiblekey in ${HOME}/.ssh/id_*; do if grep -q PRIVATE "$possiblekey"; then ssh-add "$possiblekey" fi done You can also add all of your keys to your ~/....
DopeGhoti's user avatar
  • 74k
16 votes
Accepted

How do I use `find` to go to directory of that file

At least if you have GNU find, you can use -printf '%h' to get the directory %h Leading directories of file's name (all but the last ele‐ ment). If the file name contains no ...
steeldriver's user avatar
  • 78.7k
16 votes
Accepted

Xargs into second side of pipe?

It's because you need a shell to create a pipe or perform redirection. Note that cat is the command to concatenate, it makes little sense to use it just for one file. cat file1.txt | xargs -I{} sh -c ...
Stéphane Chazelas's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible