This script was created to optimize 3 types of files on linux server - gif, jpg, png
for each file another linux command is executed. The script can be modiffied to search for other files / filenames / directory names.. and do speciffic action for each type, name..
userlist="username1 username2 username3"
for cpaneluser in $userlist;do
for imgurl in $(nice -n 19 find /home/$cpaneluser/public_html -type f -iname "*.gif" -o -iname "*.jpg" -o -iname "*.png"); do
# now we have full image url set in "imgurl" variable
# get last 3 characters from "imgurl" (file extension) so we can discover which filetype we currently processing
if [ "$curext" == "gif" ];then
gifsicle -b -O2 $imgurl
if [ "$curext" == "jpg" ];then
jpegoptim -m85 $imgurl
if [ "$curext" == "png" ];then
optipng -o2 -preserve $imgurl
chown $cpaneluser:$cpaneluser $imgurl
echo "Optimization on cpanel accounts complete, script in root of $(hostname) Start: $starttime , End: $endtime" | mail -s "IMG Optimization complete" email@example.com
Hope that helps, if you have some ideas or modiffications, please kindly share. Thank You