Doug Mill
Doug Mill

Reputation: 73

Unix - Piping each file in a directory to a program one at a time

I have a directory of input files, each of which I am running cat on and piping to STDIN of a ruby program file, like so:

cat Laser-Maze-with-Mirrors_testcases/input005.txt | ruby laser-maze.rb

I have a feeling this is simple, but how do I pipe the cat of these files in one at a time? Right now I'm typing each one manually, which seems like a really dumb thing to do.

Upvotes: 0

Views: 100

Answers (3)

hd1
hd1

Reputation: 34667

find /mypath -type f -exec ruby laser-made.rb < {} \; will also work

Upvotes: 1

chthonicdaemon
chthonicdaemon

Reputation: 19770

You can loop over the files. Note that redirection is probably a bit more direct than cat in this case, and that you should always quote the arguments in a case like this in case there happen to be spaces in the files.

for f in Laser-Maze-with-Mirrors_testcases/*.txt; do
    ruby laser-maze.rb < "$f"
done

Upvotes: 2

C.B.
C.B.

Reputation: 8326

Something as simple as

for file in /mypath/*
do
    cat $file | ruby laser-maze.rb
done

should work in a shell script

Upvotes: 1

Related Questions