Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Open directory in new tab in Thunar

This is a direct rip-off of [Tutorial] Improving Thunar in XFCE by opening new tabs instead of windows with Bash. - Linux Mint Forums. A very enterprising individual named happysadhu has produced a fantastic script that will make future blog posts easier (ahem).

If you want to open multiple directories in Thunar, in tabs and not separate windows, there's not a programmatic way to do that natively. But if you set up a script named /usr/local/bin/thunartab with the following contents, you can then run the following commands.

# Script to open new file manager tab instead of window (e.g., Thunar) in XFCE.
# Version 1. 2017-04-18
# Lock script to process multiple instances consecutively when "Open All" on Desktop.
if [[ $1 == "file:///home/$USER/Desktop/"* ]]; then
    ME=`basename "$0"`;
    exec 8>$LCK;
    flock -x 8;  # lock $LCK
    trap 'rm -f $LCK' exit # remove $LCK regardless of exit status
    sleep .1 # hack to give time for each script to execute properly--weird!
# Convert desktop file urls to thunar-friendly local paths.
# Accommodates special characters in directory names (!@#$, etc).
filepath="$(urlencode -d ${fileurl#file://})/"
# Check for running instances of $app on current desktop/workspace.
wid=( $(xdotool search --desktop $(xdotool get_desktop) --class $app) )
lastwid=${wid[*]: -1} # Get PID of newest active thunar window.
# If $wid is null launch app with filepath.
if [ -z $wid ]; then
    $app "$filepath"
# If app is already running, activate it and use shortcuts to paste filepath into path bar.
    xdotool windowactivate --sync $lastwid key ctrl+t ctrl+l # Activate pathbar in thunar
    xdotool type -delay 0 "$filepath" # "--delay 0" removes default 12ms between each keystroke
    xdotool key Return
exit 0
# Easy Installation:
# cd ~/Downloads && tar -zxvf thunartab.tar.gz && sudo chmod 755 thunartab && sudo cp thunartab /usr/local/bin/
# Install dependencies: sudo apt install gridsite-clients xdotool # gridsite-clients contains urlencode
# Reboot # if you want to run "thunartab" as a command in terminal
# Select script in the XFCE's GUI "Preferred Applications"
# For alternatives for script location run "echo $PATH" (without quotes) in terminal
# Troubleshooting: If more than one thunar window opens with "Open All", try increasing sleep value on line 12
# For other file-managers (e.g., Nemo, Caja, Nautilus), change app name on line 21
# You may also have to modified  the xdotool shortcuts on line 31.
# For example for Nemo, change line 31 to something like
# xdotool windowactivate --sync $lastwid key ctrl+t ctrl+l ctrl+v Return ctrl+l Escape
# Manually test shortcuts in file manager first.
# Main sources for learning bash for this script include:
# "Bash Scripting Tutorial" (Very good for beginners like myself"
# "Using flock to protect critical sections in shell scripts"
# (the starting idea for this script)
# Bug reports or suggestions, please email me, Sam, at

The commands to open multiple directories would then be:

thunar /path/to/first
thunartab /path/to/second
thunartab /here/is/another