Want to delete the junk files from a directory which are not listed in a TEXT file

Hello Everyone,

I want to delete the image files from a directory, which are not listed in a TEXT file.

The directory contains large number of image files (in millions) required / not required. I want to delete the image files which are "not required".

I have generated a Text file having only the required files by querying the database. I have also tried to create a list of files from the directory where the large number of files are present but it is taking time.

Is there any way to delete the junk files from a directory, which are not listed in an input TEXT file?

Please advice.

Thanks in advance.

Praveen

Untested, use at your own risk:

to_delete=""
file_good=goodones.txt

for f in *
do 	[ -f "$f" ] && \
		grep -q "$f" "$file_good" || \
		to_delete+=" $f"
done

rm "$to_delete"

hth

Thanks for the reply.

I have tried it but it is not working. Though it appends the file names which are not listed in the input file, it is unable to delete the large number of files.

Try creating a reverse list first of files to be deleted:

ls | comm -23 - textfile

The files in "textfile" need to be in sorted order
Then check if it is correct and then delete those files...

---
@sea: Doing a grep in a loop for millions of files in a file with millions of file names is going to take for ever, also += for string operations is bash / ksh93 /zsh only.

3 Likes