Find all shell scripts from a folder and execute them in order











up vote
-1
down vote

favorite












I have a folder with a lot of shell scripts to configure my server.



My main script is named "setup.sh" and all others scripts are named 0.script.sh, 1.script.sh.... More generally N.SCRIPT_NAME.sh, where N is the execution number.



So, I would like to list all shell scripts from the folder, and execute them in the order specified by the N numbers.










share|improve this question









New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
    – Isaac
    Nov 16 at 18:52










  • There are no leading zero, and N can have more than one digit.
    – graille
    Nov 16 at 18:54












  • Read man run-,parts.
    – waltinator
    Nov 16 at 19:44










  • @don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
    – Isaac
    Nov 16 at 20:31






  • 2




    @Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
    – don_crissti
    Nov 16 at 20:40

















up vote
-1
down vote

favorite












I have a folder with a lot of shell scripts to configure my server.



My main script is named "setup.sh" and all others scripts are named 0.script.sh, 1.script.sh.... More generally N.SCRIPT_NAME.sh, where N is the execution number.



So, I would like to list all shell scripts from the folder, and execute them in the order specified by the N numbers.










share|improve this question









New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
    – Isaac
    Nov 16 at 18:52










  • There are no leading zero, and N can have more than one digit.
    – graille
    Nov 16 at 18:54












  • Read man run-,parts.
    – waltinator
    Nov 16 at 19:44










  • @don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
    – Isaac
    Nov 16 at 20:31






  • 2




    @Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
    – don_crissti
    Nov 16 at 20:40















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a folder with a lot of shell scripts to configure my server.



My main script is named "setup.sh" and all others scripts are named 0.script.sh, 1.script.sh.... More generally N.SCRIPT_NAME.sh, where N is the execution number.



So, I would like to list all shell scripts from the folder, and execute them in the order specified by the N numbers.










share|improve this question









New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a folder with a lot of shell scripts to configure my server.



My main script is named "setup.sh" and all others scripts are named 0.script.sh, 1.script.sh.... More generally N.SCRIPT_NAME.sh, where N is the execution number.



So, I would like to list all shell scripts from the folder, and execute them in the order specified by the N numbers.







bash shell files wildcards






share|improve this question









New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 16 at 21:02









Isaac

9,68211444




9,68211444






New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 16 at 18:44









graille

31




31




New contributor




graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






graille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
    – Isaac
    Nov 16 at 18:52










  • There are no leading zero, and N can have more than one digit.
    – graille
    Nov 16 at 18:54












  • Read man run-,parts.
    – waltinator
    Nov 16 at 19:44










  • @don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
    – Isaac
    Nov 16 at 20:31






  • 2




    @Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
    – don_crissti
    Nov 16 at 20:40




















  • A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
    – Isaac
    Nov 16 at 18:52










  • There are no leading zero, and N can have more than one digit.
    – graille
    Nov 16 at 18:54












  • Read man run-,parts.
    – waltinator
    Nov 16 at 19:44










  • @don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
    – Isaac
    Nov 16 at 20:31






  • 2




    @Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
    – don_crissti
    Nov 16 at 20:40


















A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
– Isaac
Nov 16 at 18:52




A simple for s in *.script.sh; do printf '%sn' "$s"; done should list the scripts in lexicographical order. Is that the order you need? If that is not what you need, the questions multiply: Are the numbers N only one digit? Do the numbers have leading zero(s)? etc … etc …
– Isaac
Nov 16 at 18:52












There are no leading zero, and N can have more than one digit.
– graille
Nov 16 at 18:54






There are no leading zero, and N can have more than one digit.
– graille
Nov 16 at 18:54














Read man run-,parts.
– waltinator
Nov 16 at 19:44




Read man run-,parts.
– waltinator
Nov 16 at 19:44












@don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
– Isaac
Nov 16 at 20:31




@don_crissti Based on which reasoning you have decided to remove the OP tag of bash? Could (s)he not choose which shell to use/ask about?
– Isaac
Nov 16 at 20:31




2




2




@Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
– don_crissti
Nov 16 at 20:40






@Isaac - based on the fact that almost all newcomers tag their questions linux & bash regardless of the content and there's nothing bash-specific here (and it's already tagged shell). You can always add it back if you think this is a bash question.
– don_crissti
Nov 16 at 20:40












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










There's probably an easier way to glob filenames in sequence with zsh, but here's a brute-force way in bash:



for script in ?.script*.sh ??.script*.sh ???.script*.sh ????.script*.sh
do
printf "Executing script: %sn" "$script"
./"$script"
done


