If you need to narrow down a list of folders in a directory to the set that you need to either archive or cleanup from a linux server, here's a quick command.

First, add the list of folders that you know you want to keep to a file named file-list.txt. All other folders will be printed from this command.

$ ls -hal | grep ^d | grep -Fxvf file-list.txt

What the flags mean:

  • -mtime +7 More than seven days old

  • -maxdepth 1 Only search the current directory, not recursively

  • -F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

  • -x, --line-regexp Select only those matches that exactly match the whole line.

  • -v, --invert-match Invert the sense of matching, to select non-matching lines.

  • -f FILE, --file=FILE Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing.

If you want get a count to make sure the all folders listed in file-list.txt exist in the current directory, here's another command.

$ ls -hal | grep ^d | grep -Fxf file-list.txt | wc -l