383

I want to do exactly what unix "cat" does, but on my PC. Is there a simple equivalent command for the Windows command line?

Specifically I want to create a file from all the files of a given type in a folder

In Unix:

cat *fna >all_fna_files.fna

(which joins all the ".fna" text files into one big text file)

Sathyajith Bhat
  • 61,504
  • 38
  • 179
  • 264
Kirt
  • 7,001
  • 17
  • 51
  • 61

3 Answers3

475

type

It works across command.com, cmd, and PowerShell (though in the latter it's an alias for Get-Content, so is cat, so you could use either). From the Wikipedia article (emphasis mine):

In computing, type is a command in various VMS. AmigaDOS, CP/M, DOS, OS/2 and Microsoft Windows command line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS/4NT and Windows PowerShell. It is used to display the contents of specified files. It is analogous to the Unix cat command.

C:\>echo hi > a.txt
C:\>echo bye > b.txt
C:\>type a.txt b.txt > c.txt
C:\>type c.txt
hi
bye
ckhan
  • 7,779
  • 2
  • 17
  • 16
  • 18
    Good informative answer, so +1. Wish that I could give Microsoft a -1 though for the utterly ambiguous command name. `type filename` makes me think that the command should return the type of file, not the contents of the file! – PenguinCoder Jun 10 '12 at 16:33
  • 53
    Cat doesn't really seem much better. – David Boike Jun 10 '12 at 16:51
  • 2
    @PenguinCoder Why are you blaming MS? Seems to me they simply followed an existing convention from VMS. – Andy Jun 10 '12 at 17:28
  • 10
    @DavidBoike Although cat is from con_cat_enate AFAIR. – Mark Hurd Jun 10 '12 at 17:55
  • 1
    If I could go back in time I'd suggest "dump". – David Boike Jun 10 '12 at 18:01
  • 25
    @davidboike It is much better because it actually means and stands for what it does: *The cat program is a standard Unix utility that concatenates and lists files. The name is an abbreviation of catenate, a synonym of concatenate.* [Wikipedia Article](http://en.wikipedia.org/wiki/Cat_(Unix)) Can you say the same for the MS-DOS `type` command?? – PenguinCoder Jun 10 '12 at 18:21
  • 25
    @PenguinCoder Except `type` doesn't concatenate files; it just `type`s their contents to the screen. Its the piping in the example that is actually combining the files, not the `type` command. – Andy Jun 10 '12 at 19:50
  • @Andy Will not argue further, however quoting the exact answer: *From the Wikipedia article (emphasis mine): [sic] It is used to display the contents of specified files. **It is analogous to the Unix cat command**.* – PenguinCoder Jun 10 '12 at 19:52
  • 3
    @PenguinCoder a·nal·o·gous/əˈnaləgəs/ Adjective: 1.Comparable in **certain respects**, typically in a way that makes clearer the nature of the things compared. – Andy Jun 10 '12 at 19:53
  • 5
    MS-DOS likely inherited `TYPE` from CP/M by way of QDOS. [The TYPE command displays the content of the ASCII source file ufn on the currently logged disk at the console device.](http://web.archive.org/web/20100812024127/http://www.iso.port.ac.uk/~mike/interests/chistory/documents/cpm-22-manual/ch1.html#Section_1.4.5) – user Jun 11 '12 at 11:07
  • 2
    @Andy are you sure it doesn't concatenate? C:\>type a.txt b.txt > c.txt See, the two parameters to the TYPE command there and it's > not >>. Typing each is concatenating them. Also it looks like maybe redirection operators like > and >> are not actually called pipes. Pipes are just one form of redirection, you know, | , and not used here. See http://en.wikipedia.org/wiki/Redirection_(computing)#Piping – barlop Jun 14 '12 at 06:28
  • @barlop no, typing each will display each file one after the other, to stdout, and the > redirects stdout to a file. I misspoke when I said piping, it is redirecting. Take out the > c.txt and a.txt and b.txt will remain unchanged, so its not doing the cat, its taking advantage of the fact it will output multiple files and clever use of redirection. – Andy Jun 14 '12 at 13:03
  • 1
    @Andy isn't $cat a.txt b.txt also outputting a.txt then b.txt to stdout? a.txt and b.txt remain unchanged. just like with type. What's the difference in result between cat a.txt b.txt >c and type a.txt b.txt >c ? or, cat a.txt b.txt and type a.txt b.txt ? you can replace the word cat with the word type, and it seems to have the same effect. So I don't see how you can say that cat concatenates and type doesn't. – barlop Jun 14 '12 at 14:35
  • 1
    @Andy I think the core point that was missed in this comment chain was the perspective that `type` and `cat` both _do_ catenate the files: they're just writing the result to the one spot that's the most sensible: stdout. Frankly, I think they're both bad for intuitive uptake/guessing/recognition, and both about equally sensible once you think up of a way to remember them (`type` "types" file contents out to the output, `cat` (con)catenates files to the output). – mtraceur Nov 04 '16 at 04:34
  • Make sure you set the output file to some other directory, specially if you're using wildcards to append files. Eg " type *.sql > all.sql". Otherwise you'll end up duplicated content. Yes the command type is that stupid. – retromuz Sep 12 '17 at 06:49
  • 1
    type won't read from stdin – Erik Aronesty Feb 10 '22 at 17:15
33

From the command shell:

copy a.txt + b.txt + c.txt output.txt

(But that follows the command shells use of control-Z as an end of file marker, so not suitable in some cases).

In PowerShell:

get-content a.txt,b.txt,c.txt | out-file output.txt

and you can control (using -Encoding parameter) the file encoding (which allows transcoding by using different encoding for the read and write).

Richard
  • 8,952
  • 3
  • 26
  • 27
  • 8
    PowerShell aliases `cat` to `Get-Content` too. It's designed to accept many basic Linux commands without much, if any, modification. – Bob Jun 10 '12 at 09:12
  • 1
    Bob, except if switches and options are involved. – Joey Jun 10 '12 at 16:25
  • @Richard: Copy /b a + b + c output.txt doesn't check for on Ctrl-Z. Both variants will copy the entire file if there is NO ctrl-Z in the file. – Tonny Jun 10 '12 at 18:28
  • 2
    Note that you can include wildcards too, the way wildcards work in windows means you won't be messed up by the expansion list not containing a `+`, so `copy [/b] *.fna all_fna_files.fna`.. – Random832 Jun 10 '12 at 22:24
  • if I recall from an old test I once did, using COPY with /B to concatenate, will ignore CTRL-Z/EOF markers, and will do the concatenation properly! but judging by copy /? you may need to do a lot of /B like it seems maybe after copy, after each src file and after the dest file.. strange. – barlop Jun 14 '12 at 06:38
  • You can also use `type` command like `type *.txt > file.merge` – Riz Oct 27 '17 at 15:01
5

I have just used the cat command in DOS (Windows 7 Pro) in the following manner and successfully merged 3 files (log1.txt, log2.txt, log3.txt) into a single file:

cat log*.txt >> myBigLogFile.txt 

Note: cat log*.txt > myBigLogFile2.txt also provide the same result, but it'll override the previous file.

kenorb
  • 24,736
  • 27
  • 129
  • 199
Heidi
  • 67
  • 1
  • 1
  • 11
    maybe you have GNU for Windows, or Cygwin in your path, or are using powershell because cat does not exist on a clean Windows 7 system. DOS is no longer a thing, but most people would use it to refer to cmd.exe. – Shanteva May 09 '16 at 14:22
  • 2
    [Windows CMD and MS-DOS are not the same thing](https://superuser.com/q/451432/241386). There's no `cat` command in both cmd and DOS, only `cat` alias in powershell – phuclv Jun 16 '17 at 01:15
  • you can also use `type` command `type *.txt > file.merge` – Riz Oct 27 '17 at 15:00