Skip to content

Instantly share code, notes, and snippets.

@psyray
Last active September 3, 2019 12:06
Show Gist options
  • Save psyray/4cf4bfd8cde8e430b762973e8e1cadf4 to your computer and use it in GitHub Desktop.
Save psyray/4cf4bfd8cde8e430b762973e8e1cadf4 to your computer and use it in GitHub Desktop.
Find and delete unused joomla images
#!/bin/bash
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILE\n"
exit 1
fi
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.\n"
exit 1
fi
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
#!/bin/sh
# jfindfiles -- Find used and unused content files in your Joomla website
#
# This scripts supports Joomla versions 2.5 - 3.x
#
# Copyright 2014 Rene Kreijveld - [email protected]
#
# This program is free software; you may redistribute it and/or modify it.
#
#
# Warning! This script needs the file jfunctions. This has to be installed in the same directory as this script.
#
# general variables
mypath=$(cd $(dirname ${0}); pwd -P)
myname=$(basename ${0})
# include general functions
. ${mypath}/jfunctions.sh
# version
version=2.2
# Setup Vvariables
start=./images
curdir=`pwd`
echo "Creating database dump..."
# Dump the database to a .sql file
if mysqldump --skip-opt --add-drop-table --add-locks --create-options --disable-keys --lock-tables --quick --set-charset --host=${host} --user=${dbuser} --password=${password} ${database} > ${database}.sql
then
echo "Database dump ${database}.sql created."
else
echo "Error creating database dump."
exit 1
fi
dbdump=${curdir}/${database}.sql
usedfile=${curdir}/${sitename}-used.txt
notusedfile=${curdir}/${sitename}-notused.txt
echo "The following files were mentioned in your Joomla database:" > ${usedfile}
echo "The following files were NOT mentioned in your Joomla database:" > ${notusedfile}
echo "Checking for used and unused files..."
# Move into the images/stories directory
cd ${start}
# Find all files and check if they are mentioned in the database dump
for file in `find . -type f -print | cut -c 3- | sed 's/ /#}/g'`
do
file2=`echo $file | sed 's/#}/ /g'`
file3=`echo $file | sed 's/#}/%20/g'`
if grep -c "$file2" ${dbdump} > 0; then
echo $file2 >> ${usedfile}
elif grep -c "$file3" ${dbdump} > 0; then
echo $file3 >> ${usedfile}
else
echo $file3 >> ${notusedfile}
fi
done
# Move back to the root of the website
cd ${curdir}
# Cleanup database dump
rm ${dbdump}
# Report findings
echo "Files checking done."
echo "Check the following text-files for results:"
echo "${usedfile}"
echo "${notusedfile}"
#!/bin/sh
# jfunctions - general bash functions for use in Joomla bash scripts
# Supports Joomla versions 2.5 - 3.x
#
# Copyright 2014 - 2016 Rene Kreijveld - [email protected]
#
# This program is free software; you may redistribute it and/or modify it.
#
# Version history
# 3.0 Initial version
# 3.1 Code rewrite
# 3.2 Modification of all echo -e statements and remove backticks
# 3.3 Update to support Joomla 3.5
# 3.4 Dropped support for Joomla versions lower than 2.5
# Define general variables
version=3.4
# Check if configuration.php exists.
if [ ! -e ./configuration.php ]
then
echo ""
echo "File configuration.php not found. Are you at the root of the site?"
echo ""
exit 1
fi
# Find MySQL Socket
if [ -S /var/lib/mysql/mysql.sock ]; then
mysock=/var/lib/mysql/mysql.sock
elif [ -S /var/run/mysqld/mysqld.sock ]; then
mysock=/var/run/mysqld/mysqld.sock
elif [ -S /Applications/MAMP/tmp/mysql/mysql.sock ]; then
mysock=/Applications/MAMP/tmp/mysql/mysql.sock
elif [ -S /tmp/mysql.sock ]; then
mysock=/tmp/mysql.sock
fi
# Grab information from Joomla 2.5/3.x configuration.
sitename=$(grep '$sitename =' ./configuration.php | cut -d \' -f 2 | sed 's/ /_/g')
sitenameclean=$(grep '$sitename =' ./configuration.php | cut -d \' -f 2)
database=$(grep '$db =' ./configuration.php | cut -d \' -f 2)
dbuser=$(grep '$user =' ./configuration.php | cut -d \' -f 2)
password=$(grep '$password =' ./configuration.php | cut -d \' -f 2)
host=$(grep '$host =' ./configuration.php | cut -d \' -f 2)
prefix=$(grep '$dbprefix =' ./configuration.php | cut -d \' -f 2)
versr=$(grep 'RELEASE =' ./libraries/cms/version/version.php | cut -d \' -f 2)
versd=$(grep 'DEV_LEVEL =' ./libraries/cms/version/version.php | cut -d \' -f 2)
verss=$(grep 'DEV_STATUS =' ./libraries/cms/version/version.php | cut -d \' -f 2)
@psyray
Copy link
Author

psyray commented Dec 6, 2017

Usage :

  1. Put jfindfiles.sh & jfunctions.sh in the Joomla root dir
  2. Make them executable chown +x jf*.sh
  3. Execute ./jfindfiles.sh
  4. Put the file xxx-unused.txt in the images folder with the jdeletefiles.sh
  5. Make jdeletefiles.sh executable chown +x jdeletefiles.sh
  6. Execute it ./jdeletefiles.sh xxx-unused.txt
  7. Files are deleted ;)

Thanks to @renekreijveld & @wehsemann
Source script is here

@quodo
Copy link

quodo commented Jun 10, 2019

Thank you so much!

With the usage comment by user psyray and his files I was able to get it to work (I'm new to ssh).
I learned

  • SSH access is needed for this (found on webhost help pages how to do that)
  • putty (putty.org) is a nice program for SSH access
  • with ls -lh there is a list of who owns the files and what the permissions are (the user that uses Putty has to have execute rights) and via ftp the chmod command changes the permissions

There were three errors for the jfunctions.sh file

grep: ./libraries/cms/version/version.php: No such file or directory
grep: ./libraries/cms/version/version.php: No such file or directory
grep: ./libraries/cms/version/version.php: No such file or directory

just because the version directory does not exist in in my Joomla 3.9.6 installation. That is not a problem at all though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment