0

I need to run the following command with a for loop

cat Locate\ Message.eml | ./locatePipe.php

I am trying the following however it seems to break on the first space in the file name

for i in $(find -name "*.eml"); do cat $i | ./locatePipe.php; done

Some of the file names contain "@", "()", "-", ".", "[]", " ' " if that matters

jww
  • 97,681
  • 90
  • 411
  • 885
Taylor Reed
  • 335
  • 2
  • 6
  • 18
  • [Allowing punctuation characters in directory and file names in bash](https://stackoverflow.com/q/17143729/608639), [for name in `ls` and filenames with spaces](https://stackoverflow.com/q/8645546/608639), [for loop through files with spaces and some special characters](https://stackoverflow.com/q/33172934/608639), [Deleting filenames that have space and special characters](https://stackoverflow.com/q/50618130/608639), [How do I enter a file or directory name containing spaces or special characters in the terminal?](https://askubuntu.com/q/984801), etc. – jww Jun 06 '18 at 06:04

3 Answers3

1

Use the -exec option

find -name '*.eml' -exec cat {} + | ./locatePipe.php
choroba
  • 231,213
  • 25
  • 204
  • 289
  • I need to use a for loop because it's too many to process at once. I end up with a memory issue this method – Taylor Reed Oct 16 '15 at 14:52
  • Then use `find -name '*.eml' -exec bash -c 'cat {} | ./locatePipe.php' \;` – choroba Oct 16 '15 at 14:56
  • `-exec cat {} +` will pass as many files as possible to each invocation of `cat`; you should have substantially fewer processes than if you use a for loop, which will start one `cat` per file. – chepner Oct 16 '15 at 15:02
1

I accomplished this using the following command

find -name '*.eml' | while read email; do cat "$email" | ./locatePipe.php; done
Taylor Reed
  • 335
  • 2
  • 6
  • 18
0

If you're using bash 4, just skip using find:

shopt -s globstar
for i in **/*.eml; do
    cat "$i"
done | ./locatePipe.php
chepner
  • 497,756
  • 71
  • 530
  • 681