
Published on by

Published on by
Published on by
bash: Open matching files in a specific application
Published on by
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.
openallin APPLICATION 'INCLUDE FILE PATTERN' [MAX DEPTH] ['EXCLUDE FILE PATTERN']
The function accepts four arguments:
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.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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
openallin() {
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: openallin APPLICATION 'INCLUDE FILE PATTERN' [MAX DEPTH] ['EXCLUDE FILE PATTERN']"
else
if [ -z "$3" ]; then
maxdepth=1
else
maxdepth="$3"
fi
if [ -z "$4" ]; then
find . -type f -maxdepth "$maxdepth" -iname "$2" -exec open -a "$1" "{}" \;
else
find . -type f -maxdepth "$maxdepth" -iname "$2" ! -iname "$4" -exec open -a "$1" "{}" \;
fi
fi
}
macOS Terminal trick: Open current directory in Finder
Published on by
A cool Terminal trick from Brett Terpstra: Add the line below to your .bash_profile
file. Then when you type f
in Terminal, and it will open the current directory in Finder.
f() { open -a "Finder" "${1-.}"; }
You can also pass a directory name to the command, and it will open that directory.
Published on by
Before Bunch had the Quit Apps in… option, I created a bunch called “Quitting Time” to close all of the apps that I usually have open during the work day. I still get some satisfaction from clicking that one, especially on Fridays, so I kept it around even after Quit Apps in… was added.