find and delete files with non-ascii names

前端 未结 5 2050
天涯浪人
天涯浪人 2020-12-29 05:32

I have some old migrated files that contain non-printable characters. I would like to find all files with such names and delete them completely from the system.

Exam

5条回答
  •  长发绾君心
    2020-12-29 06:08

    It is possible to use PCRE with grep -P, just not with find (unfortunately). You can chain find with grep using exec. With PCRE (perl regex), we can use the ascii class and find any char that is non-ascii.

    find . -type f -exec sh -c "echo \"{}\" | grep -qP '[^[:ascii:]]'" \; -exec rm {} \;
    

    The following exec will not execute unless the first one returns a non-error code. In this case, it means the expression matched the filename. I used sh -c because -exec doesn't like pipes.

提交回复
热议问题