Skip to content

Commit

Permalink
feat: add maxdepth and mindepth functions
Browse files Browse the repository at this point in the history
- instead of `find . -maxdepth 2 -print`, enter `maxdepth 2`, which
  returns a POSIX-compliant equivalent:
  `find . -path './*/*/*' -prune -o -print`

- instead of `find . -mindepth 2 -print`, enter `mindepth 2`, which
  returns a POSIX-compliant equivalent:
  `find . -path './*/*' -print`

Signed-off-by: Lucas Larson <[email protected]>
  • Loading branch information
LucasLarson committed Apr 30, 2024
1 parent c9ac1e4 commit 415105e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions custom/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,33 @@ mu() {
esac
}

## find `-maxdepth` and `-mindepth`
# generate a POSIX-compliant equivalent
maxdepth() {
# instead of `find . -maxdepth 2 -print`, use `find . -path './*/*/*' -prune -o -print`
path_argument='./*'
depth="${1:-0}"
while command -p -- test "${depth-}" -gt 0; do
path_argument="${path_argument-}"'/*' &&
depth="$((depth - 1))"
done
command -p -- printf -- '#!/usr/bin/env sh\ncommand -p -- find -- . -path \047%s\047 -prune -o -print\n' "${path_argument-}"
unset -v -- path_argument 2>/dev/null || path_argument=''
unset -v -- depth 2>/dev/null || depth=''
}
mindepth() {
# instead of `find . -mindepth 2 -print`, use `find . -path './*/*' -print`
path_argument='.'
depth="${1:-0}"
while command -p -- test "${depth-}" -gt 0; do
path_argument="${path_argument-}"'/*' &&
depth="$((depth - 1))"
done
command -p -- printf -- '#!/usr/bin/env sh\ncommand -p -- find -- . -path \047%s\047 -print\n' "${path_argument-}"
unset -v -- path_argument 2>/dev/null || path_argument=''
unset -v -- depth 2>/dev/null || depth=''
}

# https://unix.stackexchange.com/a/30950
alias mv='command mv -v -i'

Expand Down

0 comments on commit 415105e

Please sign in to comment.