Anyone have a dmenu file manager script?

ive been looking for dmenu file manager , for some reason dmenufm doesn’t work for me it gives me no program selected falling back to dmenu so ive been googling and found these but the one doesn’t open anything and the other ones uses xdg-open but doesn’t show hidden files if anyone could help combine the two to show hidden files and open them that would be great

#!/bin/sh
# TODO: multisel

target="$1"
[ -z "$target" ] && target="$(realpath .)"
prompt="$2"

while true; do
	p="$prompt"
	[ -z "$p" ] && p="$target"
	sel="$(ls -1a "$target" |grep -v '^\.$' | dmenu -p "$p" -l 25)"
	ec=$?
	[ "$ec" -ne 0 ] && exit $ec

	c="$(echo "$sel" |cut -b1)"
	if [ "$c" = "/" ]; then
		newt="$sel"
	else
		newt="$(realpath "${target}/${sel}")"
	fi

	if [ -e "$newt" ]; then
		target="$newt"
		if [ ! -d "$target" ]; then
			echo "$target"
			exit 0
		fi
	fi
done


#!/bin/sh

handle_file() {
	if [ -d "$1" ]; then
		cd "$1"
	elif [ -f "$1" ]; then
		exec xdg-open "$(pwd)/$1" &
		exit 0
	else
		printf "%s is neither a file nor a directory\n" "$(pwd)/$1" >&2
	fi
}

needed=("xdg-open" "dmenu")

for n in "${needed[@]}"; do
	if ! which "$n" &> /dev/null; then
		printf "Please install %s in order to use this script!\n" "$n" >&2
		exit 1
	fi
done

while : ; do
	file="$(ls -1 --group-directories-first | dmenu -l 10 -p "Browse $(basename $(pwd)):")"

	if [ -e "$file" ]; then
		handle_file "$file"
	else
		break
	fi
done

Maybe this bloated thing? :slight_smile:

https://github.com/MarkHedleyJones/dmenu-extended

1 Like

or this script: (never used it)

#!/bin/bash
# rc file for explore-with-dmenu.sh
#
# This file is plain bash. It is sourced by explore-with-dmenu.sh after it loaded the defaults.
# This makes it possible to overwrite some or all of settings.
#
# There are 6 configurable variables that `explore-with-dmenu.sh` takes into account,
# but changing only 2 of them, the array $choices and the string $open_terminal_command,
# should be sufficient in most cases.

# the initial path explore-with-dmenu.sh uses; if not specified, defaults to $HOME
selected_path="$HOME"

# the path the selection history will be stored; defaults to ${HOME}/.config/.edm_history"
history_file="${HOME}/.config/.edm_history"

# the maximum number of recent entries that will be retained in the history file; defaults to 3
max_history_entries=3

# initial set of items shown at first
choices=(
    '<open terminal here>'              # add this special item to open a Shell at $selected_path
    '.'                                 # add this special item to open $selected_path in a Finder
    '..'                                # add this special item to traverse to the parent folder
    'path/to/some/often/used/folder'    # add subdolders of $selected_path like this
    'path/to/some/often/used/file.txt'  # add files in folders under $selected_path like this
    'my/music/library/'
    'my/music/library/my favorite song.mp3'
    "$(ls -t "$selected_path")"         # output of `ls -t` on the $selected_path
    "$(cat "$history_file")"            # recent entries from prior runs
)

# if the array $choices is not specified, it defaults to:
# choices=(
#     '<open terminal here>'
#     '.'
#     '..'
#     "$(ls "$selected_path")"
#     "$(cat "$history_file")"
# )

# the variable $open_command must store a command that will be executed with the chosen item is a
# file or the string '.';
# the command must be able to open files and folders and handle directory paths as arguments
open_command='xdg-open'

# the variable $open_terminal_command must store a command that will be executed with the chosen
# item is the string '<open terminal here>';
# the command must handle can open files and folders and directory paths as arguments
open_terminal_command='gnome-terminal --working-directory'

source: https://github.com/langenhagen/explore-with-dmenu

yea i checked that out

i think i checked this one out too it didn’t work

The initial script you posted works fine

#!/bin/sh

target="$1"
prompt="$2"
[ -z "$target" ] && target="$(realpath .)"

