0

Using find to select files to pass to another command using backticks/backquotes, I've noted that filenames that contain spaces will be split, and therfore not found.

Is it possible to avoid this behaviour? The command I issued looks like this

wc `find . -name '*.txt'` 

but for example when there is a file named a b c.txt in directory x it reports

$ wc `find . -name '*.txt'` wc: ./x/a: No such file or directory wc: b: No such file or directory wc: c.txt: No such file or directory 

When used with multiple files wc will show the output of each file, and a final summary line with the totals of all files. that's why I want to execute wc once.

I tried escaping spaces with sed, but wc produces the same output (splits filenames with spaces).

wc `find . -name '*.txt' | sed 's/ /\\\ /pg'` 

2 Answers 2

1

Use the -print0 option to find and the corresponding -0 option to xargs:

find . -name '*.txt' -print0 | xargs -0 wc 

You can also use the -exec option to find:

find . -name '*.txt' -exec wc {} + 
Sign up to request clarification or add additional context in comments.

1 Comment

That's the exact opposite of what xargs normally does. It was created to batch the calls up into optimal groups.
0

from this very similar question (should I flag my question as a duplicate?) I found another answer to this using bash's ** expansion:

wc **/*.txt 

for this to work I had to

shopt -s globstar 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.