PDA

View Full Version : Linux script to search for certain file types & do speciffic action for each filetype



Fli
11-16-2014, 07:39 PM
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"

starttime=$(date)

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
curext=${imgurl: -3}

if [ "$curext" == "gif" ];then
gifsicle -b -O2 $imgurl
sleep 0.3
fi

if [ "$curext" == "jpg" ];then
jpegoptim -m85 $imgurl
sleep 0.3
fi

if [ "$curext" == "png" ];then
optipng -o2 -preserve $imgurl
chown $cpaneluser:$cpaneluser $imgurl
sleep 0.3
fi

done
done

endtime=$(date)

echo "Optimization on cpanel accounts complete, script in root of $(hostname) Start: $starttime , End: $endtime" | mail -s "IMG Optimization complete" [email protected]

Hope that helps, if you have some ideas or modiffications, please kindly share. Thank You