Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute all the scripts in the directory in linux at one time

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to execute all the scripts under the directory in linux at one time", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to execute all the scripts under the directory at one time in linux".

1. run-parts

First, I recommend a very useful, but not very familiar command: run-parts. Its basic usage is as follows:

$ run-parts option

However, not all scripts in the directory will be executed, and there will be some naming requirements. Script names are executed only if they meet the following criteria: upper/lower case, number, underscore (_), dash (-).

In order to select scripts that satisfy the criteria, we usually need to add the--regex option. Suppose we have these scripts in our directory:

Since scripts usually end in.sh, if you want to execute all scripts properly, you can do this:

$ run-parts --regex 'sh$' .

Implementation results:

If you only want to execute scripts that start with s and end with sh, you can combine them with the following regular expression:

$ run-parts --regex '^s.* sh$' .

Implementation results:

Of course, you can write regular expressions based on your script.

To prevent mishandling, look at what scripts will be executed before they actually execute. We can add the--list option:

$ run-parts --list --regex '^s.* sh$' .

Implementation results:

As the result shows, with the--list option, it only lists scripts to be executed for your checking purposes, and doesn't actually execute them.

2. find

However, the run-parts command is still relatively small, and it is estimated that many people do not know this command, let alone know how to use it. Another way to execute all the scripts in the directory is to use the find command, which you will be more familiar with.

Specifically, we can use the find command to find all scripts in the target directory, and then use the-exec option to execute the found scripts.

$ find ~/scripts -maxdepth 1 -type f -executable -name 's*' -exec {} \; STAY HOME STAY SAFE

The above command specifies that scripts are only found at the scripts/directory level, but you can also remove this restriction and search all scripts in the current directory and its subdirectories:

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

3. for

If your shell is good, then the for loop is also a good choice. For example, if you want to execute all scripts starting with s in the directory, you can write:

$ for f in ~/scripts/s* ; do [ -x "$f" ] && [ ! -d "$f" ] && "$f" ; done STAY HOME STAY SAFE

If you want to execute all the scripts in the directory, you can make a small change:

$ for f in ~/scripts/*.sh ; do [ -x "$f" ] && [ ! -d "$f" ] &&& "$f" ; done Public Number: Good Linux hello world! STAY HOME STAY SAFE

Of course, if you want to execute other scripts, or have other needs, you can use your imagination to write corresponding commands.

The above is "linux how to directory scripts all at once" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report