I'm newbie to bash scripting, made this script work as I expected though it's still a first draft:
#!/bin/bash
find /var/www/site.com/ -type f -name "*.log" | xargs bash -c '
for name; do
parent=${name%/*}
parent=${parent##*/}
destdir=$(dirname $name)
current_year=$(date +"%Y")
if [[ "$name" =~ _([0-9]{4})- ]] &&
[[ "$name" != *"$current_year"* ]]; then
year="${BASH_REMATCH[1]}"
else continue
fi
if [ ! -d "$destdir/$year/$parent" ]; then
mkdir -p "$destdir/$year/"
fi
mv "$name" "$destdir/$year"
done '
find /var/www/site.com/ -type d -name "20*" | xargs bash -c '
for dir; do
tar_name=$(echo $dir | grep -Eo '[0-9]{4}')
tar cvfj '$tar_name'.tbz $dir
done '
exit
as you can see I'm invoking bash twice inside a bash script . Is that a bad practice ? makes no / little sense ? Or I'd better off creating an array with the output results of find commands and iterate through it? Any suggestion would be appreciated. Thanks in advance. P.S. Sorry for missing indentation