Created
March 20, 2020 06:38
-
-
Save Ricky-Wilson/606270c6eb3dc0a5a1b30481675dde4c to your computer and use it in GitHub Desktop.
Find files that match a pattern and copy them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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