5

I have recently discovered ack and ack -ir --ignore-dir={node_modules,dist,.git} <search-term> works great for most things but this

---node_modules/
---------------project1/
-----------------------node_modules/
---------------project2/
-----------------------node_modules/

I would like to search all files under the "root" node_modules and excluding all internal ones.

Note: if I run find . -type f | ack -v 'node_modules|.git|dist' under the root node_modules folder, I get a proper file list. this happen because find . gives out relative paths. Any way to feed this list to ack ?

qballer
  • 2,033
  • 2
  • 22
  • 40

2 Answers2

6
ack -i <search-term> ./node_modules/*

should do what you want. If you want to include hidden files and folders too, set shopt -s dotglob (assumes Bash) first.


Background information:

ack has many rules for ignoring files that you don't typically want to be searched built in.

As of at least version 2.14, this includes directories named .git and node_modules, at whatever level of the hierarchy (ack searches recursively by default, though you may add -r / -R / --recursive to make that explicit).
To see the complete list of directories / files that are ignored by default, run ack --dump.

By using globbing (pathname expansion) to let the shell expand pattern ./node_modules/* to a list of actual items inside ./node_modules, the ignore rule for node_modules is bypassed for those explicitly passed items.

mklement0
  • 382,024
  • 64
  • 607
  • 775
6

node_modules is in the default --ignore-dir list in ack for javascript.

You can disable this using the --noignore-dir option to un-ignore node_modules.

  • directly on the command line:

    % ack --noignore-dir=node_modules -i <search-term>
    
  • in your .ackrc file:

    # in .ackrc
    --noignore-dir=node_modules
    

    which affects all searches:

    % ack -i <search-term>
    
spazm
  • 4,399
  • 31
  • 30
  • I'm kind of answering the wrong question, but I'm'ma leave it here so I find this later.... – spazm Jul 11 '19 at 17:15
  • 1
    If you want to specifically search in that directory, you can specify it, and then ack won't ignore it, like so: `ack somestring node_modules` – Andy Lester Aug 10 '19 at 01:11