Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 2
    Of course one has to wonder what directory names the OP had to warrant running basename before counting the results. If there are no embedded newlines then removing the -exec basename {} \; will give the same results but without the cost of running a process per matching file. If the OP is using gnu find - likely given the linux tag, then nbs_files=$(ssh -q -i ${sshkey} ${user}@${server} "find ${path}/ -maxdepth 1 -mindepth 1 -type f '(' -name '*sv*' -o -name '*.json' ')' -printf '%f\n' | wc -l") will avoid the process per matching file even with embedded newlines in the directory names. Commented Jul 5, 2020 at 7:02
  • @icarus, though filenames with newlines will affect the count. You don't really need to print the actual filenames if all you're going to do is pipe to wc. Just something like find ... -printf '\n' | wc -l would do Commented Jul 5, 2020 at 15:14
  • @ilkkachu The point is that to reproduce the behavior you do need to print the filenames, you can't just print a newline for each file. Note I am not saying that the behavior is the desired one, I strongly suspect it is not, but your solution answers a different problem. Commented Jul 5, 2020 at 18:33
  • @icarus, well, they did say "retrieve the number of files", so I thought they wouldn't want names with newlines to count as two. Or was there something else I missed? Commented Jul 5, 2020 at 18:37