What does the output mean when you do
Is this what the image actually is made of?
Does it matter what file extension the image has?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
cat will print out the content of the file (zeroes and ones which will be mapped to meaningless characters, because they're not really characters right?) into
stdout which by default is your terminal.
The file extension is for us (humans), to easily figure out what is the type of file; some programs use it to open it too but mostly programs detect the file type using a header inside the file.
file file.png to get the file type, or
00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR 00000010: 0000 0002 0000 0002 0800 0000 0057 dd52 .............W.R 00000020: f800 0000 0e49 4441 5478 9c63 6c60 6062 .....IDATx.cl``b 00000030: 6000 0002 9200 84f7 7ca3 5800 0000 0049 `.......|.X....I 00000040: 454e 44ae 4260 82 END.B`.
to get a hex dump of the file content, notice the "PNG" in the first line.
Also from this file signatures reference we can see that PNG signature is:
PNG 89 50 4E 47 0D 0A 1A 0A PNG image
cat simply outputs contents of the file to
stdout stream, which in your case happens to be terminal. What happens with the sequence of 0's and 1's that comprise the file is up to the terminal. In case of binary file, sequences of bits that aren't text won't be recognized as text.
Consider for example
cating a file that has UTF characters, such as Chinese in two different terminals - one that has support for UTF characters (
terminator in the screenshot below) and one that doesn't have a good support (
xterm in the screenshot below). As you can see some characters aren't interpreted.
cat will output contents in either case, but how to interpret those contents - it's up to each terminal.
Now, if you redirect
stdout stream to a file, you're effectively creating a copy, pretty much the same as
cp original copy or
mv old new. In some rare cases you might want to splice two files - binary or text - which is also possible with
cat. The idea is the same. In accordance with POSIX definition, "[t]he input files can be any file type", which means
cat can read any file. However, there are some cases where
cat will assume text mode. In case of Ubuntu's
cat ( which is provided by GNU coreutils package) information provided in
info cat states:
‘cat’ reads in text mode if one of the options ‘-bensAE’ is used or if ‘cat’ is reading from standard input and standard input is a terminal. Similarly, ‘cat’ writes in text mode if one of the options ‘-bensAE’ is used or if standard output is a terminal.
Of course, if we actually want to view contents of binary file in readable format, we can use hex editors or any "dump" type of program, Midnight Commander file manager, or even
vi text editor. There's a pretty good list of those here and here already.
Images are made of bytes. So are text files
When a software application, for example
cat wants to read text files, it starts reading bytes and searches those bytes in character encodings such as ascii or unicode. It finds the character in the table and renders it.
But images don't have characters (they might but that's not what builds image) and you can guess what happens when you try to treat image formatted bytes like they were charcters. you get random characters for each file.
If you want to see the strings of a file use