Skip to main content
added 26 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Regarding tmp=$(find $Some_Dir -name "*.gz"):

  1. Always quote your shell variables, i.e. "$Some_Dir", not just $Some_Dir, see https://mywiki.wooledge.org/Quotes.
  2. Don't read file names into a scalar variable as it becomes much harder to deal with spaces in file names if you do, read them into an array.

so that should be:

readarray -d '' files < <(find "$Some_Dir" -type f -name '*.gz' -print0)

Now you can just loop on the files to do whatever you want, e.g. since you said I want to match the pattern starting with ZRT and ending with _somenumber:

re='ZRT.*_somenumber'
for file in "${files[@]}"; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done

Obviously you don't NEED the array of files in the first place, you could just doloop directly on the output of find:

re='ZRT.*_somenumber'
while IFS= read -r -d '' file; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done < <(find "$Some_Dir" -type f -name '*.gz' -print0)

Regarding tmp=$(find $Some_Dir -name "*.gz"):

  1. Always quote your shell variables, i.e. "$Some_Dir", not just $Some_Dir, see https://mywiki.wooledge.org/Quotes.
  2. Don't read file names into a scalar variable as it becomes much harder to deal with spaces in file names if you do, read them into an array.

so that should be:

readarray -d '' files < <(find "$Some_Dir" -type f -print0)

Now you can just loop on the files to do whatever you want, e.g. since you said I want to match the pattern starting with ZRT and ending with _somenumber:

re='ZRT.*_somenumber'
for file in "${files[@]}"; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done

Obviously you don't NEED the array of files in the first place, you could just do:

re='ZRT.*_somenumber'
while IFS= read -r -d '' file; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done < <(find "$Some_Dir" -type f -print0)

Regarding tmp=$(find $Some_Dir -name "*.gz"):

  1. Always quote your shell variables, i.e. "$Some_Dir", not just $Some_Dir, see https://mywiki.wooledge.org/Quotes.
  2. Don't read file names into a scalar variable as it becomes much harder to deal with spaces in file names if you do, read them into an array.

so that should be:

readarray -d '' files < <(find "$Some_Dir" -type f -name '*.gz' -print0)

Now you can just loop on the files to do whatever you want, e.g. since you said I want to match the pattern starting with ZRT and ending with _somenumber:

re='ZRT.*_somenumber'
for file in "${files[@]}"; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done

Obviously you don't NEED the array of files in the first place, you could just loop directly on the output of find:

re='ZRT.*_somenumber'
while IFS= read -r -d '' file; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done < <(find "$Some_Dir" -type f -name '*.gz' -print0)
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

Regarding tmp=$(find $Some_Dir -name "*.gz"):

  1. Always quote your shell variables, i.e. "$Some_Dir", not just $Some_Dir, see https://mywiki.wooledge.org/Quotes.
  2. Don't read file names into a scalar variable as it becomes much harder to deal with spaces in file names if you do, read them into an array.

so that should be:

readarray -d '' files < <(find "$Some_Dir" -type f -print0)

Now you can just loop on the files to do whatever you want, e.g. since you said I want to match the pattern starting with ZRT and ending with _somenumber:

re='ZRT.*_somenumber'
for file in "${files[@]}"; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done

Obviously you don't NEED the array of files in the first place, you could just do:

re='ZRT.*_somenumber'
while IFS= read -r -d '' file; do
    if [[ $file =~ $re ]]; then
        do whatever you like
    fi
done < <(find "$Some_Dir" -type f -print0)