Grep does not search for hidden folders

Source: Internet
Author: User

By default, grep searches for all files, including files in hidden folders.

For example, use the following command to search for the mediabutton string. The search result contains files in the hidden folder.

Grep-Nr mediabutton *

If the code is managed using SVN, each folder contains a hidden. SVN folder that contains control information on the code version, which will increase over time. If you only search by default, many search results are under the svn directory. For us, there is no value in the content, and it will take a lot of time.

The -- exclude-Dir = parameter of grep is used to exclude a directory that does not contain the directory after the equal sign. Therefore, we can use this parameter to remove the hidden directory of. SVN.

-- Exclude-Dir =:

1.If you have GNU grep, it shoshould work like this:

grep --exclude-dir=".svn"

For example, find the current directory and all code containing the "mediabutton" string in the subdirectory and print the row number.

Grep-NR -- exclude-Dir = ". SVN" mediabutton *

Or

Grep-NR -- exclude-Dir = \. SVN mediabutton *

-- Exclude-Dir = the directories to be excluded can be enclosed in double quotation marks or directly connected to the directory name, but special characters must be expressed with escape characters, such as "." In ". SVN ".".

2.If happen to be on a UNIX system without GNU grep, try the following:

This method uses pipelines for double-layer "filtering". The second grep uses the-V option, that is, reverse matching, to print out unmatched rows.

grep -R "whatever you like" *|grep -v "\.svn/*" 

3.If the -- exclude-Dir = parameter is input every time, is it very slow and error-prone, reducing efficiency?

But don't worry. we can add the -- exclude-Dir = parameter to the configuration file ~ /. Bashrc (this is the current user configuration file of BASH)
To simplify operations and improve efficiency.

In ~ /Add the following command at the end of the bashrc file:

export GREP_OPTIONS="--exclude-dir=\.svn" 

Save and execute source ~ /. Bashrc or .~ /. Bashrc to make the modification take effect.

Then, execute the following command when searching:

Grep-Nr mediabutton *

By default, the grep_options option is loaded without searching for the. SVN directory.

It should be noted that the keyword grep_options cannot be changed to another one, otherwise the system will not regard it as an option parameter of grep.

Refer:

Http://stackoverflow.com/questions/1491514/exclude-svn-directories-from-grep

Http://hi.baidu.com/jasonlyy/item/6a9594e7438e97a1c00d755c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.