7

My title may be a bit oddly worded, so here's my situation: I have a bunch of directory paths, e.g.

/a/b
/a/b/c
/a/b/c/d
/a/e/f/g/h
/a/e/f/g/h/i/j/k/l
/a/e/f/g/m/n/o
/a/e/f/g/m/n/p

and I want to filter out all lines that are child paths of an entry that already exists in the list, e.g.

/a/b
/a/e/f/g/h
/a/e/f/g/m/n/o
/a/e/f/g/m/n/p

The directory paths are obtained from find, so they should reliably be in top-down order. Solutions for parsing as an array or multi-line string are both welcome.

Sam
  • 198
  • @cas Editted to strike out the bit about assumed order. The first codeblock is the example input, the second codeblock is the example desired output. – Sam Apr 20 '21 at 16:01
  • @cas The criteria is that they are to be removed if they are "child paths of an entry that already exists in the list," /a/b is in the list and is the parent directory of /a/b/c and /a/b/c/d, so /a/b/c and /a/b/c/d are to be removed. Already have an accepted answer, though, so feel free to move on to another post. Thanks for trying to help, though, and sorry if my question wasn't sufficiently clear. – Sam Apr 20 '21 at 16:18
  • 2
    Can you use find's -prune arg to not have this problem in the first place? (It can be a bit tricky to put it in the right place to trigger on the conditions you want.) – Peter Cordes Apr 21 '21 at 07:09
  • Are you just trying to find files, then, find has -type f for that.... (it's not clear from the question). – djsmiley2kStaysInside Apr 21 '21 at 10:36
  • 1
    @PeterCordes wow, I was not aware of that option. That does seem to do what I was looking for. – Sam Apr 21 '21 at 14:29