Home FAQ How To Run All Scripts In A Directory In Linux
How To Run All Scripts In A Directory In Linux

How To Run All Scripts In A Directory In Linux

By sk
821 Views

The other day I have been testing some basic shell scripts that I created for fun. Since I am new to shell scripting, I ran and test the scripts one by one. It was OK if there are only a few scripts. As the number of scripts grew, I find it bit time consuming to test all of them. After a bit of Google search, I find out a couple methods to run all scripts in a directory in Linux, one after another.

Run All Scripts In A Directory In Linux

I have a couple scripts in my Documents directory.

$ ls Documents/
linux.sh ostechnix.sh script1.sh script2.sh script3.sh script4.sh

All of them are simple one-liner shell scripts that displays the given string(s) using "echo" command in the standard output.

Before I knew we can run all scripts in a folder, I would usually make the script executable like:

$ chmod +x script.sh

Then I run it using command:

$ ./script.sh

Or,

$ sh script.sh

Again, I make the second script executable and run it and so on.

Well, you don't need to do like this. We can run all scripts in a directory or path using "run-parts" command. The run-parts command is used to run scripts or programs in a directory or path.

$ run-parts <directory-path>

One disadvantage with run-parts command is it won't execute all scripts. It will work only if your scripts have the correct names. The names must consist entirely of ASCII upper-case and lower-case letters, ASCII digits, ASCII underscores, and ASCII minus-hyphens.

To resolve this naming problem, we can use the regex option. For example, the following command will find and run all script files in the ~/Documents directory that starts with letter 's' and end with 'sh' extension:

$ run-parts --regex '^s.*sh$' ~/Documents

Run all scripts in a directory in Linux with run-parts command


Troubleshooting:

If you encountered with an error something like below;

run-parts: failed to exec Documents/script1.sh: Exec format error

Refer the following guide to fix it.


Similarly, to run all script files that ends with .sh in Documents directory, run:

$ run-parts --regex '.*sh$' ~/Documents

To print the names of all files in the Documents directory that start with letter 's' and end with 'sh' extension, run:

$ run-parts --list --regex '^s.*sh$' Documents
Documents/script1.sh
Documents/script2.sh
Documents/script3.sh
Documents/script4.sh

This command will only print the name the script files, but won't execute them.

To list all files end with .sh in Documents directory, run:

$ run-parts --list --regex '.*sh$' Documents
Documents/linux.sh
Documents/ostechnix.sh
Documents/script1.sh
Documents/script2.sh
Documents/script3.sh
Documents/script4.sh

The run-parts command is primarily used by cron jobs and is not reliable for manual usage.

Another way to avoid dealing with renaming your scripts to fit run-parts's naming structure, you can use find or for loop commands.

To find and run all script files that start with letter "s" in ~/Documents folder, run:

$ find ~/Documents -maxdepth 1 -type f -executable -name 's*' -exec {} \;

If you want to run all scripts in the given directory and all its sub-directories, remove the maxdepth option in the above command:

$ find ~/Documents -type f -executable -name 's*' -exec {} \;

To run all scripts that ends with extension ".sh", run:

$ find ~/Documents -maxdepth 1 -type f -executable -name '*.sh' -exec {} \;

Find and run all scripts in a directory in Linux using find command

This method has also a minor disadvantage. This command can only run the scripts that can be interpreted by your current shell. If you want to run all type of scripts, for example awk, perl, python, shell scripts, use for loop method like below.

$ for f in ~/Documents/s* ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; done

The above command will run the scripts that starts with letter "s" in ~/Documents folder. If you want to run all files that ends with ".sh" extension, do:

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

For more details, refer man pages.

$ man run-parts
$ man find

Hope this helps.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

1 comment

Jalal April 5, 2020 - 10:10 am

Hi,
very useful article
man run-parts is not working under centos7
and sent error message
man: can’t open /usr/share/man/man/man4/crontabs.4: No such file or directory

and i changed the content of file /usr/share/man/man4/run-parts.4.gz
from: .so man/man4/crontabs.4
to: .so man4/crontabs.4
thanks again

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More