This assumes that you have no more than 9999 scripts -- or, more precisely, none with more than 4 numbers (characters) in the prefix part of the filename.



It also assumes that the prefixes are numbered -- it will pick up scripts named, e.g. go.script_X.sh, if you had one. To work around that, replace the ? with [0-9]'s:



for f in [0-9].script*.sh [0-9][0-9].script*.sh ...


The shell's natural globbing mechanism will be to sort the resulting filenames alphabetically, which results in them sorting numerically as well. The various filenames are spelled out in increasing scale so that "9" sorts before "101" & etc.



In zsh, glob the filenames in numerical order of their prefix with:



for script in *.script*.sh(oe_'REPLY=${REPLY%%.*}'_n)
do
printf "Executing script: %sn" "$script"
./"$script"
done


The globbing expansion first picks up all of the *.script*.sh files, then says to order (sort) them using the expression that extracts the prefix by stripping as much of "dot followed by anything" as possible, and using a numeric sort.






share|improve this answer























  • Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
    – don_crissti
    Nov 16 at 19:31










  • Thanks for the edit, don; I'm still experimenting with zsh.
    – Jeff Schaller
    Nov 16 at 19:32










  • No problem. You could also use a function as shown here
    – don_crissti
    Nov 16 at 19:35










  • Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
    – Jeff Schaller
    Nov 16 at 19:38






  • 1




    Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
    – Isaac
    Nov 16 at 19:53


















up vote
2
down vote













If the files names are simple (no spaces, no newlines, no characters that ls needs to encode with a ?) then, maybe !! , you can get away with:



IFS=$' tn'; for s in $(ls -v folder/*.script.sh); do printf '%sn' "$s"; done


In this case, ls will sort by version number -v (quite similar to numeric value).



But in general, it is a bad idea to parse (process) the output of ls.



This is better:



shopt -s nullglob
for s in folder/?{,?{,?{,?}}}.script.sh
do printf '%sn' "$s"
done


Which will list (in alphabetical order, which is equivalent to numerical order if the characters before .script.sh are only numbers) by each character up to four characters (numbers).



The idea is that brace expansions happen before pathname expansion. The brace expansion will convert and expand the ?{,?{,?{,?}}}.script.sh to



?.script.sh ??.script.sh ???.script.sh ????.script.sh


And then, the pathname expansion will find all file that match each pattern sorting each pattern in turn.



If none of the options above are a solution, you will need to use sort:



printf '%sn' *.script.sh | sort -n | xargs echo


Which becomes even more complex if there is a folder prefix:



printf '%sn' folder/*.script.sh | sort -n -k 1.8 | xargs echo


Where the 8 is the number of characters (bytes) in the word folder and the following / plus one (1).



You will need to replace the echo with sh to execute each script.
This will work correctly if no file name contains newlines.



If it is possible that filenames may contain newlines, use this:



printf '%s' folder/*.script.sh | sort -zn -k 1.8 | xargs -0 -I{} echo '{}'


Again, replace the echo with a sh (or bash) to execute the scripts.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    graille is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482215%2ffind-all-shell-scripts-from-a-folder-and-execute-them-in-order%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    There's probably an easier way to glob filenames in sequence with zsh, but here's a brute-force way in bash:



    for script in ?.script*.sh ??.script*.sh ???.script*.sh ????.script*.sh
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    This assumes that you have no more than 9999 scripts -- or, more precisely, none with more than 4 numbers (characters) in the prefix part of the filename.



    It also assumes that the prefixes are numbered -- it will pick up scripts named, e.g. go.script_X.sh, if you had one. To work around that, replace the ? with [0-9]'s:



    for f in [0-9].script*.sh [0-9][0-9].script*.sh ...


    The shell's natural globbing mechanism will be to sort the resulting filenames alphabetically, which results in them sorting numerically as well. The various filenames are spelled out in increasing scale so that "9" sorts before "101" & etc.



    In zsh, glob the filenames in numerical order of their prefix with:



    for script in *.script*.sh(oe_'REPLY=${REPLY%%.*}'_n)
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    The globbing expansion first picks up all of the *.script*.sh files, then says to order (sort) them using the expression that extracts the prefix by stripping as much of "dot followed by anything" as possible, and using a numeric sort.






    share|improve this answer























    • Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
      – don_crissti
      Nov 16 at 19:31










    • Thanks for the edit, don; I'm still experimenting with zsh.
      – Jeff Schaller
      Nov 16 at 19:32










    • No problem. You could also use a function as shown here
      – don_crissti
      Nov 16 at 19:35










    • Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
      – Jeff Schaller
      Nov 16 at 19:38






    • 1




      Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
      – Isaac
      Nov 16 at 19:53















    up vote
    2
    down vote



    accepted










    There's probably an easier way to glob filenames in sequence with zsh, but here's a brute-force way in bash:



    for script in ?.script*.sh ??.script*.sh ???.script*.sh ????.script*.sh
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    This assumes that you have no more than 9999 scripts -- or, more precisely, none with more than 4 numbers (characters) in the prefix part of the filename.



    It also assumes that the prefixes are numbered -- it will pick up scripts named, e.g. go.script_X.sh, if you had one. To work around that, replace the ? with [0-9]'s:



    for f in [0-9].script*.sh [0-9][0-9].script*.sh ...


    The shell's natural globbing mechanism will be to sort the resulting filenames alphabetically, which results in them sorting numerically as well. The various filenames are spelled out in increasing scale so that "9" sorts before "101" & etc.



    In zsh, glob the filenames in numerical order of their prefix with:



    for script in *.script*.sh(oe_'REPLY=${REPLY%%.*}'_n)
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    The globbing expansion first picks up all of the *.script*.sh files, then says to order (sort) them using the expression that extracts the prefix by stripping as much of "dot followed by anything" as possible, and using a numeric sort.






    share|improve this answer























    • Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
      – don_crissti
      Nov 16 at 19:31










    • Thanks for the edit, don; I'm still experimenting with zsh.
      – Jeff Schaller
      Nov 16 at 19:32










    • No problem. You could also use a function as shown here
      – don_crissti
      Nov 16 at 19:35










    • Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
      – Jeff Schaller
      Nov 16 at 19:38






    • 1




      Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
      – Isaac
      Nov 16 at 19:53













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    There's probably an easier way to glob filenames in sequence with zsh, but here's a brute-force way in bash:



    for script in ?.script*.sh ??.script*.sh ???.script*.sh ????.script*.sh
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    This assumes that you have no more than 9999 scripts -- or, more precisely, none with more than 4 numbers (characters) in the prefix part of the filename.



    It also assumes that the prefixes are numbered -- it will pick up scripts named, e.g. go.script_X.sh, if you had one. To work around that, replace the ? with [0-9]'s:



    for f in [0-9].script*.sh [0-9][0-9].script*.sh ...


    The shell's natural globbing mechanism will be to sort the resulting filenames alphabetically, which results in them sorting numerically as well. The various filenames are spelled out in increasing scale so that "9" sorts before "101" & etc.



    In zsh, glob the filenames in numerical order of their prefix with:



    for script in *.script*.sh(oe_'REPLY=${REPLY%%.*}'_n)
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    The globbing expansion first picks up all of the *.script*.sh files, then says to order (sort) them using the expression that extracts the prefix by stripping as much of "dot followed by anything" as possible, and using a numeric sort.






    share|improve this answer














    There's probably an easier way to glob filenames in sequence with zsh, but here's a brute-force way in bash:



    for script in ?.script*.sh ??.script*.sh ???.script*.sh ????.script*.sh
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    This assumes that you have no more than 9999 scripts -- or, more precisely, none with more than 4 numbers (characters) in the prefix part of the filename.



    It also assumes that the prefixes are numbered -- it will pick up scripts named, e.g. go.script_X.sh, if you had one. To work around that, replace the ? with [0-9]'s:



    for f in [0-9].script*.sh [0-9][0-9].script*.sh ...


    The shell's natural globbing mechanism will be to sort the resulting filenames alphabetically, which results in them sorting numerically as well. The various filenames are spelled out in increasing scale so that "9" sorts before "101" & etc.



    In zsh, glob the filenames in numerical order of their prefix with:



    for script in *.script*.sh(oe_'REPLY=${REPLY%%.*}'_n)
    do
    printf "Executing script: %sn" "$script"
    ./"$script"
    done


    The globbing expansion first picks up all of the *.script*.sh files, then says to order (sort) them using the expression that extracts the prefix by stripping as much of "dot followed by anything" as possible, and using a numeric sort.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 at 19:30









    don_crissti

    48.7k15129157




    48.7k15129157










    answered Nov 16 at 18:55









    Jeff Schaller

    36.3k952119




    36.3k952119












    • Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
      – don_crissti
      Nov 16 at 19:31










    • Thanks for the edit, don; I'm still experimenting with zsh.
      – Jeff Schaller
      Nov 16 at 19:32










    • No problem. You could also use a function as shown here
      – don_crissti
      Nov 16 at 19:35










    • Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
      – Jeff Schaller
      Nov 16 at 19:38






    • 1




      Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
      – Isaac
      Nov 16 at 19:53


















    • Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
      – don_crissti
      Nov 16 at 19:31










    • Thanks for the edit, don; I'm still experimenting with zsh.
      – Jeff Schaller
      Nov 16 at 19:32










    • No problem. You could also use a function as shown here
      – don_crissti
      Nov 16 at 19:35










    • Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
      – Jeff Schaller
      Nov 16 at 19:38






    • 1




      Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
      – Isaac
      Nov 16 at 19:53
















    Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
    – don_crissti
    Nov 16 at 19:31




    Ideally OP would zero-pad the file names, then using a simple glob like ./* should work with any shell...
    – don_crissti
    Nov 16 at 19:31












    Thanks for the edit, don; I'm still experimenting with zsh.
    – Jeff Schaller
    Nov 16 at 19:32




    Thanks for the edit, don; I'm still experimenting with zsh.
    – Jeff Schaller
    Nov 16 at 19:32












    No problem. You could also use a function as shown here
    – don_crissti
    Nov 16 at 19:35




    No problem. You could also use a function as shown here
    – don_crissti
    Nov 16 at 19:35












    Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
    – Jeff Schaller
    Nov 16 at 19:38




    Ahhh; I had searched here for zsh filename sorting and landed on one of Gilles' answers and ran with it. Credit to Gilles, blame to me!
    – Jeff Schaller
    Nov 16 at 19:38




    1




    1




    Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
    – Isaac
    Nov 16 at 19:53




    Why the complexity in zsh? This zsh -c 'for script in folder/*.script*.sh(n);do printf "Executing script: %sn" "$script";done' seems to work correctly. Reference
    – Isaac
    Nov 16 at 19:53












    up vote
    2
    down vote













    If the files names are simple (no spaces, no newlines, no characters that ls needs to encode with a ?) then, maybe !! , you can get away with:



    IFS=$' tn'; for s in $(ls -v folder/*.script.sh); do printf '%sn' "$s"; done


    In this case, ls will sort by version number -v (quite similar to numeric value).



    But in general, it is a bad idea to parse (process) the output of ls.



    This is better:



    shopt -s nullglob
    for s in folder/?{,?{,?{,?}}}.script.sh
    do printf '%sn' "$s"
    done


    Which will list (in alphabetical order, which is equivalent to numerical order if the characters before .script.sh are only numbers) by each character up to four characters (numbers).



    The idea is that brace expansions happen before pathname expansion. The brace expansion will convert and expand the ?{,?{,?{,?}}}.script.sh to



    ?.script.sh ??.script.sh ???.script.sh ????.script.sh


    And then, the pathname expansion will find all file that match each pattern sorting each pattern in turn.



    If none of the options above are a solution, you will need to use sort:



    printf '%sn' *.script.sh | sort -n | xargs echo


    Which becomes even more complex if there is a folder prefix:



    printf '%sn' folder/*.script.sh | sort -n -k 1.8 | xargs echo


    Where the 8 is the number of characters (bytes) in the word folder and the following / plus one (1).



    You will need to replace the echo with sh to execute each script.
    This will work correctly if no file name contains newlines.



    If it is possible that filenames may contain newlines, use this:



    printf '%s' folder/*.script.sh | sort -zn -k 1.8 | xargs -0 -I{} echo '{}'


    Again, replace the echo with a sh (or bash) to execute the scripts.






    share|improve this answer



























      up vote
      2
      down vote













      If the files names are simple (no spaces, no newlines, no characters that ls needs to encode with a ?) then, maybe !! , you can get away with:



      IFS=$' tn'; for s in $(ls -v folder/*.script.sh); do printf '%sn' "$s"; done


      In this case, ls will sort by version number -v (quite similar to numeric value).



      But in general, it is a bad idea to parse (process) the output of ls.



      This is better:



      shopt -s nullglob
      for s in folder/?{,?{,?{,?}}}.script.sh
      do printf '%sn' "$s"
      done


      Which will list (in alphabetical order, which is equivalent to numerical order if the characters before .script.sh are only numbers) by each character up to four characters (numbers).



      The idea is that brace expansions happen before pathname expansion. The brace expansion will convert and expand the ?{,?{,?{,?}}}.script.sh to



      ?.script.sh ??.script.sh ???.script.sh ????.script.sh


      And then, the pathname expansion will find all file that match each pattern sorting each pattern in turn.



      If none of the options above are a solution, you will need to use sort:



      printf '%sn' *.script.sh | sort -n | xargs echo


      Which becomes even more complex if there is a folder prefix:



      printf '%sn' folder/*.script.sh | sort -n -k 1.8 | xargs echo


      Where the 8 is the number of characters (bytes) in the word folder and the following / plus one (1).



      You will need to replace the echo with sh to execute each script.
      This will work correctly if no file name contains newlines.



      If it is possible that filenames may contain newlines, use this:



      printf '%s' folder/*.script.sh | sort -zn -k 1.8 | xargs -0 -I{} echo '{}'


      Again, replace the echo with a sh (or bash) to execute the scripts.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        If the files names are simple (no spaces, no newlines, no characters that ls needs to encode with a ?) then, maybe !! , you can get away with:



        IFS=$' tn'; for s in $(ls -v folder/*.script.sh); do printf '%sn' "$s"; done


        In this case, ls will sort by version number -v (quite similar to numeric value).



        But in general, it is a bad idea to parse (process) the output of ls.



        This is better:



        shopt -s nullglob
        for s in folder/?{,?{,?{,?}}}.script.sh
        do printf '%sn' "$s"
        done


        Which will list (in alphabetical order, which is equivalent to numerical order if the characters before .script.sh are only numbers) by each character up to four characters (numbers).



        The idea is that brace expansions happen before pathname expansion. The brace expansion will convert and expand the ?{,?{,?{,?}}}.script.sh to



        ?.script.sh ??.script.sh ???.script.sh ????.script.sh


        And then, the pathname expansion will find all file that match each pattern sorting each pattern in turn.



        If none of the options above are a solution, you will need to use sort:



        printf '%sn' *.script.sh | sort -n | xargs echo


        Which becomes even more complex if there is a folder prefix:



        printf '%sn' folder/*.script.sh | sort -n -k 1.8 | xargs echo


        Where the 8 is the number of characters (bytes) in the word folder and the following / plus one (1).



        You will need to replace the echo with sh to execute each script.
        This will work correctly if no file name contains newlines.



        If it is possible that filenames may contain newlines, use this:



        printf '%s' folder/*.script.sh | sort -zn -k 1.8 | xargs -0 -I{} echo '{}'


        Again, replace the echo with a sh (or bash) to execute the scripts.






        share|improve this answer














        If the files names are simple (no spaces, no newlines, no characters that ls needs to encode with a ?) then, maybe !! , you can get away with:



        IFS=$' tn'; for s in $(ls -v folder/*.script.sh); do printf '%sn' "$s"; done


        In this case, ls will sort by version number -v (quite similar to numeric value).



        But in general, it is a bad idea to parse (process) the output of ls.



        This is better:



        shopt -s nullglob
        for s in folder/?{,?{,?{,?}}}.script.sh
        do printf '%sn' "$s"
        done


        Which will list (in alphabetical order, which is equivalent to numerical order if the characters before .script.sh are only numbers) by each character up to four characters (numbers).



        The idea is that brace expansions happen before pathname expansion. The brace expansion will convert and expand the ?{,?{,?{,?}}}.script.sh to



        ?.script.sh ??.script.sh ???.script.sh ????.script.sh


        And then, the pathname expansion will find all file that match each pattern sorting each pattern in turn.



        If none of the options above are a solution, you will need to use sort:



        printf '%sn' *.script.sh | sort -n | xargs echo


        Which becomes even more complex if there is a folder prefix:



        printf '%sn' folder/*.script.sh | sort -n -k 1.8 | xargs echo


        Where the 8 is the number of characters (bytes) in the word folder and the following / plus one (1).



        You will need to replace the echo with sh to execute each script.
        This will work correctly if no file name contains newlines.



        If it is possible that filenames may contain newlines, use this:



        printf '%s' folder/*.script.sh | sort -zn -k 1.8 | xargs -0 -I{} echo '{}'


        Again, replace the echo with a sh (or bash) to execute the scripts.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 at 19:39

























        answered Nov 16 at 19:23









        Isaac

        9,68211444




        9,68211444






















            graille is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            graille is a new contributor. Be nice, and check out our Code of Conduct.













            graille is a new contributor. Be nice, and check out our Code of Conduct.












            graille is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482215%2ffind-all-shell-scripts-from-a-folder-and-execute-them-in-order%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre