1

If I have a binary file with the following contents:

48 65 6C 6C 6F 1A 48 65 6C 6C 6F

Then when I run the TYPE command on it, it stops reading at the 1A character:

C:\Temp>type file.bin
Hello

However, when I run TYPE again but this time pipe the output to MORE, it produces the following output:

C:\Temp>type file.bin|more
Hello→Hello

C:\Temp>

Which is more representative of the actual contents of the file than the previous command.

What exactly does piping output to MORE do that makes it print out the entire ASCII representation of the file regardless of the presence of a 1A character?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Be aware that `more` modifies Tabulator characters, it changes them to sequences of Space characters; see also its [`/Tn`](http://ss64.com/nt/more.html) option... – aschipfl Sep 26 '16 at 18:36
  • Just a thought - as piped output goes into a file called `something.$$$`. More is getting a block device - a file. –  Sep 26 '16 at 22:19

1 Answers1

3

type command checks internally where the output will be sent.

If the output stream is the console, the input buffer is checked for the presence of Ctrl-Z (0x1A character) that is handled as EOF.

If the output stream is not the console, then all the characters are handled. This happens not only with pipes, but also redirection.

type file.bin | more
type file.bin > con
type file.bin > file.bin.out

Both redirected and piped type commands will handle all the characters.

This behaviour of 0x1A character is also present in copy command, but in this case it is also documented.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • I think I can somehow remember that `type` aborted at EOF characters also for files back in MS-DOS days; no clue why they changed it though... – aschipfl Sep 26 '16 at 18:35
  • 3
    @aschipfl, It seems it is a [CP/M heritage](https://blogs.msdn.microsoft.com/oldnewthing/20040316-00/?p=40233) – MC ND Sep 26 '16 at 18:57
  • 1
    I have a horrible feeling that there is a 32kB limitation on TYPE or MORE, by the way. Or maybe 32,000 lines. Can't remember but watch out. – Mark Setchell Sep 26 '16 at 20:55
  • 2
    @MarkSetchell, `more` is designed for interactive usage. When writing to console it fills a page and waits for a keypress. When its output is redirected it writes 65534 lines and waits for a keypress. – MC ND Sep 26 '16 at 21:22
  • 1
    Files coming from a stream (COM port or console) have no size (unlike block devices like hard drives). So to tell when the file has reached the end Ctrl + Z is added. –  Sep 26 '16 at 22:05
  • So `copy com1 file` copies a file in text mode - it copies until Ctrl + Z is reached - this is the default for block devices. `copy file file` copies in block mode - the default for block devices. `copy /a file file` copies the file as if it's a text file - it will stop at the first ctrl + Z. It makes no sense to do `copy /b com1 file` as it will not work (compare `copy con file.txt` then enter and Ctrl + Z with copy /b con file.txt` then enter and Ctrl + Z. –  Sep 27 '16 at 01:04