shell-base/modules/utility/find.lib

197 lines
4.2 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# export BASH_DEBUG=true
module_load debug
module_load iecho
module_load file
# find, superceeds find use `command find` to get the super
function _find () {
# BASH_DEBUG=find
# USAGE
# all option arguments that contain globs/wildcards must be quoted to avoid expansion
# f sets path and file excludes from a supplied file path
# all lines ending in / will be treated as directory names to ignore, otherwise files
# p option explictly excludes paths(directories)
# d option sets the directory depth which is current directy by default, 0 is all
# n inclucdes only file globs otherwise it's all except .files
# if no directory is given it will attempt to source the present working directory
# example:
# source_dir -p "archive" -e '"*.off" "*.md"' -d 0 # $DIR/$SUBDIR
local EXCLUDE_FILE
local PATHS
local NAMES
local ENAMES
local DEPTH=1
local HIDDEN
local raw
local mounted
local usesudo
mounted="-mount"
declare OPTION
declare OPTARG
declare OPTIND
while getopts 'mrst:p:d:e:n:f:hl' OPTION; do
case "$OPTION" in
s)
usesudo=sudo
;;
r)
raw=true
;;
t)
TYPE=$OPTARG
# echo "TYPE $TYPE"
;;
f)
EXCLUDE_FILE=$OPTARG
# echo EXCLUDE FILE $EXCLUDE_FILE >&2
;;
p)
# PATHS=("$OPTARG")
IFS=',' read -r -a PATHS <<< "$OPTARG"
# echo EXCLUDING THESE PATHS ${PATHS[*]}
;;
e)
IFS=',' read -r -a ENAMES <<< "${OPTARG}"
# echo EXCLUDING THESE FILE NAMES ${ENAMES[*]}
;;
n)
# NAMES=("$OPTARG")
IFS=',' read -r -a NAMES <<< "${OPTARG}"
# NAMES=$OPTARG
;;
d)
DEPTH=$OPTARG
# echo "SOURCING TO DEPTH (0=any)" "$DEPTH"
;;
h)
HIDDEN=true
# echo "SOURCING TO DEPTH (0=any)" "$DEPTH"
;;
l)
LINKS=-L
;;
m)
# include mounted directories
mounted=""
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
if [[ $raw ]]; then
echo executing raw find command
$usesudo $(which find) "$@"
fi
local DIR
DIR="$*"
if [ ! "$DIR" ]; then
if [ -v PS1 ]; then
echo no directory provided to search
echo searching present working directory $(pwd)
read -p "Do you want to continue? " -n 1 -r
[[ $REPLY =~ ^[Yy]$ ]] && DIR=$(pwd) || return 1
else
return 1
fi
fi
[ ! -d "$DIR" ] && echo " directory $DIR does not exist, aborting" && return 1
# echo dir $DIR
local FIND
FIND="$usesudo $(which find) $LINKS $DIR"
FIND+=$([ ! $DEPTH == 0 ] && echo " -maxdepth $DEPTH ")
# FIND+=" -type $([ $TYPE ] && echo "$TYPE" || echo "f")"
TYPE=${TYPE:-f}
debug exclude mounts: $mounted
FIND+=" $mounted -type $TYPE "
# include HIDDEN files and directories IS FALSE BY DEFULT
[[ ! $HIDDEN ]] && FIND+="! -path \"*/.*/*\" ! -name \".*\" "
local name
local path
if [[ -f $EXCLUDE_FILE ]]; then
local ignores=$(lines_2_str "$EXCLUDE_FILE")
# echo ignores: $ignores >&2
for exclude in $ignores ; do
# echo exclude: ${exclude} >&2
[[ "$exclude" == */ ]] && PATHS+=("${exclude::-1}") || ENAMES+=("$exclude")
done
fi
# echo paths ${PATHS[@]} >&2
# echo exclude names ${ENAMES[@]} >&2
set -o noglob
if [[ ${PATHS[0]} ]]; then
for path in ${PATHS[@]}; do
# echo excluding $path
FIND+=$(echo ' ! -path "*/'$path'/*"')
done
fi
if [[ ${ENAMES[0]} ]]; then
for name in ${ENAMES[@]}; do
debug excluding name "$name"
FIND+=$(echo ' ! -name '$name'')
done
fi
debug "INCLUDING ONLY THESE FILE NAMES ${NAMES[*]}"
if [[ ${NAMES[0]} ]]; then
for name in "${NAMES[@]}"; do
debug only finding $name
FIND+=$(echo " -name '${name}'")
done
fi
# if [[ $NAMES ]]; then
# debug names for find command $NAMES
# for name in $NAMES; do
# debug "xxonly finding '$name'"
# FIND+=$(set -o noglob;echo " -name '${name}'")
# done
# fi
# echo
# echo find dir: $DIR >&2
debug "find command: $FIND"
set +o noglob
local FILES
FILES=$(eval $FIND | sort)
[[ $FILES ]] && echo $FILES
return 0
}
# change-ext() {
# local depth
# local d
# d=${3:-0}
# let d+=1
# [ $d -lt 1 ] && depth="" || depth=" -maxdepth $d "
# find . $depth -name '*.'$1 -exec rename -n 's/\.'$1'/.'$2'/' \{} \;
# read -p "do REALLY want to change the extensions? " -n 1 -r
# echo
# [[ ! $REPLY =~ ^[Yy]$ ]] && return 1
# find . $depth -name '*.'$1 -exec rename 's/\.'$1'/.'$2'/' \{} \;
# }
# alias chext=change-ext