Skip to main content
118 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,296
61 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
  • 903
54 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
53 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
  • 639
39 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
36 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,656
29 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
29 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
  • 73.9k
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
28 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
26 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
  • 371
25 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
  • 73.9k
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
  • 141k
23 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
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
  • 73.9k
22 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,271
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
  • 18.2k
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
  • 339k
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
  • 56.7k
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
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
  • 76.9k
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
  • 81.8k
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
16 votes
Accepted

Why xargs does not process the last argument?

What went wrong b appears in the output, so it was processed, just not the way you expected. As a first step, ask bash to tell you what it sees: pass the -x option to enable its traces. $ echo a b | ...
Gilles 'SO- stop being evil''s user avatar
15 votes
Accepted

Piping list of files and directories to du only shows sizes of directories?

What's going on is that xargs puts (if it can) all of the names on one command-line, so that you see only one command passed to du. Then du ignores the filenames (as you might expect: the files are ...
Thomas Dickey's user avatar
15 votes
Accepted

cp after xargs not working

ls -al /var/cache/apt/archives/ | grep 'i386' | awk '{print $9}' can be simplified to /var/cache/apt/archives/*i386* So, use either of these two: cp -u /var/cache/apt/archives/*i386* /home/...
Sundeep's user avatar
  • 12.1k
14 votes
Accepted

List files with timestamp since epoch

If you have GNU find, you can do it entirely using find: find / -printf '%p %C@\n' The format specifiers are: %p File's name. %Ck File's last status change time in the format ...
muru's user avatar
  • 73.9k

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