Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fzf to find and execute a program from a directory

Tags:

bash

terminal

fzf

I'm trying to create a command line for fzf that will easily allow me to select and run one of the many handy utilities (i.e. appimages) that I've downloaded.

E.g. this shell command finds all of the executables that are in my Downloads directory:

find ~/Downloads -maxdepth 1 -perm -111 -type f

I can pipe them into fzf, but I'm having trouble making this into a script that will execute the selected file and can run this script from my desktop.

like image 902
Jay Marm Avatar asked Dec 05 '25 18:12

Jay Marm


2 Answers

If you want to run the command in the same terminal:

#!/bin/bash

executable=$(find ~/Downloads -maxdepth 1 -perm -111 -type f | fzf)
$executable
like image 191
philipper Avatar answered Dec 08 '25 06:12

philipper


I figured it out with the help of these:
https://unix.stackexchange.com/questions/108782/pass-the-output-of-previous-command-to-next-as-an-argument
https://askubuntu.com/questions/46627/how-can-i-make-a-script-that-opens-terminal-windows-and-executes-commands-in-the

I created this script "find-n-exec2.sh" (save it somewhere e.g. ~/Downloads) and be sure to chg its permission to be executable:

#!/bin/bash
find ~/Downloads -maxdepth 1 -perm -111 -type f | fzf | xargs -I{} lxterminal -e {}

Then I created this 2nd script "find-n-exec.sh" and saved it to my ~/Desktop (and also make sure to chg its permission to be executable):

#!/bin/bash
lxterminal --title="test" --command="bash -c '~/Downloads/find-n-exec2.sh'"

Note: I'm using lxterminal, so if you're using xterm or terminal, etc. make sure to chg it appropriately.

Now I can double click on the 'find-n-exec.sh' from the desktop and it opens a window listing all of the executables in my Downloads directory... cursor up/down to find the pgm I want and hit enter. It opens a new terminal window and executes that program.

Hope you find this useful!

like image 31
Jay Marm Avatar answered Dec 08 '25 06:12

Jay Marm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!