while :; do
	p="$prompt"
	[ -z "$p" ] && p="$target"
	sel="$(ls -1a "$target" | grep -v '^\.$' | dmenu -p "$p" -l 25)" || exit
	if [ "$(echo "$sel" | cut -b1)" = "/" ]; then
		newt="$sel"
	else
		newt="$(realpath "$target/$sel")"
	fi
	if [ -e "$newt" ]; then
		target="$newt"
		if [ ! -d "$target" ]; then
			echo "$target"; break
		fi
	fi
done

exit 0

Running it and selecting a file outputs that file’s path on stdout (standard for dmenu), to run or use the file selected you can integrate it into a command. An example would be to use xdg-open and let it determine which program to open the file with depending on the filetype

xdg-open "$(/path/to/script)"
# make sure it's executable!!

For me this opens text files in nvim, images in sxiv, and other filetypes in applications you’d expect.


As an aside I don’t really see the point in this as there’s plenty of lightweight file managers/browsers that already have this functionality without the need for shell scripting knowledge.

yea i know im not a programmer by any means , i can follow code and figure it out in time, im just in a dmenu phase now
so how exactly do i pipe xdg-open to this script?

in this?

	sel="$(ls -1a "$target" | grep -v '^\. | dmenu -p "$p" -l 25)" || exit

solved in the script that uses xdg-open the ls is different

file="$(ls -1a -1 --group-directories-first | dmenu -l 10 -p "Browse $(basename $(pwd)):")"

I showed an example of using it with xdg-open above.

How are you launching the script?

You can just change the one line if you don’t want it to be modular and work with anything but xdg-open

echo "$target"; break

# becomes
xdg-open "$target"; break

Using -1 twice does nothing, see man ls

yea i kinda saw an example and figured that out just now , either or will work and i found this just now which is practically the same script as one of the two just lesser code

so if you put $xdg-open (ls -1a blabla it should work right

#!/bin/bash

while true; do
    open=$(ls -1a --group-directories-first --file-type | dmenu "$@")
    if [[ -d "$open" ]]; then
        cd "$open"
    else
        if [[ "$open" != "" ]]; then
            xdg-open "$open"
        fi
        exit 0
    fi
done

Kinda yes, the length could be reduced/simplified but I don’t think it helps or is a good idea. You lose a lot of the functionality from the original (ascending/descending directories, symlink resolving with realpath, possibly others I’m missing). Try it out though and use it a bit, you’ll soon see if something is missing or bothers you and adjust from there.

yea i can see that … it sometimes opens in geany or firefox , id like to make it open in vim but il slowly get around to it thank you for your help as always

so the one script i posted is a better idea that the one i just found?this one ?

#!/bin/sh

target="$1"
prompt="$2"
[ -z "$target" ] && target="$(realpath .)"

while :; do
	p="$prompt"
	[ -z "$p" ] && p="$target"
	sel="$(ls -1a "$target" | grep -v '^\.
``` | dmenu -p "$p" -l 25)" || exit
	if [ "$(echo "$sel" | cut -b1)" = "/" ]; then
		newt="$sel"
	else
		newt="$(realpath "$target/$sel")"
	fi
	if [ -e "$newt" ]; then
		target="$newt"
		if [ ! -d "$target" ]; then
			echo "$target"; break
		fi
	fi
done

exit 0

im launching it with a key binding in dwm

Yes, that way the script just finds and allows selecting of the files, you can then use it with multiple openers or other programs.

1 Like

so i would have to specify what files open what hmmmmm so st -e vim for configs mpv for video feh for pics and so on could i have an example if u have the time?theres a ton of stuff for configuring xdg-open il figure it out thanks for your help once again

You could do that but it seems kinda annoying, xdg-open is also an overly complicated mess. I use soap, an xdg-open replacement I forked and “improved” which you could build/install with sudo make install, edit the config.h to use applications you want then make a keybind to run

xdg-open "$(/path/to/script)"

or edit the script to call xdg-open directly, soap replaces it and will fallback to the original if needed.

1 Like

il give it a try il get back to you tomorrow thank for your help

great little tool u got there man!!thats a keeper!!also checking noice i like it already…

hey nate quick dumb question how do i implement a icon on the left hand side of each file/directory with printf or echo? i cant seem to get it, maybe you could give me an examle?or do i have to rewrite the whole script