Three Quick Ways to Run All Scripts in a Directory on Linux
Learn how to efficiently execute every script in a folder using three distinct Linux techniques—run-parts with regex filtering, the versatile find command with -exec, and a simple for loop—while avoiding common pitfalls and ensuring safe execution.
1. run-parts
The run-parts command can execute all executable files in a directory that match specific naming rules (letters, numbers, underscores, hyphens). Use the --regex option to fine‑tune which scripts are run. $ run-parts --regex 'sh$' . This runs every file ending with .sh. To list the scripts without executing them, add --list: $ run-parts --list --regex '^s.*sh$' . Example output (images illustrate the result):
2. find
The find utility is more widely known and can locate scripts based on name patterns and execute them with -exec. A basic example that runs all executable files starting with s in ~/scripts:
$ find ~/scripts -maxdepth 1 -type f -executable -name 's*' -exec {} \;Removing the -maxdepth 1 restriction lets the search recurse into sub‑directories:
$ find -maxdepth 1 -type f -executable -name '*.sh' -exec {} \;3. for loop
For users comfortable with shell scripting, a for loop provides a flexible way to run selected scripts. The following loop executes every script in ~/scripts that starts with s:
$ for f in ~/scripts/s* ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; doneTo run all .sh files regardless of name, adjust the glob pattern:
$ for f in ~/scripts/*.sh ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; doneThese three methods cover most scenarios for batch‑executing scripts, allowing you to choose the most convenient tool for your workflow while providing options to preview actions before they run.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
