Like in title. On Windows, I can do this:

explorer /select,"C:\folder\file.txt"

which will result in opening of explorer.exe, that will immediately open C:\folder and select file.txt.

I believe ROX had this functionality too.

Can I do the same with thunar?


With a little digging, I discovered this is possible using D-Bus:

#!/usr/bin/env python
import dbus
import os
import sys
import urlparse
import urllib

bus = dbus.SessionBus()
obj = bus.get_object('org.xfce.Thunar', '/org/xfce/FileManager')
iface = dbus.Interface(obj, 'org.xfce.FileManager')

_thunar_display_folder = iface.get_dbus_method('DisplayFolder')
_thunar_display_folder_and_select = iface.get_dbus_method('DisplayFolderAndSelect')

def display_folder(uri, display='', startup_id=''):
    _thunar_display_folder(uri, display, startup_id)

def display_folder_and_select(uri, filename, display='', startup_id=''):
    _thunar_display_folder_and_select(uri, filename, display, startup_id)

def path_to_url(path):
    return urlparse.urljoin('file:', urllib.pathname2url(path))

def url_to_path(url):
    return urlparse.urlparse(url).path

def main(args):
    path = args[1]  # May be a path (from cmdline) or a file:// URL (from OS)
    path = url_to_path(path)
    path = os.path.realpath(path)
    url = path_to_url(path)

    if os.path.isfile(path):
        dirname = os.path.dirname(url)
        filename = os.path.basename(url)
        display_folder_and_select(dirname, filename)

if __name__ == '__main__':

Execute with:

$ ./thunar-open-file.py /home/user/myfile.txt

And it will still open a folder, if you pass that:

$ ./thunar-open-file.py /home/user/

screencast for hardcore proof

|improve this answer|||||
  • Unfortunately, some applications (looking at you, Chrome) don't even send the full file path. They actually send the path to the parent folder =/ – theY4Kman Feb 17 '17 at 21:48
  • 1
    In the new Thunar version (shipped with xfce 4.14), you can just send the path to a file and it will select that file on opening. – brunoais Oct 5 '19 at 9:43

Building upon answer from theY4Kman, this is how to do it without Python:

dbus-send --type=method_call --dest=org.xfce.Thunar /org/xfce/FileManager org.xfce.FileManager.DisplayFolderAndSelect string:"/home/user/Downloads" string:"File.txt" string:"" string:""

The only caveat is that you need to separate folder path and file name.

|improve this answer|||||

With thunar's built-in command line switches, you can't. If you see man thunar, you'll figure that you could only open a folder that way, but you won't be able to preselect a file in it.

Does it mean that you can't do it at all?

Fortunately not, but you'll need help from external programs. An example that accomplishes this using xdotool to send ctrl+s and type in filename (which effectively is going to select it):

[ -z "$file" ]; then
    echo 'No file selected' 1>&2
    exit 1

if [[ ! $(command -v thunar) ]]; then
    echo 'Thunar is not installed' 1>&2
    exit 1

if [ -d "$file" ]; then
    thunar "$file" &
    if [ ! -f "$file" ]; then
        echo 'File does not exist' 1>&2
        exit 1

    if [[ ! $(command -v xdotool) ]]; then
        echo 'Xdotool is not installed' 1>&2
        exit 1

    set -e #exit on any error
    thunar "$(dirname "$file")" &
    window_id=`xdotool search --sync --onlyvisible --class 'Thunar' | head -n1`
    xdotool key --clearmodifiers 'ctrl+s'
    xdotool type "$(basename "$file")"
    xdotool key Return

Usage: script /path/to/file-or-folder

There are two caveats:

  1. You'll notice slight lag due to xdotool --sync, but I believe it's acceptable.
  2. This won't work with files hidden in thunar for any reason, such as dotfiles.
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.