Recursively find files with a specific extension

LinuxBash

Linux Problem Overview


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 !

Linux Solutions


Solution 1 - Linux

My preference:

find . -name '*.jpg' -o -name '*.png' -print | grep Robert

Solution 2 - Linux

Using find's -regex argument:

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

Or just using -name:

find . -name 'Robert.*' -a \( -name '*.cpp' -o -name '*.h' \)

Solution 3 - Linux

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".

Solution 4 - Linux

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

eval find . -false "-o -name Robert".{jpg,pdf}

Solution 5 - Linux

This q/a shows how to use find with regular expression: https://stackoverflow.com/questions/6844785/how-to-use-regex-with-find-command

Pattern could be something like

'^Robert\\.\\(h|cgg\\)$'

Solution 6 - Linux

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)$'

Solution 7 - Linux

Using bash globbing (if find is not a must)

ls Robert.{pdf,jpg} 

Solution 8 - Linux

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

ftype="jpg"
ls -1R *.${ftype}  2> /dev/null

Solution 9 - Linux

For finding the files in system using the files database:

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

Make sure locate package is installed i.e. mlocate

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA1XView Question on Stackoverflow
Solution 1 - LinuxFoggyDayView Answer on Stackoverflow
Solution 2 - LinuxEtan ReisnerView Answer on Stackoverflow
Solution 3 - LinuxMark SetchellView Answer on Stackoverflow
Solution 4 - LinuxjxhView Answer on Stackoverflow
Solution 5 - LinuxJens KrogsboellView Answer on Stackoverflow
Solution 6 - Linuxjm666View Answer on Stackoverflow
Solution 7 - LinuxonceView Answer on Stackoverflow
Solution 8 - LinuxwusemanView Answer on Stackoverflow
Solution 9 - LinuxheyzshView Answer on Stackoverflow