4

I have a Java application in which I need to "reveal" a file to the user (i.e. open the containing folder of the file in an explorer and highlight the file). I have configured it to do so in both Windows and Mac OSX, but I have not been able to find a terminal command to do so on Linux/Ubuntu operating systems.

The below batch/terminal commands are the Windows and Mac equivalents of what I am looking for.

Windows:

Explorer /select,filename

Mac:

open -R filename

2 Answers 2

2

The file manager in Ubuntu is called Nautilus, and it seems to do what you are after when you pass a file to it on the command line. For instance, the following command:

nautilus ~/Documents/foo.txt

opens a file manager window showing the ~/Documents folder with foo.txt selected.

3
  • Thanks! I have one question though: when I posted a related question on StackOverflow, I received a response which said "On Linux it would depend on the window manager or file browser they are using." Would that command you posted work on most Ubuntu/Linux systems?
    – FThompson
    May 8, 2012 at 18:57
  • 1
    It should work for people using the Unity or GNOME desktops (Nautilus is the Gnome file manager). You would need something different for Kubuntu or Xubuntu desktops. There are cross-desktop tools like xdg-open that should open the desktop's file manager if you pass them a folder, but this doesn't give you a way to select a file in the folder. If I used xdg-open in the example from my answer, it would instead open a text editor. May 9, 2012 at 0:33
  • 1
    Although xdg-open doesn't reveal the file, I'll use that for Linux users so that functionality is ensured. Thanks again.
    – FThompson
    May 9, 2012 at 2:52
2

Dbus may be an option:

dbus-send --print-reply --dest=org.freedesktop.FileManager1 /org/freedesktop/FileManager1 org.freedesktop.FileManager1.ShowItems array:string:"file:///home/john.doe/foo.txt" string:""

Electron is using that and if it fails it falls back to

xdg-open /home/john.doe/

Which does not highlight the file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .