Categories
linux shell

How can I recursively find all files in current and subfolders based on wildcard matching?

2626

How can I recursively find all files in current and subfolders based on wildcard matching?

0

    3751

    Use find:

    find . -name "foo*"
    

    find needs a starting point, so the . (dot) points to the current directory.

    26

    • 30

      I know this is tagged as linux but this is worth mentioning: the path is required for on other *nix variants that aren’t linux. On linux, the path is optional if you want to use dot.

      – IslandCow

      Nov 16, 2013 at 0:14

    • 5

      @Seatter “foo*” tells find to look for all files that start with “foo”. It is just his example. You could use “gpio*” to find all files who’s names start with gpio, or just “gpio1” to find all files named gpio1.

      Apr 2, 2014 at 18:00

    • 47

      note that the “foo*” is in quotes so the shell doesn’t expand it before passing it to find. if you just did find . foo*, the foo* would be expanded AND THEN passed to find.

      – grinch

      May 19, 2014 at 14:29

    • 68

      Worth stressing that " " is very necessary for recursive searching.

      Oct 21, 2016 at 11:43


    • 24

      Also useful: If you don’t want to be notified about directories you don’t have permission to (or other errors), you can do find . -name "foo*" 2>/dev/null

      – Jobbo

      Aug 15, 2017 at 10:54


    283

    Piping find into grep is often more convenient; it gives you the full power of regular expressions for arbitrary wildcard matching.

    For example, to find all files with case insensitive string “foo” in the filename:

    ~$ find . -print | grep -i foo
    

    5

    • 90

      find also has the -iname, -regex, and -iregex flags for case-insensitive wildcard, regex, and case-insensitive regex matching, so piping to grep is unnecessary.

      – iobender

      Aug 4, 2015 at 16:54

    • 9

      I don’t think it is about being unnecessary, but being more convenient.

      Feb 8, 2017 at 16:57

    • 3

      However, piping to grep -v can allow you to use simple strings or regexes to remove entries you don’t want.

      Apr 6, 2017 at 3:15

    • 7

      @iobender – Sadly, I can tell you from experience that not all systems come with a find command that supports those options. Sometimes grep becomes the only option.

      – Mr. Llama

      Jul 5, 2018 at 17:36

    • One important caveat here is that if you’re using find on a directory that contains A LOT of files (eg; “) then this can be quite slow.

      Jul 22 at 16:38

    201

    find will find all files that match a pattern:

    find . -name "*foo"
    

    However, if you want a picture:

    tree -P "*foo"
    

    Hope this helps!

    3