Unlinked images clean up

Hi

does anybody know of a free programme that identifies and removes (to another destination) all unlinked images in the server?

I have over a thousand that are no longer used.

Thanks

What do you mean by unlinked? 404 images?

My God! You’re everywhere. I was hiding out of shame following our last exchange!

I’ve uploaded hundreds of images I no longer need for the pages as they are. Many of those images are not linked to any page. I would like to identify them, bunch them together and FTP them out of the server

I’m unable to add to my last message.

I wanted to say that I’ve googled and googled and found nothing free.

You should be able to edit your post by clicking on the pencil icon.

I did but for some reason the reply area remained blank instead of containing the already posted text.

Only happens to me!

1 Like

There have been a few glitches in the system recently. If you run into the same problem again, let us know and we’ll investigate further.

Will do.

Your glitches are nothing compared to mine!

I found this on-line but it’s all Chinese to me!

#!/bin/bash

if [ $# -eq 0 ]
  then
    echo "Please supply path to search under"
    exit 1
fi
MYPATH=$1

find "$MYPATH" -name *.jpg > /tmp/patterns
find "$MYPATH" -name *.png >> /tmp/patterns
find "$MYPATH" -name *.gif >> /tmp/patterns
find "$MYPATH" -name *.js >> /tmp/patterns
find "$MYPATH" -name *.php >> /tmp/patterns

for p in $(cat /tmp/patterns); do
    f=$(basename $p);
    grep -R $f "$MYPATH" > /dev/null || echo $p;
done

Could someone tell me if this would do the job, and if so, what do I do with it, please?

I found this on-line but it’s all Chinese to me!

I’m after a method to clean up the server from unused, unlinked images that built over time. I would like to identify them, lump them together, and FTP them out of the server.

I don’t understand the code below and would be grateful if someone could tell me if this would do the job, and if so, what do I do with it, please?

#!/bin/bash

if [ $# -eq 0 ]
  then
    echo "Please supply path to search under"
    exit 1
fi
MYPATH=$1

find "$MYPATH" -name *.jpg > /tmp/patterns
find "$MYPATH" -name *.png >> /tmp/patterns
find "$MYPATH" -name *.gif >> /tmp/patterns
find "$MYPATH" -name *.js >> /tmp/patterns
find "$MYPATH" -name *.php >> /tmp/patterns

for p in $(cat /tmp/patterns); do
    f=$(basename $p);
    grep -R $f "$MYPATH" > /dev/null || echo $p;
done

This is bash script, not PHP.
I’m not expert in bash but seems like it finds all the jpg/png/gif/js/php files inside path specified in first command line argument, stores names of that files in file /tmp/patterns. Then it loops through each line of /tmp/patterns and deletes corresponding file.

If you want to run it save it as text file (for example remover.sh), make executable:

$ chmod +x remover.sh

and run:

$ ./remover.sh /path/to/clear

Thanks megazoid

Goes to show how much I understand of Chinese…

I thought the idea, from what I read, and that is what I need, was to isolate images that are not linked to any file in the server.

can it be adjusted to do that?

Oh, I was wrong. It doesn’t delete images. It actually prints names of the files not mentioned in other php/js files. So, looks like you can get list of orphan files with it.

I’ve merged the two topics, as they’re on the same subject and both belong in the same category.

Hi

Thanks megazoid

Ok, so let’s see if I understood your original post. is this what I have to do?

#!/bin/bash

http://pintotours.net=$1

find "http://pintotours.net" -name *.jpg -exec basename {} \; > /tmp/patterns
find "http://pintotours.net" -name *.png -exec basename {} \; >> /tmp/patterns
find "http://pintotours.net" -name *.gif -exec basename {} \; >> /tmp/patterns

for p in $(cat /tmp/patterns); do
    grep -R $p "$http://pintotours.net" > /dev/null || echo $p;

$ chmod +x remover.sh
and run:

$ ./remover.sh /path/to/clear
done

This seems like a rather dangerous thing.

Bash scripts can be very powerful and unless one really understands the syntax they can be difficult to figure out what they do.

If you are better with PHP you could write your own scripts.

  • recursively go through all files that would link to images and parse out the src values
  • recursively go through all CSS files to get back-ground image paths / names
  • recursively go through all folders that contain images

then compare and contrast

Hi Mittineague

I’m afraid I’m not good with anything! And know nothing about php…

There are over a thousand images. Could that be done automatically?

In fact, by now, all I want to do is to separate the linked ones from the non-linked. I don’t want to delete anything.

I’ve been looking around and cme up with some more code but don’t know where to put it.:

This should save to a new file, say:

$ ./remover.sh /path/to/clear "http://pintotours.net" > files_to_remove

But wher would all this go? Uploaded into the server, or controlled from the PC (Windows)?

With that many I would probably want to put the data into two tables, eg. ‘used_image_files’ and ‘all_image_files’

Sure, the right regex could do it easily enough.

AFAIK you would need to run it from the Command Line.

Chances are you do not have this for your live site, but you could download the site to your computer, work with it, then FTP delete orphaned image files from your live site.
Could be a real chore, but not impossible. I think most people just leave them cluttering up their space.

* an after-thought, if you are calling in images from a database you will need to get those values too.

Do you mean php? I would not know where to start. Could you help?

I’ve been trying that already, but after 20 years without messing around with dos I’m having trouble getting into the public_html directory (which appears to be NOt a directory, or my DOS memory is worse than I thought…

The images are in folders (about 7 of them). The problem is working out which are linked. For that i think I need some sort of script