remove file but exclude all files in a list

The rm command is commented out so that you can check and verify that it's working as needed. Then just un-comment that line.

The check directory section will ensure you don't accidentally run the script from the wrong directory and clobber the wrong files.

You can remove the echo deleting line to run silently.

#!/bin/bash

cd /home/me/myfolder2tocleanup/

# Exit if the directory isn't found.
if (($?>0)); then
    echo "Can't find work dir... exiting"
    exit
fi

for i in *; do
    if ! grep -qxFe "$i" filelist.txt; then
        echo "Deleting: $i"
        # the next line is commented out.  Test it.  Then uncomment to removed the files
        # rm "$i"
    fi
done

This python script can do this:

#!/usr/bin/env python3
import os
no_remove = set()
with open('./dont-delete.txt') as f:
     for line in f:
         no_remove.add(line.strip())

for f in os.listdir('.'):
    if f not in no_remove:
        print('unlink:' + f ) 
        #os.unlink(f)

Important part is to uncomment the os.unlink() function.

NOTE: add this script and dont-delete.txt to your dont-delete.txt so that they both are on the list, and keep them in the same directory.


Here's a one-liner:

comm -2 -3 <(ls) <(sort dont_delete) | tail +2 | xargs -p rm
  1. ls prints all files in the current directory (in sorted order)
  2. sort dont_delete prints all the files we don't want to delete in sorted order
  3. the <() operator turns a string into a file-like object
  4. The comm commands compares two pre-sorted files and prints out lines on which they differ
  5. using the -2 -3 flags causes comm to only print lines contained in the first file but not the second, which will be the list of files that are safe to delete
  6. the tail +2 call is just to remove the heading of the comm output, which contains the name of the input file
  7. Now we get a list of files to delete on standard out. We pipe this output to xargs which will turn the output stream into a list of arguments for rm. The -p option forces xargs to ask for confirmation before executing.