Exclude With Grep
[#excluding-single-pattern]Excluding a single pattern[#excluding-single-pattern]
When using the [.inline-code]grep[.inline-code] command, to invert the search and return lines that do not include a specific pattern or exclude a specific string, you can use the [.inline-code]-v[.inline-code] flag.
For example, to search for all lines in a file that do not contain the word "apple", you can use the following command:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d321507_6427349e1bf2f01b3af733d4_CsgCJVkmN7QLuf453SE0jczbflX8yqaGV4w_ImtrzzHN8B2nRR2cYwI5f9BXpCkaYuf84oIIYbdbuT7vCxcQBsUGiWhi0DaxEp9Nmqgeu5mLHAajXJdgviH_8PdB4zoYW7tyVxs_DvTzlMXijcltTGJeI8z5G9qhdPLAhFm7I963dso-tIgxm21pMe76Kg.png)
Alternatively, if you want to return lines that do not include a specific pattern as a whole, which means a pattern surrounded by non-word characters, such as spaces, punctuation, or the start/end of a line, you can use the [.inline-code]-w[.inline-code] flag.
For example:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d321517_6427349e1bf2f061d6f733df_lJj394DmnRFI4AMhxDqcCQvAOoQimbCzjetnE4KFLuMzhJzyzjNjIHUyWeLKhqpOz9i3G6jdO0grNZ-gZYTOb252FSMIos1ATSm0CVZb1leZ9MRa3lDSvNz2wJZWZFglM3IhGVYy77a9Bx6IuwGQcjcvPPe8XImYbbwOyGhrW3dCcyBo50mDfQo2cWvb_A.png)
[#excluding-multiple-patterns]Excluding multiple patterns[#excluding-multiple-patterns]
To return lines that specifically do not include multiple patterns (see [.inline-code]grep[.inline-code] multiple strings for the opposite), you can use the [.inline-code]-e[.inline-code] flag combined with the [.inline-code]-v[.inline-code] flag.
For example:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d321511_6427349e1bf2f0097ff733e2_77ERKjGKffLJbRBH3aYggO9vrdaJadS1I-eK5PyvvBD57S6Dtc_aH9PDkED7cF5Qe8xuMcIYva4AiOIQB0Wrt8rmKMPdmKljDtqVGGiXMjiqoK2EUz2sE4KsSC08bc_IrFJLw3XhF4dL1ERSiSALq1UQGOn0TgRUEJfkQIRjZVvVcXmdJuNvl_uyIWREPA.png)
Alternatively, you can use the [.inline-code]-E[.inline-code] flag combined with the [.inline-code]|[.inline-code] symbol:
For example:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d321514_6427349e1bf2f0c9f5f733de_BrAAu24w7Yj0cwdI562ehXRlwYcpkR_ClVqXB-4SNPApeZKKorQLtrrqtWAB8f-sPy35hJ2aOUs175638aCV93ugzdt1BUWFANeDvA6mwfAZGLf3ZQznOg5Y_Bqoy5BwfNTIBhz3--TmGhVmzJBKgJ7Hd3k-ZjHtx4sKZ7b-2B8qDx_Y-tFj97bFRnchPg.png)
[#excluding-files-and-directories]Excluding Files and Directories From a Search[#excluding-files-and-directories]
As the opposite of including specific directories in a [.inline-code]grep[.inline-code], It is sometimes necessary to exclude certain files and directories from a search when using the recursive flag [.inline-code]-r[.inline-code] flag.
[#excluding-files]Excluding files[#excluding-files]
To exclude one or more files that match a glob pattern, you can use the [.inline-code]--exclude[.inline-code] flag.
For example:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d32150b_6427349e1bf2f08784f733d3_h2g-5XjZuETs3cLPLZQf39wbxsX6PWEVMscu0wEhDXWyrWA5CoM5XC9NNtaNyRM4hMEY1NOqVXKNYoOtYYSVsKjEle603K-crZQaxKViDV79p2_7DK3pA_VHg9TTpc2Dqh3kxNOw3tY2O2v58gcHWaMHqq87C3GnPw1r8nByStyw_W2aOb7ZfCRHhP6pMA.png)
[#excluding-directories]Excluding directories[#excluding-directories]
To exclude one or more directories that match a glob pattern, you can use the [.inline-code]--exclude-dir[.inline-code] flag.
For example:
![](https://cdn.prod.website-files.com/64b7506ad75bbfcf43a51e90/64c96f28d48f3e1b7d32151b_6427349e1bf2f0b045f733dc_FD_Vw718GefcrAJTPwlGWbLyY2DQmjyIdyj6nS74to3lZyLLTjuKzmgA3kkX2yqaoE-MvAAzy3BOssbMWT-LBN1_9QMTJVCI_ZZY2Je6XU7huBk8cy3zcPdx84ew1J-VDtbJTFhmw6Mo1_1Lt_LRuEgVmlpfITNOlLc74s4t3wOQ9Z4FEIuDJUc_z1Zo9g.png)