find filenames NOT ending in specific extensions on Unix?

前端 未结 8 1563
悲&欢浪女
悲&欢浪女 2020-11-30 17:26

Is there a simple way to recursively find all files in a directory hierarchy, that do not end in a list of extensions? E.g. all files that are not *.dll or *.exe

相关标签:
8条回答
  • 2020-11-30 17:34

    Linux/OS X:

    Starting from the current directory, recursively find all files ending in .dll or .exe

    find . -type f | grep -P "\.dll$|\.exe$"
    

    Starting from the current directory, recursively find all files that DON'T end in .dll or .exe

    find . -type f | grep -vP "\.dll$|\.exe$"
    

    Notes:

    (1) The P option in grep indicates that we are using the Perl style to write our regular expressions to be used in conjunction with the grep command. For the purpose of excecuting the grep command in conjunction with regular expressions, I find that the Perl style is the most powerful style around.

    (2) The v option in grep instructs the shell to exclude any file that satisfies the regular expression

    (3) The $ character at the end of say ".dll$" is a delimiter control character that tells the shell that the filename string ends with ".dll"

    0 讨论(0)
  • 2020-11-30 17:35
    $ find . -name \*.exe -o -name \*.dll -o -print
    

    The first two -name options have no -print option, so they skipped. Everything else is printed.

    0 讨论(0)
  • 2020-11-30 17:38
    find  /data1/batch/source/export   -type f -not  -name "*.dll" -not -name "*.exe"
    
    0 讨论(0)
  • 2020-11-30 17:47

    You could do something using the grep command:

    find . | grep -v '(dll|exe)$'
    

    The -v flag on grep specifically means "find things that don't match this expression."

    0 讨论(0)
  • 2020-11-30 17:48

    Or without ( and the need to escape it:

    find . -not -name "*.exe" -not -name "*.dll"
    

    and to also exclude the listing of directories

    find . -not -name "*.exe" -not -name "*.dll" -not -type d
    

    or in positive logic ;-)

    find . -not -name "*.exe" -not -name "*.dll" -type f
    
    0 讨论(0)
  • 2020-11-30 17:48

    Other solutions on this page aren't desirable if you have a long list of extensions -- maintaining a long sequence of -not -name 'this' -not -name 'that' -not -name 'other' would be tedious and error-prone -- or if the search is programmatic and the list of extensions is built at runtime.

    For those situations, a solution that more clearly separates data (the list of extensions) and code (the parameters to find) may be desirable. Given a directory & file structure that looks like this:

    .
    └── a
        ├── 1.txt
        ├── 15.xml
        ├── 8.dll
        ├── b
        │   ├── 16.xml
        │   ├── 2.txt
        │   ├── 9.dll
        │   └── c
        │       ├── 10.dll
        │       ├── 17.xml
        │       └── 3.txt
        ├── d
        │   ├── 11.dll
        │   ├── 18.xml
        │   ├── 4.txt
        │   └── e
        │       ├── 12.dll
        │       ├── 19.xml
        │       └── 5.txt
        └── f
            ├── 13.dll
            ├── 20.xml
            ├── 6.txt
            └── g
                ├── 14.dll
                ├── 21.xml
                └── 7.txt
    

    You can do something like this:

    ## data section, list undesired extensions here
    declare -a _BADEXT=(xml dll)
    
    ## code section, this never changes
    BADEXT="$( IFS="|" ; echo "${_BADEXT[*]}" | sed 's/|/\\|/g' )"
    find . -type f ! -regex ".*\.\($BADEXT\)"
    

    Which results in:

    ./a/1.txt
    ./a/b/2.txt
    ./a/b/c/3.txt
    ./a/d/4.txt
    ./a/d/e/5.txt
    ./a/f/6.txt
    ./a/f/g/7.txt
    

    You can change the extensions list without changing the code block.

    NOTE doesn't work with native OSX find - use gnu find instead.

    0 讨论(0)
提交回复
热议问题