1

When I type this in Ubuntu:

# cat -A my.config

It outputs:

^@^@^@^@^@^@^@^@^@^@^@

But for:

# cat my.config

nothing shows up in the terminal.

So from bash, how can you tell if a file contains those strange characters?

Thanks!

4
  • They're NUL bytes. But if you could test for them, what would you do with that information? Commented Jan 13, 2017 at 23:41
  • Use the file command, it will tell you if the file is a text file or something else. Commented Jan 13, 2017 at 23:45
  • 2
    SO is for programming questions, not questions about using Unix. Commented Jan 13, 2017 at 23:46
  • Interactively, you already know how -- you're showing that fact in the question itself! So what's the actual question? Commented Jan 14, 2017 at 0:00

3 Answers 3

2

Those characters are NULL bytes. You can search for them on a file with:

grep -Pa '\x00' /folder/file

You may also search for all non-ASCII characters on a file with this:

grep -Pa "[\x80-\xFF]" /folder/file

Finally, you could check for all non-ASCII characters on all files in a folder with the following code:

grep -Pa -r "[\x80-\xFF]" /folder

Note: the core of this answer relies on grep -P, which might not be available on all grep versions. Also, from grep man about the -P option:

-P, --perl-regexp Interpret PATTERN as a Perl regular expression. This is highly experimental and grep -P may warn of unimplemented features.

Sign up to request clarification or add additional context in comments.

3 Comments

grumbles about use of GNUisms without explicit caveat or warning
@Charles: Is it better now? If you think another caveat should be added please comment again.
Yup. (I was trying to decide whether to bring this up at all, since the OP explicitly specifies "Linux" in the title, and GNU grep is most common there -- but then, there are quite a lot of embedded Linux systems running Busybox, or a few running GNU grep compiled without the optional -- meaning, not-always-compiled-in even with latest GNU grep -- PCRE support).
1

Assuming "viewable" class in your post matches grep "graph" class, this should work

grep -la "[^[:graph:]]"

1 Comment

There are actually stronger guarantees that this will be available than the GNU-specific documentation you linked to -- the graph character class is part of POSIX BRE, so absent -a this answer would be guaranteed to work on all POSIX platforms.
1

Octal dump is your friend:

od -c my.config

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.