Linux命令行查找文件

Linux命令行查找文件 #

搜索文件名:find #

Example:

bash
find ./target-dir/ -name '*.txt'

find ./target-dir/ -maxdepth 1 -name '*.txt'

搜索文件内容:grep #

Usage:

text
grep [OPTIONS] PATTERN [FILE...]
Options:
  -r   recursive
  -i   case-insensitive
  -n   line-number
  -F   fixed-string
  -E   extended-regexp
  -x   matches-whole-line
  -v   invert-match
  -d skip   skip-directoires
  -d recursive   same as the option -r

Example:

bash
# Search case-insensitive 'Apple' in target-dir recursively and print line-number
grep -rni Apple ./target-dir/

# Search 'Apple' with max-depth 3
grep Apple  *  */*  */*/*

# Search 'Apple' or 'Banana' in the files of current directory
grep -n -d skip 'Apple\|Banana' *

搜索文件内容:vim,vimgrep #

Usage:

text
:vimgrep /pattern/[g][j] files...
    g   Search one, not all
    j   No jump if matches

Example:

bash
# Enter vim
vim

# Search 'Apple' in current workdir recursively
:vimgrep /Apple/ **/*

# Search case-insensitive 'Apple' or 'Banana' in *.txt and no jump
:vimgrep /\cApple\|Banana/j *.txt

# Open quickfix window
:cw[indow]

# List quickfix
:cl[ist]

# Next quickfix
:cn[ext]

# Previous quickfix
:cp[revious]
2025年7月14日