I have different folders with the name "templates" in various locations in my code. I need to list all the files within the folders with the name "templates"


This will find all the directories with the name "templates" beneath the current directory and list all the files in them

find -name 'templates' -type d -execdir ls {} \;
  • Awesome this work great. But sometimes I have other folders within the templates folder as well. In such scenarios I want to do this recursively. Is that something that is possible?
    – Gokul N K
    Dec 6 '16 at 6:07
  • 1
    Try this find -name 'templates' -type d -execdir find {} \; Dec 6 '16 at 6:16

All files within templates folders, recursively:

find . -path '*/templates/*'

If you don't want the files in the subdirectories of templates folders (except for the templates/foo/templates/bar ones for course), you could use the -regex extension of some find implementations:

find . -regex '.*/templates/[^/]*'

If you only want regular files, add a -type f. For any type of files except directory, ! -type d.

If you only care for the files' names and not the path leading to them, with GNU find, add a -printf '%f\n' (and maybe pipe to sort -u, assuming the file names don't contain newline characters, to get unique names).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.