Skip to content

Instantly share code, notes, and snippets.

@sk22
Last active August 25, 2020 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sk22/c94391c0ea7c6debee1e2b0062e19fba to your computer and use it in GitHub Desktop.
Save sk22/c94391c0ea7c6debee1e2b0062e19fba to your computer and use it in GitHub Desktop.
search using a regex pattern and link the resulting files into a directory
#!/bin/sh
export SEARCH_PATH=${SEARCH_PATH:-../source}
export USE_PATTERN_AS_RESULTS=${USE_PATTERN_AS_RESULTS:-true}
export DELETE_FOLDER=${DELETE_FOLDER:-true}
#!/bin/sh
alias words=./words.sh
alias search=~/bin/search
echo "search results folder:" $(search $(words -f $@))
if [ ! -z "$1" ]; then
echo "command added:" $(echo "search \$(words -f $@)" | tee -a searches.sh)
fi
#!/bin/sh
set -o errexit -o pipefail -o noclobber
default_results_directory=${SEARCH_RESULTS_DIRECTORY:-./_search-results}
default_search_path=./
usage() {
cmd=$(basename $0)
echo search using a regex pattern and link the resulting files into a directory
echo
echo usage:
echo " $cmd [-n] regex_pattern" \
[search_path = \"$default_search_path\"] \
[results_directory = \"$default_results_directory\"]
echo
echo switches:
echo ' * `-n` - dry run: terminates the program before making changes ' \
'to the file system'
echo
echo examples:
echo " * \`$cmd '(kitty|cat)' ./photos ./cat-pics\`"
echo ' (searches for files inside ./photos containing "kitty" or "cat"' \
'and puts them into ./cat-pics)'
echo " * \`cd \$($cmd '.mp4$')\`"
echo ' (searches for files ending with .mp4, puts them in a' \
'folder and enters it)'
echo
echo variables:
echo ' * `USE_PATTERN_AS_RESULTS=true`'
echo ' declares that the pattern can be used as the results' \
'directory name as a fallback'
echo ' * `DELETE_FOLDER=true`'
echo ' declares that the results_directory should be delete and' \
're-created'
echo ' * `DEBUG=true`'
echo ' output more debug info to stderr'
echo ' * `SEARCH_PATH`'
echo ' allows the omission of the search_path argument and uses this' \
'environment variable instead'
echo
echo output:
echo ' relative path to the search results directory.' \
'outputs the results_directory argument as-is'
echo
echo function:
echo ' `find -E $search_path -type f -regex ".*$regex_pattern.*"' \
'-exec ln -s ../{} $results_directory \;`'
exit
}
debug() {
if [ "$DEBUG" == true ]; then
echo $@ >&2
fi
}
if [ $1 == '-n' ]; then
dry=true
shift
fi
if [ -f .searchrc ]; then
. .searchrc
fi
if [ -z "$1" ]; then
usage
fi
debug "[debug] USE_PATTERN_AS_RESULTS=$USE_PATTERN_AS_RESULTS"
debug "[debug] DELETE_FOLDER=$DELETE_FOLDER"
debug "[debug] DEBUG=$DEBUG"
debug "[debug] SEARCH_PATH=$SEARCH_PATH"
debug
regex_pattern=$1
if [ -z "$3" ] && [ ! -z "$SEARCH_PATH" ]; then
debug [warn] detected custom SEARCH_PATH environment variable!
debug [info] using second argument as results_directory.
search_path="$SEARCH_PATH"
results_directory="$2"
else
search_path="$2"
results_directory="$3"
fi
if [ -z "$results_directory" ] && [ "$USE_PATTERN_AS_RESULTS" = true ]; then
debug [warn] detected environment variable USE_PATTERN_AS_RESULTS!
debug [info] using pattern \"$regex_pattern\" as search results directory name.
results_directory=$regex_pattern
fi
# use defaults in case everything else failed
search_path=${search_path:-$default_search_path}
results_directory=${results_directory:-$default_results_directory}
debug
debug "[debug] search_path=$search_path"
debug "[debug] regex_pattern=$regex_pattern"
debug "[debug] results_directory=$results_directory"
if [ "$dry" == true ]; then
debug [info] dry run is set. exiting
exit 0
fi
if [ "$DELETE_FOLDER" = true ]; then
rm -rf $results_directory
fi
mkdir -p $results_directory
find -E $(realpath $search_path) -type f -regex ".*$regex_pattern.*" \
-exec ln -s {} $results_directory \;
echo $results_directory
#!/bin/sh
alias words=./words.sh
alias search=~/bin/search.sh
search $(words -f cat)
#!/bin/sh
if [ $1 == '-f' ]; then
output_folder_name=true
shift
fi
word_delimiters='[-]'
word_prefix="(^|$word_delimiters)"
word_suffix="($word_delimiters|$)"
# (^|[-])(oida|foida)([-]|$)
export PATTERN="$word_prefix($(echo $@ | tr ' ' '|'))$word_suffix"
export FOLDER_NAME=$(echo $@ | tr ' ' '-')
if [ "$output_folder_name" = true ]; then
echo "$PATTERN $FOLDER_NAME"
else
echo $PATTERN
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment