It just took me way too long to solve this, so I’m going to write it up quickly:
The Problem
- I have a file containing the basenames of spreadsheets that I want to keep in a directory structure. I want to delete all the other spreadsheets.
- There are levels of nested directories and the files are distributed among them
- The file names, and the directory names contain spaces (don’t look at me like that, it’s other people’s work on Windoze — at least I’m using bash
- I’m using bash on Linux, so I have proper find, etc.
A Solution
#!/bin/bash modelfiles=../files-to-keep.txt find . -name \*.xls\? | while read f do if ! grep -q "${f##*/}" $modelfiles then rm "$f" fi done
I’m sure there are more efficient ways. Contact me if you have ideas. Meanwhile I’ve got to get back to work so I’m sticking with “it works”.
UPDATE: that didn’t work when I tried to re-run it just now. So switched from grep -v to if ! …