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
Alexander Zeitler
  • 1,253
  • 2
  • 14
  • 18

1 Answers1

2

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
Alexander Zeitler
  • 1,253
  • 2
  • 14
  • 18
  • 1
    No time for a good quality answer now, therefore just a hint: research `-prune`. – Kamil Maciorowski Sep 03 '19 at 20:05
  • 1
    Nice. Two issues: (1) `$PWD` should be [double-quoted](https://unix.stackexchange.com/a/131767/108618). (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. – Kamil Maciorowski Sep 03 '19 at 20:37
  • Thanks, I mitigated (2) by adding `-P 1` as `xargs` option. – Alexander Zeitler Sep 04 '19 at 09: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. – Kamil Maciorowski Sep 04 '19 at 10:02