Suppose I want to look through a list of files and interactively open it through the terminal, I've been using

locate filename | head -n 1 | xargs xdg-open

or some such thing. The other option is to click on the filename that shows up using the mouse and then paste it.

Tmux let's me navigate my terminal, copy lines and incorporate them into commands. Is there a way to do this in bash or zsh, or perhaps directly in the terminal emulator?


First put the choices in an array. Bash syntax:

IFS=$'\n' read -r -d '' -a choices < <(locate filename)

Zsh syntax:

choices=("${(@f)$(locate filename)}")

The low-tech, but not very user-friendly way to let the user select one of the item is to use the select construct.

select choice in "${choices[@]}"; do
  xdg-open "$choice"

(Bash syntax; this also works in zsh but there it can be simplified.)

For a nicer interface, you can use dialog.

for c in "${choices[@]}"; do
  menu_args+=("$c" "$c")
if choice=$(dialog --menu "$title" "$LINES" "$COLUMNS" "$LINES" "${menu_args[@]}"); then
  xdg-open "$choice"
  • This is a cool solution! Here's my solution, which uses dmenu (in a script): locate --all $* | dmenu | xargs xdg-open. It still doesn't let me navigate my console like tmux however. :(
    – user128063
    Sep 25 '16 at 3:38

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.