How to run all scripts in a directory on Linux

Last updated on June 18, 2020 by Dan Nanni

Question: I have a bunch of scripts in a directory. I want to automatically run all the scripts in the directory, regardless of how many these are.

If you want to run all scripts or executable binaries in a particular directory, you can use a command line utility called run-parts. This tool can automatically discover multiple scripts or programs in a directory, and run them all.

You can use run-parts command in the following format.

$ run-parts [options] <target-directory>

Scripts found in a directory will be run one by one in a lexically sorted order. run-parts will execute all the scripts whose names consist of alphanumeric letters, underscores and hyphens.

For example, to run all scripts in the current directory:

$ run-parts .

Optionally, you can run only those scripts whose names are matched with a regular expression. For that, use --regex option.

For example, to run all scripts in /etc whose names start with a and end with .sh:

$ run-parts --regex '^a.*.sh$' /etc

With --test option, you can print the names of the scripts which would be executed, without actually running them. This is useful for testing purpose.

$ run-parts --test ./my_script_directory

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean