cron daily and weekly scripts for a minimal Nginx server environment I've created the following cron for daily and weekly scripts for a minimal Nginx server environment (as part of my larger script).

The daily cronjob updates WordPress and brings back original permissions (that are changed by WordPress in its update). The weekly task clears accumulated WordPress cache, make local backups, and delete old backups.

I wonder where this script could be shortened.

cat &lt;&lt;-"CRON_DAILY" &gt; /etc/cron.daily/nses_cron_daily
    for dir in /var/www/html/*/; do cd "$dir" &amp;&amp; /usr/local/bin/wp plugin update --all --allow-root; done
    for dir in /var/www/html/*/; do cd "$dir" &amp;&amp; /usr/local/bin/wp core update --allow-root; done
    for dir in /var/www/html/*/; do cd "$dir" &amp;&amp; /usr/local/bin/wp theme update --all --allow-root; done
    chown www-data:www-data /var/www/html/* -R
    find /var/www/html/* -type d -exec chmod 755 {} \;
    find /var/www/html/* -type f -exec chmod 644 {} \;
CRON_DAILY
chmod +x /etc/cron.daily/nses_cron_daily

# ---------------------------------------------------------------------------------------------------- #

cat &lt;&lt;-"CRON_WEEKLY" &gt;&gt; /etc/cron.weekly/nses_cron_weekly
    for dir in /var/www/html/*/wp-content; do cd "$dir" &amp;&amp; cd cache &amp;&amp; rm -rf *; done;

    zip -r /root/backups/dirs/html-$(date +\%F-\%T).zip /var/www/html
    find /root/backups/dirs/* -mtime +30 -exec rm {} \;
    mysqldump -u root -pPASSWORD --all-databases &gt; /root/backups/db/db-$(date +\%F-\%T).sql
    find /root/backups/db/* -mtime +30 -exec rm {} \;
CRON_WEEKLY
chmod +x /etc/cron.weekly/nses_cron_weekly
echo "Change DB root password in crontab."
As mentioned by cas in the discussion, you can simplify your loops:
for dir in /var/www/html/*/; do
    if pushd &quot;$dir&quot;; then
        /usr/local/bin/wp plugin update --all --allow-root &amp;&amp;
        /usr/local/bin/wp core update --allow-root &amp;&amp;
        /usr/local/bin/wp theme update --all --allow-root
        popd
    fi
done

You can also “simplify” the chmod calls:
find /var/www/html/* -exec chmod a-x,a=rX,u+w {} \+

This processes directories and files in one go, and calls chmod as few times as possible (\+ instead of \;). Using the recursive flag to chmod means you can also drop find:
chmod -R a-x,a=rX,u+w /var/www/html/*

In the weekly job,
for dir in /var/www/html/*/wp-content; do cd &quot;$dir&quot; &amp;&amp; cd cache &amp;&amp; rm -rf *; done;

should be doable as
for dir in /var/www/html/*/wp-content/cache/; do rm -rf &quot;${dir}/*&quot;; done

instead, or even
rm -rf /var/www/html/*/wp-content/cache/*

The \+ comment regarding find is also relevant here.
I highly recommend using “proper” backup tools instead of ad hoc backups (but it’s great to have backups, whatever they are). In particular, automysqlbackup is packaged in Debian and works great for MySQL backups, with automatic pruning and nice password handling; I find BorgBackup great for file archiving.
As cas also tried to suggest, you could do away with the heredocs and perhaps make life simpler for yourself by retrieving the scripts directly. One way to do this would be to store them separately, alongside nses.sh, and retrieve them either as part of a git clone or using curl.