105

I'm trying to find files with specific extensions. For example, I want to find all .pdf and .jpg files that's named Robert

I know I can do this command

$ find . -name '*.h' -o -name '*.cpp'

but I need to specify the name of the file itself besides the extensions. I just want to see if there's a possible way to avoid writing the file name again and over again Thank you !

jww
  • 97,681
  • 90
  • 411
  • 885
A1X
  • 1,099
  • 3
  • 8
  • 11
  • 5
    Anyone copying this command, or any below it, should use `iname` instead - which is **case insensitive**. – Addison Aug 31 '18 at 00:12
  • 2
    Good point, Addison (hence the +1). However, it seems that the OP wants files named 'Robert' with a capital 'R'. As I interpret the post, this means that files containing 'robert' should NOT be outputted. '**R**obert` is what's written. However, there are situations when someone would want either 'Robert' or 'robert' to be found. Maybe the OP is in this situation. Whether to use `iname` or `name` depends on what you want to find. I've been in situations where the case was essential. – bballdave025 Sep 07 '18 at 06:26

9 Answers9

134

My preference:

find . -name '*.jpg' -o -name '*.png' -print | grep Robert
wjandrea
  • 28,235
  • 9
  • 60
  • 81
FoggyDay
  • 11,962
  • 4
  • 34
  • 48
22

Using find's -regex argument:

find . -regex '.*/Robert\.\(h\|cpp\)$'

Or just using -name:

find . -name 'Robert.*' -a \( -name '*.cpp' -o -name '*.h' \)
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
6
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".

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
3

As an alternative to using -regex option on find, since the question is labeled , you can use the brace expansion mechanism:

eval find . -false "-o -name Robert".{jpg,pdf}
jxh
  • 69,070
  • 8
  • 110
  • 193
  • find report "paths must precede expression", what's the problem? I usually add quote to resolve it, but don't work for this expression. Thanks! – netawater Nov 16 '15 at 05:28
  • I suspect you either forgot `eval` or the `.` in the command above, or your `find` is an alias for something more complicated, or you are not using the stock GNU `find` on your version of Linux. – jxh Nov 16 '15 at 16:51
  • Thanks for you reply, I used a script like this: FILEEXTEIONS=cs,[ch] eval find . -false "-o -name *".{$FILEEXTEIONS}, and it report that error. – netawater Nov 17 '15 at 01:47
  • As far as I know, variable expansion will not occur inside the brace expansion braces. – jxh Nov 17 '15 at 03:14
  • oh, I continue tried eval find . -false "-o -name *".{cs,config}, it report the same error. – netawater Nov 18 '15 at 02:21
  • If you are using glob expansion characters, escape them with `\ ` when passing to `eval`. – jxh Nov 18 '15 at 17:23
  • finally eval find . -false "-o -name \*".{cs,config} is OK now, thanks very much! – netawater Nov 19 '15 at 04:55
  • 1
    I wanted to use this technique in an Android shell but its `find` does not support `-false`. I worked around this by giving two mutually-exclusive conditions so they are collectively false: `-inum 1 -inum 2`. – starfry May 13 '18 at 10:41
2

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\\)$'
Community
  • 1
  • 1
Jens Krogsboell
  • 1,093
  • 11
  • 18
1

As a script you can use:

find "${2:-.}" -iregex ".*${1:-Robert}\.\(h\|cpp\)$" -print
  • save it as findcc
  • chmod 755 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)$'
clt60
  • 62,119
  • 17
  • 107
  • 194
1

Using bash globbing (if find is not a must)

ls Robert.{pdf,jpg} 
once
  • 1,369
  • 4
  • 22
  • 32
0

Recurisvely with ls: (-al for include hidden folders)

ftype="jpg"
ls -1R *.${ftype}  2> /dev/null
wuseman
  • 1,259
  • 12
  • 20
0

For finding the files in system using the files database:

locate -e --regex "\.(h|cpp)$"

Make sure locate package is installed i.e. mlocate