Opening Finder from the terminal with the selected file

I want to open the Finder from the terminal with the selected file selected. I know using open . I can open the current directory in Finder, but I also want to select a file in the Finder window.

The main task I want to do is to run a script that randomly selects a file among many in the folder, and for this I need to open a new Finder window with the selected file.

+7
shell terminal macos
source share
1 answer

. . means only the path at the current location (this will be the folder), so open decides that the right application to use is Finder. If you were to make open myTextFile.txt , which is at your current location in the terminal that opens, you decide to use a text editor. However, you can specify the application to open the file with the -a flag, so that your command looks like this: open -a Finder myTextFile.txt .

It is assumed that Faisal will also work, the -R flag is equivalent to using ⌘↩ (Return Command) in Spotlight.

this and some other useful open-shell shell tricks are described in this post: Shell tricks: OS X open command

+2
source share

All Articles