8

Saying, I have this tree and I want to execute ruby on all scripts within the folder. How could I archive through one line of command.

./
../
rb-script/
    sample1.rb
    sample2.rb

2 Answers 2

10

Something along the lines (depending on your shell) of

for file in rb-script/*.rb; do ruby "$file"; done

should do the trick; alternatively (and shell-independently, I think)

find rb-script -type f -exec ruby "{}" \;

using find; where you can, depending on your situation, be more specific, à la

find rb-script -maxdepth 1 -type f -name '*.rb' -exec ruby "{}" \;

In general, command <glob-pattern> would also work if command accepts several files (and there are less files than the commandline argument maximum of your shell), but I doubt ruby does that. (You could write a wrapper script such that ruby-wrapper rb-script/*.rb would do what you want, though.)

2
  • Thanks. Good tips for using ; to execute multiple commands.
    – steveyang
    Mar 10, 2012 at 11:11
  • The for file in works like a charm! Is there a way to make the loop run on these files sorted by name? May 13, 2020 at 13:47
1

If this will be part of a daemon, you could use the run-parts command to execute everything in the folder in alphabetical order. This command is especially useful if you need to execute it in reverse order for shutdown.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .