I desire to match all files ending with a certain extension with a shell glob.

In this case I desire to target all files ending with the .sh extension, which are bourne files I execute with the Bash shell after putting a "shebang" (like #!/bin/bash) at their first line.

This is, for example, a cron command I have:

0 0 * * * "$HOME"/public_html/cron_daily/myfile.sh 2>/dev/null

Instead myfile.sh I need to target all files in that dir, ending with a .sh extension.

Is the following code correct?

0 0 * * * "$HOME"/public_html/cron_daily/*$.sh 2>/dev/null


I think this is good when using a glob:

  • I thought “You’d just use *.sh to match all files with a .sh extension.” was clear enough... *.sh is the glob you use to match files ending with .sh. – Stephen Kitt Jul 11 '18 at 21:03

You can't do it that way, since they will be combined into one command line.

for scr in "$HOME"/public_html/cron_daily/*.sh ; do "$scr" 2> /dev/null; done
  • I believe it isn't a great deal but writing file instead scr could be clearer and more accurate as it is a file containing a script. What do you think? – user9303970 Jul 14 '18 at 23:02

You’d just use *.sh to match all files with a .sh extension. (Adding the path as appropriate.)

However this won’t have the result you’re after. Look at run-parts to run multiple scripts from cron:

0 0 * * * /usr/bin/run-parts "$HOME"/public_html/cron_daily/ 2>/dev/null

(This will run all executables in .../public_html/cron_daily, not just .sh files. By the way, are you sure it’s a good idea to keep cron scripts under public_html? Is that directory being served by your web server?)

  • I think only website directories are served from that directory but maybe not. – user9303970 Jul 11 '18 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.