From efa7054b82b1edf21f8b8a04670ea158cbd04576 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Fri, 3 Jun 2022 13:14:32 +0200 Subject: [PATCH] zsh:funcs: Add `suffix` Find files that end with one of multiple given suffixes. Usage: suffix sfx... [-- path...] `sfx` is given to `find` in the form `-name "*$sfx"`. `path` is given as starting point to `find`, defaulting to `.`. --- .config/zsh/zshrc.d/40-functions.zsh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.config/zsh/zshrc.d/40-functions.zsh b/.config/zsh/zshrc.d/40-functions.zsh index 20e543f..b35f570 100644 --- a/.config/zsh/zshrc.d/40-functions.zsh +++ b/.config/zsh/zshrc.d/40-functions.zsh @@ -531,3 +531,27 @@ shellcheck() { | "$json_parser" done } + +# Find files that end with one of multiple given suffixes. +# +# Usage: +# suffix sfx... [-- path...] +# +# `sfx` is given to `find` in the form `-name "*$sfx"`. +# `path` is given as starting point to `find`, defaulting to `.`. +suffix() { + if (( ! $+commands[find] )); then + printf >&2 "find not installed\n" + return 1 + fi + + local i=1 + for arg; do + [[ $arg != "--" ]] || break + : "$((i++))" + done + # NOTE: if "--" is not included in $@, i will be greater than $#, and no + # starting point is passed to `find`, which then defaults to `.`. + + find "${@:$((i+1))}" -name "*${(@j: -o -name *:)@:1:$((i-1))}" +}