1

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

Update

I think this is good when using a glob:

*.{sh}
Arcticooling
  • 1
  • 12
  • 44
  • 103
  • 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

2 Answers2

4

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

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
1

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
Ignacio Vazquez-Abrams
  • 44,857
  • 7
  • 93
  • 100
  • 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? – Arcticooling Jul 14 '18 at 23:02