1

I am trying to get a vertical list from ls, that is recursive but leaves a out a single directory called node_modules.

I've used:

ls -lt -h -R

This gets me a very long list that is vertical, human readable and recursive. But it has the directory node_modules included and that just has too many files and directories.

  • 2
    Add -I node_modules – Stéphane Chazelas Apr 27 '14 at 14:16
  • find . -name node_modules -prune -o -print. – Mikel Apr 27 '14 at 14:53
  • Mangle the output from ls with grep or some of the other filters – vonbrand Apr 27 '14 at 18:08
2

You can either use -I to ignore the directory as Stephane suggested or, better, avoid using ls altogether. Just use find which is far better for this kind of thing and can deal with strange file names gracefully:

find . | grep -v node_modules

Or, for a safer approach that can deal with newlines in file names correctly:

find . -not -regex ".*node_modules.*"

or, for non-GNU find:

find . ! -regex ".*node_modules.*"

If you can have node_modules as part of a file name and don't want to skip that one, use this instead:

find . -not -regex "./node_modules.*"
  • \! -name 'node_modules' would be okay (instead of the regex) as it seems that's the full name of the thing to leave out. – gniourf_gniourf Apr 27 '14 at 14:53
  • That is excellent, thank you very much. I need it to display in a word document, I would like it nice and neat because its going in a uni report. – Callum Linington Apr 27 '14 at 15:05
  • @gniourf_gniourf no it's not, that will remove the directory but return the list of the files contained in that directory which is exactly what the OP wants to avoid. – terdon Apr 27 '14 at 16:30
  • Oh my bad, I misread the OP. Then you can use -prune to entirely cut the branch: find . -name 'node_modules' -prune -o -true (but this also outputs the dir name—but doesn't recurse into) or find . ! -name 'node_modules' -o -name 'node_modules' -prune -false to not recurse into and not show it. – gniourf_gniourf Apr 27 '14 at 16:37
  • @gniourf_gniourf yes, I know about -prune :) How is that better than my approach though? It seems absurdly complex for such a simple operation. – terdon Apr 27 '14 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.