Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is xargs not replacing the second {}

Tags:

xargs

I'm using xargs to try to echo the name of a file, followed by its contents. Here is the command

find output/ -type f | xargs -I {} sh -c "echo {} ; cat {}"

However for some reason, the second replace after cat is not being replaced. Only for some files, so some files do work correctly.

To be clear, I'm not looking for a command that lets me echo the name of a file followed by its contents, I'm trying to understand why this specific command does not work.

like image 521
Carlos Bribiescas Avatar asked Aug 09 '17 15:08

Carlos Bribiescas


People also ask

What can I use instead of xargs?

If you can't use xargs because of whitespace issues, use -exec . Loops are just as inefficient as the -exec parameter since they execute once for each and every file, but have the whitespace issues that xargs have.

What is the point of xargs?

xargs (short for “eXtended ARGuments”) is a command on Unix and most Unix-like operating systems. It converts input from standard input (STDIN) into arguments to a command. In this brief tutorial, we'll see how we can use xargs to build and execute commands from standard input.

What is the default command used by xargs?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.

Can we use xargs and standard input?

xargs is a Unix command which can be used to build and execute commands from standard input.


1 Answers

Turns out that the command was too long, so it was working with shorter file names and failing for longer ones. From man xargs

-I replstr
             Execute utility for each input line, replacing one or more occurrences of replstr in up to replacements (or 5 if no -R flag is specified) arguments to utility with the
             entire line of input.  The resulting arguments, after replacement is done, will not be allowed to grow beyond 255 bytes; this is implemented by concatenating as much
             of the argument containing replstr as possible, to the constructed arguments to utility, up to 255 bytes.  The 255 byte limit does not apply to arguments to utility
             which do not contain replstr, and furthermore, no replacement will be done on utility itself.  Implies -x.

like image 55
Carlos Bribiescas Avatar answered Sep 22 '22 20:09

Carlos Bribiescas