How to find files with a certain subpath?

8,062

Solution 1

That's not a regex. For globs one should use the -path predicate instead.

Solution 2

I am not sure what you want to do with the files after you find them, but for interactive use in zsh I would use something like this:

ls **/trunk/**/config/*.xml
Share:
8,062
Loom
Author by

Loom

Updated on September 18, 2022

Comments

  • Loom
    Loom over 1 year

    I need to find all xml-files that are placed in folders named config. Also config must be somewhere under a folder named trunk. For example, I am interested in all files like below:

    ~/projects/e7/trunk/a/b/c/config/foo.xml
    ~/projects/d/trunk/config/bar.xml
    ~/projects/trunk/config/other.xml
    ~/projects/e/e/e/trunk/e/e/e/e/e/e/e/e/config/eeeee.xml
    

    I tried the find command:

    find ~/projects -regex "*/trunk/*/config/*.xml"
    

    , but the output was empty. What is the correct way to find the required files?

    • Hauke Laging
      Hauke Laging almost 11 years
      You do need -regex if you want to make sure that only files from a directory config are returned unless you can guarantee that no directory config has subdirectories (which have a *.xml in their tree).
  • Loom
    Loom almost 11 years
    Thank you. It works find ~/projects -path "*/trunk*/config/*.xml"
  • Loom
    Loom almost 11 years
    Actually, I'd like to look in some files from recieved list. After your answer, I became interested in zsh and installed it. But unfortunately you command-line didn't work. I tried to modify your command (for example ls ~/projects '**/trunk/**/config/*.xml' --recursive) but without success.
  • phunehehe
    phunehehe almost 11 years
    Ah I forgot, zsh doesn't do much with the default settings, you need to put setopt EXTENDED_GLOB in ~/.zshrc.
  • phunehehe
    phunehehe almost 11 years
    You might want to have a look at oh-my-zsh or my zshrc.