I would list the files in the filesystem, remove those that exist in your set of files to be kept, and delete the remainder.
Here I've used NULL-terminated filenames throughout so that there is no confusion with xargs
between a filename containing spaces and its space separated parts:
find /mnt/cache/vfs/cf -type f -print0 |
LC_ALL=C sort -z |
LC_ALL=C comm -z -23 - <(LC_ALL=C sort list-of-files-to-keep.list | tr '\n' '\0') |
xargs -0 printf '%s\n' {}
Replace printf '%s\n'
with rm --
when you are ready to perform the deletions.
The comm
command takes two sorted files and compares them line by line. The first column of output is entries only in the first file, the second is entries only in the second file, and the third is entries in both files. The -1
, -2
, and -3
qualifiers inhibit output of the corresponding column, so our comm -23
will output lines that are present only in the first file (-
, i.e stdin).
I've forced the locale to C
so that sort
and comm
work in a consistent manner with each other (comm
requires sorted input), and also so that every line is sorted in deterministic manner (some locales sort sets of characters the same, so the characters in such a set may be ordered in an inconsistent way).