我正试图找到具有特定扩展名的文件。 例如,我想找到所有名为 Robert 的. pdf 和. jpg 文件
我知道我可以执行这个命令
$ find . -name '*.h' -o -name '*.cpp'
但是我需要指定除了扩展名之外的文件本身的名称。 我只是想看看是否有一种可能的方法可以避免一遍又一遍地写文件名 谢谢!
This q/a shows how to use find with regular expression: How to use regex with find command?
Pattern could be something like
'^Robert\\.\\(h|cgg\\)$'
Using find's -regex argument:
find
-regex
find . -regex '.*/Robert\.\(h\|cpp\)$'
Or just using -name:
-name
find . -name 'Robert.*' -a \( -name '*.cpp' -o -name '*.h' \)
find -name "*Robert*" \( -name "*.pdf" -o -name "*.jpg" \)
The -o repreents an OR condition and you can add as many as you wish within the braces. So this says to find all files containing the word "Robert" anywhere in their names and whose names end in either "pdf" or "jpg".
-o
OR
As an alternative to using -regex option on find, since the question is labeled bash, you can use the brace expansion mechanism:
eval find . -false "-o -name Robert".{jpg,pdf}
As a script you can use:
find "${2:-.}" -iregex ".*${1:-Robert}\.\(h\|cpp\)$" -print
findcc
and use it as
findcc [name] [[search_direcory]]
e.g.
findcc # default name 'Robert' and directory . findcc Joe # default directory '.' findcc Joe /somewhere # no defaults
note you cant use
findcc /some/where #eg without the name...
also as alternative, you can use
find "$1" -print | grep "$@"
and
findcc directory grep_options
like
findcc . -P '/Robert\.(h|cpp)$'
My preference:
find . -name '*.jpg' -o -name '*.png' -print | grep Robert
Using bash globbing (if find is not a must)
ls Robert.{pdf,jpg}
Recurisvely with ls: (-al for include hidden folders)
ftype="jpg" ls -1R *.${ftype} 2> /dev/null
For finding the files in system using the files database:
locate -e --regex "\.(h|cpp)$"
Make sure locate package is installed i.e. mlocate
locate