I had a problem where I wanted to find the length of each path from a find command. My first attempt was to run something like this:
find . -exec sh -c "echo {} | wc -c" \;
I got this idea from this answer. (The command above is not my question, I'm just using it as an example and it's completely contrived. Also, I may need more than one pipe sometimes.)
But when I ran it, there were errors in the output, likely due to special characters in the output paths. Unfortunately, I don't have the knowledge to troubleshoot which paths caused the issue and the error messages weren't informative. Regardless...
I later stumbled upon this answer:
The
findcommand executes the command directly. The command, including the filename argument, will not be processed by the shell or anything else that might modify the filename. It's very safe.
That seems very convenient. So convenient, in fact, that the -exec sh -c ... "cure" seems worse than the disease.
So my question is, what should I do when I need to pipe commands with find and my paths may have special characters? Is there a generic solution to this problem where I don't have to consider a bunch of caveats? I am using bash.
Note: This is a similar question: how best to send the output of a find + exec command to a pipeline? The difference is, I'm not necessarily trying to pipe the output outside of -exec. i.e., if find ... -exec ... foo {} | bar \; is the way to go, that's perfectly fine by me. I'm just looking for a generic path of least resistance, the structure of the command isn't important to me.