Highlight Those Files With Space, Backslash or Non-Printable Characters
ls
listing.
ls
has a -b flag that is able to print those non-printable characters to be in the octal \ddd notation. It is possible to recursively list subdirectories using ls -R -b
to find out file names with non-printable characters with \ddd octal string. Together with the -1
(minus one) option, we can print one entry per line of output. The output of ls -1Rb
can be piped to a regular expression grep to single out those problematic filenames.
ls -1Rb | egrep '\\[0-7][0-7][0-7]|[\\ ]'
will grab filenames with \ddd octal, blackslash or space.
You can even give those special characters some colour which I blogged about it before. Here is the script that will highlight these characters. ^[
means "Escape" and you need to type Ctrl-V followed by Esc to get that.
#! /bin/sh ls -1Rb ${1:-.} | \ nawk ' /:$/ { sub(":","") d=$0 next } $0 != "" { printf("%s/%s\n",d,$0) }' | \ egrep '\\[0-7][0-7][0-7]|[\\ ]' | \ sed ' # non-printable character in octal \ddd s/\(\\[0-7][0-7][0-7]\)/^[[31m\1^[[0m/g # space s/\([ ]\)/^[[42m\1^[[0m/g # blackslash but not \ddd in octal s/\(\\\)\([^0-7][^0-7][^0-7]\)/^[[34m\1^[[0m\2/g '
Labels: shell script
1 Comments:
I love this tip!
Post a Comment
<< Home