How to exclude a directory in find . command

后端 未结 30 1356
醉酒成梦
醉酒成梦 2020-11-22 03:36

I\'m trying to run a find command for all JavaScript files, but how do I exclude a specific directory?

Here is the find code we\'re using.<

相关标签:
30条回答
  • 2020-11-22 03:52
    find -name '*.js' -not -path './node_modules/*' -not -path './vendor/*'
    

    seems to work the same as

    find -name '*.js' -not \( -path './node_modules/*' -o -path './vendor/*' \)
    

    and is easier to remember IMO.

    0 讨论(0)
  • 2020-11-22 03:54

    For those of you on older versions of UNIX who cannot use -path or -not

    Tested on SunOS 5.10 bash 3.2 and SunOS 5.11 bash 4.4

    find . -type f -name "*" -o -type d -name "*excluded_directory*" -prune -type f
    
    0 讨论(0)
  • 2020-11-22 03:55

    This is the only one that worked for me.

    find / -name MyFile ! -path '*/Directory/*'
    

    Searching for "MyFile" excluding "Directory". Give emphasis to the stars * .

    0 讨论(0)
  • 2020-11-22 03:55

    I found the functions name in C sources files exclude *.o and exclude *.swp and exclude (not regular file) and exclude dir output with this command:

    find .  \( ! -path "./output/*" \) -a \( -type f \) -a \( ! -name '*.o' \) -a \( ! -name '*.swp' \) | xargs grep -n soc_attach
    
    0 讨论(0)
  • 2020-11-22 03:57

    You can use the prune option to achieve this. As in for example:

    find ./ -path ./beta/* -prune -o -iname example.com -print
    

    Or the inverse grep “grep -v” option:

    find -iname example.com | grep -v beta
    

    You can find detailed instructions and examples in Linux find command exclude directories from searching.

    0 讨论(0)
  • 2020-11-22 03:58

    Better use the exec action than the for loop:

    find . -path "./dirtoexclude" -prune \
        -o -exec java -jar config/yuicompressor-2.4.2.jar --type js '{}' -o '{}' \;
    

    The exec ... '{}' ... '{}' \; will be executed once for every matching file, replacing the braces '{}' with the current file name.

    Notice that the braces are enclosed in single quote marks to protect them from interpretation as shell script punctuation*.


    Notes

    * From the EXAMPLES section of the find (GNU findutils) 4.4.2 man page

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