1

I need to find files matching the following criteria:

  • extension is md
  • within folder . and all subfolders
  • exclude all node_modules folders
  • sort by modified date, latest first across all results

I tried this but I guess my exclude for node_modules is wrong as I still get results including node_modules folders when including the fragment \( ! -iname "node_modules" \):

find $PWD -name "*.md" \( ! -iname "node_modules" \) -print0 | xargs -0 ls -laht
0

1 Answer 1

3

This seems to work but it is very slow when having a lot of subfolders. So I'm open for better solutions:

find "$PWD" -name "*.md" ! -path '*/node_modules/*' -print0 | xargs -0 ls -laht

Update: Based on the hint from Kamil Maciorowski regarding -prune, I came up with this solution which is much faster now:

find "$PWD" -name "node_modules" -prune -o -name "*.md" -print0 | xargs -0 ls -laht -P 1
4
  • 1
    No time for a good quality answer now, therefore just a hint: research -prune. Sep 3, 2019 at 20:05
  • 1
    Nice. Two issues: (1) $PWD should be double-quoted. (2) When there are many results, xargs may run two or more ls processes in sequence. Each ls will sort independently, so most likely the overall result will not be sorted as a whole. Sep 3, 2019 at 20:37
  • Thanks, I mitigated (2) by adding -P 1 as xargs option. Sep 4, 2019 at 9:39
  • I think -P 1 in your code will be passed as an option to ls, am I wrong? If you move it before ls then it will be an option to xargs. Even if it changes anything (1 is the default value in GNU xargs, I don't know macOS), it doesn't solve the problem I mentioned. (You may be aware, you used the word "mitigated" instead of "solved"). A way to make a single ls "sort" (semi-)arbitrarily many files is to copy/hardlink/symlink them into a separate directory and invoke ls for the entire directory (with -L if needed). There may be "maximum number of files per directory" limit though. Sep 4, 2019 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .