Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find unused and used content files in your Joomla website
#!/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@renekreijveld.nl
#
# 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
# 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'`
result1=`grep -c "$file2" ${dbdump}`
result2=`grep -c "$file3" ${dbdump}`
if [[ $result1 = 0 ]]; then
if [[ $result2 = 0 ]]; then
echo $file2 >> ${notusedfile}
else
echo $file2 >> ${usedfile}
fi
else
echo $file2 >> ${usedfile}
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}"
@javigomez

This comment has been minimized.

javigomez commented Aug 8, 2014

This is a great idea! ^_^ thanks for sharing Rene

what is this ". ${MYPATH}/joomlafunctions" ?

@renekreijveld

This comment has been minimized.

Owner

renekreijveld commented Sep 8, 2014

Hi Javier, In order for this script to work, you need to install the script joomlafunctions as well" https://gist.github.com/renekreijveld/9663741
The line ". ${MYPATH}/joomlafunctions" is a sort-of include statement for Bash scripts. The joomlafunctions script figures out what Joomla version is running and sets some variables accordingly.

@headgr

This comment has been minimized.

headgr commented Mar 5, 2017

Won't parse configuration file.
CentOS 7, Joomla 3.6.
Manually typed database name works.

@HerKle

This comment has been minimized.

HerKle commented Apr 16, 2017

Hi Rene, found this forum by Google in search for finding pics in joomla's image folder which are unused. Have no idea how to install your code to make it work. So could you just step back from prof talk and tell a newbie how to use / where to put the code you provide here? Since it's the only hit for my search… Thanks, Herbert

@wehsemann

This comment has been minimized.

wehsemann commented Sep 5, 2017

Hello @HerKle,
I tried it out and after some modifications it is working now for me.

So here my Instruction:

  1. in "jfindfiles" above the part:

result1=grep -c "$file2" ${dbdump} result2=grep -c "$file3" ${dbdump} if [[ $result1 = 0 ]]; then if [[ $result2 = 0 ]]; then echo $file2 >> ${notusedfile} else echo $file2 >> ${usedfile} fi else echo $file2 >> ${usedfile} fi

is not working for me. I got an error. I changed the code to:

if grep -c "$file2" ${dbdump} > 0 then echo $file2 >> ${usedfile} else if grep -c "$file3" ${dbdump} > 0 then echo $file3 >> ${usedfile} else echo $file3 >> ${notusedfile} fi fi

  1. place the file "jfindfiles" in your wwwroot directory of your joomla page and give the file execution rights.
  2. Place the file "jfunctions" (https://gist.github.com/renekreijveld/9663741) in your wwwroot directory of your joomla page and give the file execution rights.
  3. excecute the "jfindfiles" in the command line of the server with:
    ./findfiles
  4. You should not run it in the live enviroment when many users are on the page because the database dump take some resources.
  5. after the script is finished you have to text files with the result.
  6. remove the both scripts and the text files from the server finally, after downloading it.
@psyray

This comment has been minimized.

psyray commented Dec 6, 2017

Hi,

Thanks for this very useful script.

Here is my gist with @wehsemann modifications, with a script to delete files using the unused text file and with the howto

@bolando

This comment has been minimized.

bolando commented Apr 11, 2018

Hello,
I am not experienced in Linux, but I need to run this script with my Joomla.
So I launched it and got to the point that it works, but I get an error "Error creating database dump."

What can be the causes? How to fix it? I have diskspace available, so it's not the problem.

My system is Red Hat Enterprise Linux Server release 6.7 (Santiago)

Thanks

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