Вы можете перечислить файлы по времени модификации (или созданию или доступу )с помощью ls.
просто скажи
ls -t *.txt | head -n5
, чтобы получить последние 5 последних файлов.txt
Вы можете передать его в myutility с помощью $ ()или xargs.
ls -t *.txt | head -n5 | xargs myutility
Если вам нужна рекурсия, вы можете использовать find.
find -type f -iname "*.txt" | xargs ls -t | head -n5 | xargs myutility
(возможная проблема :у вас должен быть хотя бы один файл.txt, чтобы это работало. вы можете исправить это, но это самое чистое решение ).
РЕДАКТИРОВАТЬ :ОП хотела знать, как обрабатывать имена файлов с пробелами.Вот одно решение:
find -type f -iname "*.txt" | xargs ls -t | head -n5 | tr '\n' '\0' | xargs -0 myutility
Если вы хотите решить возможную проблему, упомянутую выше, вы можете проверить наличие файлов перед -с помощью find..... | egrep '.*' || exit
или просто включить какое-то бессмысленное имя файла в ls и проигнорировать предупреждение вроде xargs ls -t ''
.
Предположим, что функция, для которой вы хотите определить завершение, называется myfunc
.
Во-первых, давайте настроим фактическую функцию:
myfunc
. .zsh
или .sh
. myfunc
, находится в вашем $fpath
. Например, если файл myfunc
находится в ~/func
, добавьте его в свой ~/.zshrc
файл :. fpath+=( ~/func )
myfunc
в файл ~/.zshrc
:# We pass a couple of options that make the code less likely to break:
# -U suppresses alias expansion
# -z marks the function for zsh-style autoloading == `unsetopt KSH_AUTOLOAD`
autoload -Uz myfunc
Теперь вы сможете использовать myfunc
в командной строке (, но без каких-либо дополнений ).
Далее создадим функцию завершения:
_myfunc
. #compdef myfunc
# The line above means "This function defines completions for myfunc."
# The combination of that line, plus the file name starting with an
# `_`, plus having this file's parent dir in your `$fpath`, ensures
# this file will get autoloaded when you call `compinit`.
# `+X` makes sure `myfunc`'s definition will get loaded immediately,
# even if you have not called this function yet.
autoload +X -Uz myfunc
# Get the definition of `myfunc` in string form.
local funcdef=$(type -f myfunc)
# Get the part that matches `case*esac`, then split it on whitespace
# and put the resulting words in an array.
local -a words=( ${=funcdef[(r)case,(r)esac]} )
# Keep only the words that start with `(` and end with `)`.
# Even if you used the `case` syntax with only the closing `)`s,
# in `type -f`, your cases will appear with both `(` and `)`.
local -a required=( ${(M)words:#'('*')'} )
# `-s`: Allow options to `myfunc ` to be stacked, that is, you are
# allowed to specify `myfunc -rm`. If not, remove the `-s`
# option.
# `*:`: Let this argument be completed in any position.
_arguments -s \
{-r,--readonly}'[description for "readonly"]' \
{-m,--mount}'[description for "mount"]' \
"*:required argument:( ${required//[()]/} )"
замените required argument
тем, что вы хотите назвать своим аргументом, и заполните описания параметров. funcname() {…}
. $fpath
. autoload -Uz compinit; compinit
в своем файле .zshrc
и убедитесь, что он запускается после того, как указанный выше каталог был добавлен в ваш $fpath
. exec zsh
или закройте окно терминала и откройте новое.
Теперь вы должны получить завершение для myfunc
.
Если readonly
и mount
являются взаимоисключающими, вам нужно будет переписать последнюю строку функции завершения следующим образом:
_arguments \
(-m --mount){-r,--readonly}'[description for "readonly"]' \
(-r --readonly){-m,--mount}'[description for "mount"]' \
"*:required argument:( ${required//[()]/} )"