Find and Replace

  • To Find Files:
    • {sh} grep -RilZP pat dir ; OR
      • R — recursive, follow symlinks (r to not follow)
      • i — case insensitive
      • l — print file names, not matches
      • Z — Use NUL to separate output not \n
        • By default, other commands separate on all whitespace (spaces and newlines), include spaces in paths. If we use NUL across all commands, we can avoid this
      • P — Use Perl Regexp (use whatever pat matching syntax is preferred)
    • {sh} rg -l0 pat dir
      • l — print file names, not matches
      • 0 — NUL separated
      • Uses rust ERE (no lookarounds, back references) by default; enable full perl regexp with P
  • To replace:
    • {sh} rg -l0 regexp dir | xargs -0 sed 's/find-pat/replace-with-pat/g'
      • xargs
        • -0 — NUL separated
      • sed
        • ssubstituteg` globally
        • Access ERE-style regrexp with -E
  • See also: