使用 fzf 从目录中查找和执行程序

Using fzf to find and execute a program from a directory

我正在尝试为 fzf 创建一个命令行,它可以让我轻松地 select 和 运行 我使用的许多方便的实用程序(即 appimages)之一已下载。

例如此 shell 命令查找我的下载目录中的所有可执行文件:

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

我可以将它们通过管道传输到 fzf,但是我无法将其制作成一个脚本来执行 selected 文件并且可以从我的桌面 运行 这个脚本。

我在这些帮助下弄明白了:
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

我创建了这个脚本"find-n-exec2.sh"(将它保存在某个地方,例如 ~/Downloads) 一定要更改它的权限可执行:

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

然后我创建了第二个脚本 "find-n-exec.sh" 并将其保存到我的 ~/Desktop (并确保更改其可执行权限):

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

注意:我使用的是 lxterminal,因此如果您使用的是 xterm 或终端等,请确保对其进行适当的更改。

现在我可以在桌面上双击 'find-n-exec.sh',它会打开 window,列出我的下载目录中的所有可执行文件...光标 up/down 找到pgm 我想要并按回车键。它打开一个新终端 window 并执行该程序。

希望你觉得这有用!

如果你想运行同一终端中的命令:

#!/bin/bash

executable=$(find ~/Downloads -maxdepth 1 -perm -111 -type f | fzf)
$executable