Why is xargs not replacing the second {}

Viewed 3738

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.

3 Answers

The root cause of the problem is pointed out in Carlos' answer, but without a solution.

After some googling, I couldn't find a way to lift up the 255 characters limit.

So a probable way to workaround it, is to use shell variable as a substitution.

Example:

find . | xargs -I% sh -c 'F="%";iconv -f gb2312 -t utf-8 "$F">"$F.out";mv "$F.out" "$F"'

Remember to use single quotes at the outermost sh -c parameter string, we don't want the $F inside to be replaced by our parent shell.

Related