Skip to content

Instantly share code, notes, and snippets.



Last active Sep 3, 2019
What would you like to do?
Find and delete unused joomla images
if [ -z "$1" ]; then
echo -e "Usage: $(basename $0) FILE\n"
exit 1
if [ ! -e "$1" ]; then
echo -e "$1: File doesn't exist.\n"
exit 1
while read -r line; do
[ -n "$line" ] && rm -- "$line"
done < "$1"
# jfindfiles -- Find used and unused content files in your Joomla website
# This scripts supports Joomla versions 2.5 - 3.x
# Copyright 2014 Rene Kreijveld -
# 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}/
# version
# Setup Vvariables
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
echo "Database dump ${database}.sql created."
echo "Error creating database dump."
exit 1
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'`
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}
echo $file3 >> ${notusedfile}
# 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}"
# jfunctions - general bash functions for use in Joomla bash scripts
# Supports Joomla versions 2.5 - 3.x
# Copyright 2014 - 2016 Rene Kreijveld -
# 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
# Check if configuration.php exists.
if [ ! -e ./configuration.php ]
echo ""
echo "File configuration.php not found. Are you at the root of the site?"
echo ""
exit 1
# Find MySQL Socket
if [ -S /var/lib/mysql/mysql.sock ]; then
elif [ -S /var/run/mysqld/mysqld.sock ]; then
elif [ -S /Applications/MAMP/tmp/mysql/mysql.sock ]; then
elif [ -S /tmp/mysql.sock ]; then
# 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)

This comment has been minimized.

Copy link
Owner Author

@psyray psyray commented Dec 6, 2017

Usage :

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

Thanks to @renekreijveld & @wehsemann
Source script is here


This comment has been minimized.

Copy link

@quodo 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 ( 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 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