1

This is potentially a stupid question, or simply the answer might not be helpful—but I am curious. It looks like the strings command and the cat command are equivalent on non-binary files. Is this true? Or are there some caveats of using strings on all file-types?

0

2 Answers 2

2

By default strings finds strings of at least four printable characters. Try echo abc | strings. I am sure there are more subtle differences. So, the answer to the question in the title is "no".

In addition, the two commands have different purposes and a range of different command line options, which makes them very non-equivalent.

1
  • 1
    See also echo Stéphane | strings which gives phane with GNU strings (St too short, é not considered as text). Apr 23, 2021 at 5:54
1

Of course they're not equivalent. Here's an example with normal text files

$ cat file_with_newline_at_the_end
with new line



another line


$ strings file_with_newline_at_the_end
with new line
another line
$ cat file_without_newline_at_the_end

As you can see, strings just ignore blank lines but cat simply passes the whole byte stream to the terminal by default. That also means cat knows nothing and doesn't care about file encodings but strings does. By default strings just looks for 7-bit ASCII characters so strings with a byte larger than 0x7F will produce different output. UTF-16 and UTF-32 files also won't work with cat but outputs correctly in GNU strings with the --encoding option

Besides strings looks for printable characters (0x20-0x7E) so files with control characters like a vertical tab (0x0B) will have different outputs

$ echo -e "abcd\x0bcdef" | strings
abcd
cdef
$ echo -e "abcd\x0bcdef" | cat
abcd
    cdef

There are also many options that produce different output such as -o or -t in strings or -benstv in cat

Another difference is that strings looks for strings at least 4 characters long by default, as mentioned in the other answer

GNU strings:

For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the options below) and are followed by an unprintable character.

macOS strings:

Strings looks for ASCII strings in a binary file or standard input. Strings is useful for identifying random object files and many other things. A string is any sequence of 4 (the default) or more printing characters [ending at, but not including, any other character or EOF]. Unless the - flag is given, strings looks in all sections of the object files except the (__TEXT,__text) section. If no files are specified standard input is read

2
  • Oh yes, I forgot about new lines! Apr 23, 2021 at 4:11
  • 1
    not only new lines but also non-printable characters
    – phuclv
    Apr 23, 2021 at 4:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.