How to Export Image Filenames from WordPress Database

exportMySQL

I have a WordPress install that's using about 10,000 images. I'd like to export all filenames used in WordPress to a file, filenames.log

filenames.log would look like this:

car.jpg
boat.jpg
milf.jpg
car2.jpg
other.jpg

etc…

The reason I'm doing this is that I have orphaned images in /uploads, and I want to run a delete operation and exclude these filenames that are actually in use.

What command could I run to generate such a file?

Thanks!

Best Answer

Pure MySQL, be sure to correct it for your desired filepath /tmp/filenames.log: (Thanks Phil for the select query)

"SELECT meta_value AS filename
FROM wp_posts p, wp_postmeta pm
WHERE p.id = pm.post_id
AND p.post_type = 'attachment'
AND meta_key = '_wp_attached_file'
INTO OUTFILE '/tmp/filenames.log'
LINES TERMINATED BY '\n';"