Skip to content

Instantly share code, notes, and snippets.

@Ricky-Wilson
Created March 20, 2020 06: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 Ricky-Wilson/606270c6eb3dc0a5a1b30481675dde4c to your computer and use it in GitHub Desktop.
Save Ricky-Wilson/606270c6eb3dc0a5a1b30481675dde4c to your computer and use it in GitHub Desktop.
Find files that match a pattern and copy them
#!/bin/bash
while getopts "r:p:o:" flag; do
case "$flag" in
# Where to stert searching.
r) ROOT=$OPTARG;;
# The pattern.
p) NAME=$OPTARG;;
# where to store the findings.
o) OUTPUT=$OPTARG;;
esac
done
if [[ -z "$NAME" ]]; then
echo "Missing argument [-p pattern]"
exit 1
fi
if [[ -z "$OUTPUT" ]]; then
echo "Missing argument [-O output_dir/]"
exit 1
fi
if [[ ! $OUTPUT =~ "/" ]]; then
OUTPUT+="/"
fi
if [ -z "$ROOT" ]; then
ROOT="$PWD"
fi
mkdir "$OUTPUT" 2> /dev/null
find "$ROOT" -type f -iname "$NAME" -exec cp "{}" $OUTPUT \;
#ls $OUTPUT | sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment