bash: Open matching files in a specific application

I needed to open a lot of JPG files in Pixelmator, so I wrote a bash function called openallin. It searches the current directory for files matching a pattern and opens them in the specified application. To open all JPG files in Pixelmator, the command would be openallin Pixelmator '*.jpg'. (This works on macOS. I don’t know if it’s possible on other Unix-like systems.)

Call the function with up to four arguments as shown below. The first two arguments are required. The third is required only if you want to pass the fourth (see explanation below), and the fourth is always optional.


The function accepts four arguments:

  1. APPLICATION (required): The name of the application you want to open the files in.
  2. INCLUDE FILE PATTERN (required): A pattern that identifies the files you want to open. For example, ‘*.jpg' matches all files with the extension “jpg.” This argument must be quoted so that the shell will interpret any wildcard characters as literal characters and not attempt to expand them.
  3. MAX DEPTH (optional): The function uses the find command, and this argument is passed to find as the -maxdepth option. find will by default search for matching files in the current directory and every directory below the current directory; maxdepth limits the search. If you don’t pass a third argument to openallin, the function uses “1” as the max depth and searches for files in the current directory only. If you want the function to search subdirectories, pass a number as the third argument. For example, “2” tells the function to look in the current directory and directories immediately under the current directory. If you don’t know how deep the search needs to go, just take a guess. The function will search down to the specified max depth or until it hits the last directory, whichever comes first. Just keep in mind that the greater the number, the deeper the search will go, and the more files it will try to open. Passing a really large number as this argument is probably a good way to crash your computer.
  4. EXCLUDE FILE PATTERN (optional): A pattern that identifies files you don’t want to open. This is only needed if you want to open a subset of the files matched by the include file pattern. For example, if I just want to open JPG files that don’t contain “thumbnail” in the file name (this was actually the scenario that prompted me to write the function), the command would be openallin Pixelmator '*.jpg' 1 '*thumbnail*'. Like INCLUDE FILE PATTERN, this argument must be quoted. bash functions only accept positional parameters, and the function expects EXCLUDE FILE PATTERN to be the fourth argument. If you want to pass an exclude file pattern, you must also pass a max depth. (If you don’t know what max depth to use, just pass ‘1'.)

To use the function, add the code below to your .bash_profile file.

openallin() {
    if [ -z "$1" ] || [ -z "$2" ]; then
        if [ -z "$3" ]; then

        if [ -z "$4" ]; then
            find . -type f -maxdepth "$maxdepth" -iname "$2" -exec open -a "$1" "{}" \;
            find . -type f -maxdepth "$maxdepth" -iname "$2" ! -iname "$4" -exec open -a "$1" "{}" \;