1. 查找在哪些文件中含有某个字符串(模糊匹配)
命令格式:find +目标目录 + -name + 目标文件名 | xargs grep + 目标字符串
注:
(1) 文件可以模糊匹配 [“*xxx”]
(2) 其中文件名和目标字符串都是字符串,双引号可加可不加
例:在当前目录下的查找含有字符串USB_DEVICE_STACK的所有的Kconfig文件
xxxxxx@srv-pad-compile5 $ find . -name "Kconfig" | xargs grep "USB_DEVICE_STACK"
结果: ./drivers/usb/device/Kconfig: select USB_DEVICE_STACK ./subsys/usb/Kconfig:menuconfig USB_DEVICE_STACK ./subsys/usb/Kconfig:if USB_DEVICE_STACK ./subsys/usb/Kconfig:endif # USB_DEVICE_STACK ./subsys/usb/class/Kconfig:if USB_DEVICE_STACK /* 非严格匹配查找的结果,注意对比下文严格匹配查找的例子。 */ ./subsys/usb/class/Kconfig:endif # CONFIG_USB_DEVICE_STACK ./subsys/usb/class/netusb/Kconfig: depends on USB_DEVICE_STACK -------------------------------------------------------------------> 下面命令执行得到的是一样的结果: find . -name Kconfig | xargs grep USB_DEVICE_STACK find . -name "Kconfig" | xargs grep "USB_DEVICE_STACK" find . -name "Kconfig" | xargs grep USB_DEVICE_STACK find . –name Kconfig | xargs grep "USB_DEVICE_STACK"
|
2. 查找在哪些文件中含有某个字符串(严格匹配)
严格匹配就加上参数-w
例:在当前目录下的查找含有字符串USB_DEVICE_STACK的所有的Kconfig文件
xxxxxx@srv-pad-compile5: $ find . -name Kconfig | xargs grep -wrn USB_DEVICE_STACK 输出结果: ./drivers/usb/device/Kconfig:13: select USB_DEVICE_STACK ./subsys/usb/Kconfig:9:menuconfig USB_DEVICE_STACK ./subsys/usb/Kconfig:17:if USB_DEVICE_STACK ./subsys/usb/Kconfig:92:endif # USB_DEVICE_STACK ./subsys/usb/class/Kconfig:9:if USB_DEVICE_STACK ./subsys/usb/class/netusb/Kconfig:10: depends on USB_DEVICE_STACK
|
3. 查找指定名称的文件
命令格式:find +目标目录 + -name + 目标文件名
例:在当前目录下查找文件usb_device.c
xxxxxx@srv-pad-compile5: $ find . -name usb_device.c 输出结果: ./subsys/usb/usb_device.c
|
4. 全局查找含有某个字符串的文件
当不知道目标字符串可能会出现在哪些文件中的时候,就没法指定查找的文件范围(模糊匹配文件或严格匹配文件),这时候可以进行全局查找,即在所有文件中查找。
命令格式:grep 目标字符串 -nr *
例:在当前目录下查找含有USB_DEVICE_STACK这个字符串的所有文件。
xxxxxx@srv-pad-compile5: $ grep "USB_DEVICE_STACK" -nr * drivers/usb/device/Kconfig:13: select USB_DEVICE_STACK include/linker/common-ram.ld:176:#if defined(CONFIG_USB_DEVICE_STACK) …………………………………………………………………………………… |
同样可以加参数-w,严格匹配查找的目标字符串,输出就不会含有模糊匹配的结果。如下:
xxxxxx@srv-pad-compile5: $ grep USB_DEVICE_STACK -wnr * 输出: drivers/usb/device/Kconfig:13: select USB_DEVICE_STACK subsys/usb/Kconfig:9:menuconfig USB_DEVICE_STACK subsys/usb/Kconfig:17:if USB_DEVICE_STACK subsys/usb/Kconfig:92:endif # USB_DEVICE_STACK subsys/usb/class/Kconfig:9:if USB_DEVICE_STACK subsys/usb/class/netusb/Kconfig:10: depends on USB_DEVICE_STACK
|