Perfect use of Find+grep for global code search under Linux

Source: Internet
Author: User
Tags php source code sublime text

  Zhanhailiang Date: 2014-10-11
Background

There are a number of handy graphical tools under window to enable global search, but linuxserver is a higher threshold because of the use of command-line operations.

The first of these methods

First, For the simplest example, we often need to look at the definition of a function while reading the code, but the general open source project code is quite large, and you can use Find+grep to easily find the definition of the function, for example, below. I want to search the PHP source code for the definition of the Is_array method. For example, the following:

[[email protected" standard]#  Span style= "COLOR: #ff0000" >find. -name ' *.c '-exec grep-hna ' Is_array ' {} \;  ./basic_functions.c:2540:zend_begin_arg_info (Arginfo_is_array, 0)./basic_ Functions.c:3049:php_fe (Is_array,arginfo_is_array)./type.c:283:/* {{{proto bool Is_array (mixed Var)./type.c:285: Php_function (Is_array)  

Here are two points to note: 1. Find when using-exec ";" is required, such as the following two methods will cause "find:missing argument to '-exec '", in which the first command error is due to the fact that ";", while the second command error is due to run the statement in the Shell ";" It is thought to be a terminator, not a-exec, so it must be escaped when used.

[[email protected" standard]# find-name ' *.c '-exec Grep-hna ' Is_array ' {}  find:missing argument to '-exec '  [[email protected" standard]# find. -name ' *.c '-exec grep-hna ' Is_array ' {};  find:missing argument to '-exec '  
[[email protected] standard]# find--help actions:-delete-print0-printf format-fprintf FILE format-print       -FPR Int0 file-fprint File-ls-fls file-prune-quit      -exec command;-exec command {} +-OK command;       -execdir command;-execdir command {} +-okdir command;

2. As in the above example, when using grep, I added-hna, which output file path, line number, and corresponding text content:

[email protected] standard]# grep--help  -N,--line-number         print line number withoutput LINES      --line-buffered       flush output on every line  -H,       --with-filename print thefilename for Each match  -A,                --text equivalent to--binary-files=text
A different approach

Given the need to view the source code for a view project, it would be perfect to install the Ctags tool on the server to match vim, please refer to the "Sublime Text 2 installation ctags plug-in" similar principle to install the configuration on Linux, Strong technical flow of students using this method.

Perfect use of Find+grep for global code search under Linux

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.