Panoid
Panoid

Reputation: 57

Feeding all files matching a pattern into stdin of a script

I have a folder with 10000 txt files, and a shell script that, if used on a single file, works in the following way:

cat some_file.txt | ./myscript.sh

However, I am trying to create a loop that passes the script to every file in that folder. Here is what I've tried this far--and dosent work:

for f in /Users/me/desktop/folder*.txt; do ./myscript.sh “$f”; done

Any ideas how to add the "cat" command in my script?

Upvotes: 0

Views: 45

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295472

If one instance of myscript.sh will suffice for the content of all the files:

find /Users/me/desktop/ -maxdepth 1 -type f -name 'folder*.txt' \
  -exec cat {} + | ./myscript.sh

...this will have find invoke a single cat instance for each batch of files up to the operating system's limit on command-line length, and have the results of all of those cat instances fed in a single stream into one instance of myscript.sh.


Alternately, if you want a different instance of your script for each file:

for f in /Users/me/desktop/folder*.txt; do ./myscript.sh <"$f"; done

Upvotes: 2

Related Questions