Search code examples
wildcardls

List only folders which matches the wildcard expression


Is there a command which lists all folders that matches a wildcard expression? Example, if there are thousands of directories and I only want those ending in M or those starting in JO to be listed, can I do that with a certain Linux command? Thanks!


Solution

  • Use find command, for example:

    # find anything that start with 'jo' end with 'm' (case insensitive)
    find . -iname 'jo*m'
    

    You can execute any command after that, for example:

    # find just like above but case sensitive, and move them to `/tmp`
    find . -name 'JO*M' -exec mv -v {} /tmp \;
    

    To find only a directory, you can use -type d flag, for example:

    # find any directory that start with JO
    find . -name 'JO*' -type d
    

    Explanation, first argument is the starting directory, . means current directory. The next argument means the search criteria -name for case sensitive search, -iname for case insensitive search, -type for type of item search, -exec to execute certain command where the {} is the file name matched. You can learn more here or for your specific case here.