0

编辑:我找到了一个可行的解决方案。引用文件路径以确保它们被整体阅读显然很重要。

path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"

我写了一个我想在 osx 的终端中使用的命令。它获取目录中的文件和文件夹路径列表,将它们打乱,然后在打乱后获取 txt 第一行中的路径。

这是我到目前为止所得到的:[1]

gfind ~/x/y/ | gshuf | ghead -n 1

有了gfind ~/x/y/ | gshuf | ghead -n 1 > ~/Desktop/z.txt这种格式的文件路径,/Users/me/x/y/some folder/some file.txt我不想将此文件或文件夹的路径写入.txt,而是想打开它,就好像我只是在finder中双击它一样。我怎样才能做到这一点?根据手册页中的描述,我认为 open 命令 [2] 是正确的,但我不确定如何使用它。我应该如何使用它?或者,如果它是错误的命令,我应该使用哪个命令?

[1]

我通过 macports 使用 gnu coreutils,这就是为什么熟悉的命令名称前面有 ag

[2]

open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments] Help: Open opens files from a shell. By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL. Options: -a Opens with the specified application. -b Opens with the specified application bundle identifier. -e Opens with TextEdit. -t Opens with default text editor. -f Reads input from standard input and opens with TextEdit. -F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents. -R, --reveal Selects in the Finder instead of opening. -W, --wait-apps Blocks until the used applications are closed (even if they were already running). --args All remaining arguments are passed in argv to the application's main() function instead of opened. -n, --new Open a new instance of the application even if one is already running. -j, --hide Launches the app hidden. -g, --background Does not bring the application to the foreground. -h, --header Searches header file locations for headers matching the given filenames, and opens them.

4

2 回答 2

0

这对我有用:

path_to_open=$(gfind ~/x/y/ | gshuf | ghead -n 1) && open "${path_to_open}"

于 2013-06-15T22:41:58.800 回答
0

要在 Finder 中打开的结果中返回文件夹,这应该可以:

open -a Finder `gfind ~/x/y/ | gshuf | ghead -n 1`
于 2013-06-14T23:56:14.033 回答