一点一滴学Linux

    返回首页    发表留言
本文作者:李德强
          第三节 文件查找命令
 
 

一、find

        find命令为查找命令,可以根所文件名来查找文件。它的基本用法为:

find [path] [option] [filename]

        比如:要在/home/lidq下查找一个叫switch_button_off.xcf的文件:

find /home/lidq -name switch_button_off.xcf
./Pictures/switch_button_off.xcf

        将-name修改为-iname就是不区分大小来查找文件:

find /home/lidq -iname SWITCH_BUTTON_OFF.XCF
/home/lidq/Pictures/switch_button_off.xcf

        还可以使用通配符*来查找文件,比如查找所有以.png结尾的文件:

find /home/lidq/Pictures/ -name *.png
/home/lidq/Pictures/Selection_003.png
/home/lidq/Pictures/icon/eclipse.png
/home/lidq/Pictures/icon/qq.png
/home/lidq/Pictures/icon/sqldeveloper.png
/home/lidq/Pictures/background/FootFall.png
/home/lidq/Pictures/Selection_001.png

        如果想要查找多个扩展名的文件,比如*.h和*.c可以使用-o参数:

find . -iname "*.h" -o -iname "*.c"
./lidqos/printf/printf.c
./lidqos/include/boot/code16.h
./lidqos/include/boot/main.h
./lidqos/include/boot/boot.h
./lidqos/include/kernel/printf.h
./lidqos/boot/main.c
./lidqos/kernel/kernel.c

        还可以使用下面的方法:

find . -iname "*.[h|c]"
./lidqos/printf/printf.c
./lidqos/include/boot/code16.h
./lidqos/include/boot/main.h
./lidqos/include/boot/boot.h
./lidqos/include/kernel/printf.h
./lidqos/boot/main.c
./lidqos/kernel/kernel.c

        另外使用find也可以使用正则表达式-regex来查找:

find . -regex ".*\.\(c\|h\)"
./lidqos/printf/printf.c
./lidqos/include/boot/code16.h
./lidqos/include/boot/main.h
./lidqos/include/boot/boot.h
./lidqos/include/kernel/printf.h
./lidqos/boot/main.c
./lidqos/kernel/kernel.c

二、grep

        grep的使用格式为grep string file1 file2 file3 … 比如在boot.S和main.c文件中查找字符串protect:

grep protect boot.S main.c 
boot.S:.global _start, _to_the_protect_mode
boot.S:_to_the_protect_mode:
boot.S:_to_the_protect_mode_end: nop
main.c:        to_protect_mode();
main.c: * to_protect_mode: 
main.c:void to_protect_mode()
main.c:        _to_the_protect_mode();

        -c:计算找到查找字符串的次数:

grep protect -c boot.S main.c 
boot.S:3
main.c:4

        -i:忽略大小写的不同,所以大小写视为相同。

grep PROTECTED -i boot.S main.c 
boot.S:.global _start, _to_the_protect_mode
boot.S:_to_the_protect_mode:
boot.S:_to_the_protect_mode_end: nop
main.c:        to_protect_mode();
main.c: * to_protect_mode: 
main.c:void to_protect_mode()
main.c:        _to_the_protect_mode();

        -n:输出查找字符串在文件中的行号。

grep  protect -n  boot.S main.c 
boot.S:10:.global _start, _to_the_protect_mode
boot.S:135:_to_the_protect_mode:
boot.S:205:_to_the_protect_mode_end: nop
main.c:32:        to_protect_mode();
main.c:118: * to_protect_mode
main.c:121:void to_protect_mode()
main.c:124:        _to_the_protect_mode();

        -v:反向选择,即显示出没有查找字符串内容的那些行: 

grep  protect -v  boot.S main.c 
boot.S:#ifndef _BOOT_S_
boot.S:#define _BOOT_S_
boot.S:
boot.S://16位操作数和16位寻址模式
boot.S:.code16
boot.S:
boot.S:#include <boot/boot.h>
… …

 

    返回首页    返回顶部
  看不清?点击刷新

 

  Copyright © 2015-2023 问渠网 辽ICP备15013245号