How to open a nautilus directory and select a file in it from the command line?

I know how to open directory, but I do not know how to select a file from the command line.

Edit:

I do not know how it works, but when I move any file to a directory and then open the directory that this file is selected.


Solution 1:

I think it's actually easier than it seems. What I tried is just this:

nautilus <path_to_file>

At first, the file doesn't look to be selected, but that's just because the nautilus window isn't active. If you click on it, you'll see that the file name is selected.

Since I guess you want to have that extra visual feedback, I used xdotool to make a simple test to select /var/log/dpkg.log as follows:

nautilus /var/log/dpkg.log && sleep 3 && xdotool search -name log windowactivate

What you should see is that nautilus in opened with using /var/log/ directory and that after three seconds the window is activated (the window name is just log) and the file name is highlighted since it's selected.

Note: in this example if sleep is set to a shorter time you might not get the expected result since you need the window to be properly displayed before trying to activate it.

Solution 2:

I don't think there's an option to do this. See:

  • man nautilus
  • nautilus --help
  • http://live.gnome.org/Nautilus

You can open a file as if you had double clicked it in nautilus using the xdg-open command:

xdg-open file

If you really wanted to have this specific feature, it would require hacking on the Nautilus code.