How to optimize only non optimized mysql tables of all mysql databases?
I found several mysql linux commands, here:

Code:
mysqlcheck -u root -p --auto-repair -c -o --all-databases
this may not work to optimize tables

Code:
mysqlcheck -Aos -uuser -p
not tested

This script i ran and it checked all databases and its tables and optimized those that was "fragmented":
Code:
#!/bin/sh                                                                                                                                   
mysql -NBe "SHOW DATABASES;" | grep -v 'lost+found' \
    | while read database ; do

    #skip system-db
    if [ "$database" = "mysql" ] ; then
        continue
    fi
    mysql -NBe "SHOW TABLE STATUS;" $database | while read name engine version rowformat rows avgrowlength datalength maxdatalength indexlength datafree autoincrement createtime updatetime checktime collation checksum createoptions comment ; do

        #skip views
        if [ "$datafree" = "NULL" ] ; then
            continue
        fi
        if [ "$datafree" -gt 0 ] ; then
            echo "$database.$name is fragmented"
            mysql -NBe "OPTIMIZE TABLE $name;" "$database"
        fi
    done
done