Operations 5 min read

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.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Three Quick Ways to Run All Scripts in a Directory on Linux

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" ; done

To run all .sh files regardless of name, adjust the glob pattern:

$ for f in ~/scripts/*.sh ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; done

These 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BashShell scriptingfind commandrun-parts
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.