find and delete files with non-ascii names

前端 未结 5 2066
天涯浪人
天涯浪人 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

    By now, you probably have solved your question, but it didn't work well for my case, as I had files that was not being shown by find when I used the -regex switch. So I developed this workaround using ls. Hope it can be useful to someone.

    Basically, what worked for me was this:

    ls -1 -R -i | grep -a "[^A-Za-z0-9_.':@ /-]" | while read f; do inode=$(echo "$f" | cut -d ' ' -f 1); find -inum "$inode" -delete; done
    

    Breaking it in parts:

    ls -1 -R -i
    

    This will recursively (-R) list (ls) files under current directory, one file per line (-1), prefixing each file by its inode number (-i). Results will be piped to grep.

    grep -a "[^A-Za-z0-9_.':@ /-]"
    

    Filter each entry considering each input as text (-a), even when it is eventually binary. grep will let a line pass if it contains a character different from the specified in the list. Results will be piped to while.

    while read f
    do
        inode=$(echo "$f" | cut -d ' ' -f 1)
        find -inum "$inode" -delete
    done
    

    This while will iterate through all entries, extracting the inode number and passing the inode to find, which will then delete the file.

提交回复
热议问题