This could be simpler:
#!/usr/bin/env bash
FILETYPES=(html css js xml json)
DIRECTORIES=("$PWD")
MIN_SIZE=1024
IFS='|' eval 'FILTER="^.*[.](${FILETYPES[*]})\$"'
for DIR in "${DIRECTORIES[@]}"; do
while IFS= read -ru 4 FILE; do
GZ_FILE=$FILE.gz
if [[ -e $GZ_FILE ]]; then
[[ $GZ_FILE -ot "$FILE" ]] && gzip -k -4 -c "$FILE" > "$GZ_FILE"
elif [[ $(exec stat -c '%s' "$FILE") -ge MIN_SIZE ]]; then
gzip -k -4 -c "$FILE" > "$GZ_FILE"
fi
done 4< <(exec find "$DIR" -mindepth 1 -type f -regextype egrep -iregex "$FILTER")
done
- There's no need to use
pwd. You can just have $PWD. And probably what you needed was an array variable as well.
- Instead of calling bash multiple times as an argument to
find with static string commands, just read input from a pipe or better yet from a named pipe through process substitution.
- Instead of comparing stats, you can just use
-ot or -nt.
- You don't need
-f if you're writing the output through redirection (>) as that form of redirection overwrites the target by default.
- You can just call
find against multiple files once by making a pattern as it's more efficient. You can check how I made the filter and used -iregex. Probably doing \( -iname one_ext_pat -or -iname another_ext_pat \) can also be applicable but it's more difficult.
exec is optional to prevent unnecessary use of another process.
- Always prefer
[[ ]] over [ ].
4< opens input with file descriptor 4 and -u 4 makes read read from that file descriptor, not stdin (0).
- What you probably need is
-ge MIN_SIZE (greater than or equal) not -gt.
Come to think of it, readarray is a cleaner option if your bash is version 4.0 or newer:
for DIR in "${DIRECTORIES[@]}"; do
readarray -t FILES < <(exec find "$DIR" -mindepth 1 -type f -regextype egrep -iregex "$FILTER")
for FILE in "${FILES[@]}"; do
...
done
done
find "$currentdir", notfind $currentdir."$(foo)" -gt "$MIN_SIZE", not$(foo) -gt $MIN_SIZE(and$()in place of backticks if you want saner nesting behavior)."$PLAINFILE", not$PLAINFILE. And, yes, you need to export any variables you want subprocesses to see.finddecide whether the file is larger than your minimum or not? There's no need to use a shell for that.