Scripting question how to filter list of files based on globs

Paul D newtwen at gmail.com
Wed Apr 10 16:03:25 PDT 2024


On 2024-04-10 16:45, Philip Prindeville wrote:
> 
> 
>> On Apr 9, 2024, at 6:03 PM, Paul D <newtwen at gmail.com> wrote:
>>
>> On 2024-04-09 23:30, Philip Prindeville via openwrt-devel wrote:
>>> I'm trying to modify a script generates a list of filenames one per 
>>> line, but should be filtered against a blacklist of file globs.
>>>
>>> Something like:
>>>
>>> % find dir <args> -print | grep -v -f blacklist
>>
>>
>> I got this. When I run it on the openwrt source, where package is the packages directory:
>>
>> Vanilla Output
>>
>> find package -type f -print
>>
>> ===
>> ...
>> package/utils/uencrypt/src/CMakeLists.txt
>> package/utils/uencrypt/src/uencrypt-mbedtls.c
>> package/utils/uencrypt/src/uencrypt-openssl.c
>> package/utils/uencrypt/src/uencrypt.c
>> package/utils/uencrypt/src/uencrypt.h
>> package/utils/uencrypt/Makefile
>> package/utils/zyxel-bootconfig/Makefile
>> package/utils/zyxel-bootconfig/files/95_apply_bootconfig
>> package/utils/zyxel-bootconfig/src/Makefile
>> package/utils/zyxel-bootconfig/src/zyxel-bootconfig.c
>> package/utils/ucode-mod-bpf/Makefile
>> package/utils/ucode-mod-bpf/src/bpf.c
>> package/utils/firmware-utils/Makefile
>> package/utils/yafut/Makefile
>> package/utils/debugcc/Makefile
>> ===
>>
>>
>> Exclude 'Makefile's
>>
>> find package -type f -name 'Makefile' -prune -o -type f -print
>>
>> ===
>> ...
>> package/utils/util-linux/patches/001-meson-properly-handle-gettext-non-existence.patch
>> package/utils/uencrypt/src/CMakeLists.txt
>> package/utils/uencrypt/src/uencrypt-mbedtls.c
>> package/utils/uencrypt/src/uencrypt-openssl.c
>> package/utils/uencrypt/src/uencrypt.c
>> package/utils/uencrypt/src/uencrypt.h
>> package/utils/zyxel-bootconfig/files/95_apply_bootconfig
>> package/utils/zyxel-bootconfig/src/zyxel-bootconfig.c
>> package/utils/ucode-mod-bpf/src/bpf.c
>> ===
>>
>> Exclude Makefiles and c files
>>
>> find package -type f -name 'Makefile' -o -name '*.c' -prune -o -type f -print
>>
>> ===
>> ...
>> package/utils/util-linux/patches/001-meson-properly-handle-gettext-non-existence.patch
>> package/utils/uencrypt/src/CMakeLists.txt
>> package/utils/uencrypt/src/uencrypt.h
>> package/utils/zyxel-bootconfig/files/95_apply_bootconfig
>> ===
>>
>> Exclude Makefiles and c files and patch files
>>
>>
>> find package -type f -name 'Makefile' -o -name '*.c' -o -name '*.patch' -prune -o -type f -print
>> ===
>> ...
>> package/utils/uencrypt/src/CMakeLists.txt
>> package/utils/uencrypt/src/uencrypt.h
>> package/utils/zyxel-bootconfig/files/95_apply_bootconfig
>> ===
>>
>> find package -type f -name 'Makefile' -o -type d -name 'utils' -prune -o -type f -print
>>
>> ===
>> ...
>> ===
>>
>>
>> Set your -type accordingly.
> 
> 
> The issue is that the pattern of names to exclude is user-defined, so we don't know it in advance.
> 

It's not beautiful, but it works:

xargs -a blacklist.txt -I {} sh -c 'find package \( -name "$1" -prune \) -o -type f -print' sh {}


:)




More information about the openwrt-devel mailing list