I want to traverse all subdirectories, except the node_modules directory.
I want to traverse all subdirectories, except the node_modules directory.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
SOLUTION 1 (combine
findandgrep)The purpose of this solution is not to deal with
grepperformance but to show a portable solution : should also work with busybox or GNU version older than 2.5.Use
find, for excluding directories foo and bar :Then combine
findand the non-recursive use ofgrep, as a portable solution :SOLUTION 2 (using the
--exclude-diroption ofgrep):You know this solution already, but I add it since it’s the most recent and efficient solution. Note this is a less portable solution but more human-readable.
To exclude multiple directories, use
--exclude-diras:--exclude-dir={node_modules,dir1,dir2,dir3}SOLUTION 3 (Ag)
If you frequently search through code, Ag (The Silver Searcher) is a much faster alternative to grep, that’s customized for searching code. For instance, it automatically ignores files and directories listed in
.gitignore, so you don’t have to keep passing the same cumbersome exclude options togreporfind.