From 6c1fafea1a9b853a513cc4936e0e5cd682a39a5d Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Fri, 30 Sep 2022 23:50:14 -0700 Subject: [PATCH] Split search type functionality into multiple functions --- bash/search.sh | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/bash/search.sh b/bash/search.sh index cc6f33f..89c45d7 100644 --- a/bash/search.sh +++ b/bash/search.sh @@ -1,5 +1,5 @@ # Utility functions and helpers for searching -DEFAULT_FD_OPTS=('--hidden' '--type' 'f' '--type' 'l' '--threads' "$(nproc)") +DEFAULT_FD_OPTS=('--hidden' '--type' 'symlink' '--threads' "$(nproc)") # Search and return a string with filenames delimited by \n # Parameters: @@ -9,8 +9,39 @@ find_files() { if [[ -d "$1" ]]; then local directory="$1" shift - fd "${DEFAULT_FD_OPTS[@]}" "$@" -- . "$directory" + fd "${DEFAULT_FD_OPTS[@]}" --type f "$@" -- . "$directory" else - fd "${DEFAULT_FD_OPTS[@]}" "$@" + fd "${DEFAULT_FD_OPTS[@]}" --type f "$@" + fi +} + +# Search and return a string with filenames delimited by \n +# Parameters: +# $1: directory +# $2-n: extra arguments +find_directories() { + if [[ -d "$1" ]]; then + local directory="$1" + shift + fd "${DEFAULT_FD_OPTS[@]}" --type d "$@" -- . "$directory" + else + fd "${DEFAULT_FD_OPTS[@]}" --type d "$@" + fi +} + +# Search all files and directories, determine what type of files to search +# Parameters: +# $1: filetype (specific to fd's --type flag) +# $2: directory +# $3-n: extra arguments +find_specific() { + local type_opt="$1" + shift + if [[ -d "$1" ]]; then + local directory="$1" + shift + fd "${DEFAULT_FD_OPTS[@]}" --type "$type_opt" "$@" -- . "$directory" + else + fd "${DEFAULT_FD_OPTS[@]}" --type "$type_opt" "$@" fi }