What is the Windows equivalent of the Unix command cat?

224

41

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)

Kirt

Posted 2012-06-10T07:22:07.960

Reputation: 5 119

None of these suggestions can accept a redirect of STDIN. – will – 2016-04-19T03:11:36.130

2Powershell: cat file.txt – Kolob Canyon – 2016-10-05T18:12:15.567

Cross-duplicate: https://stackoverflow.com/q/60244/11683

– GSerg – 2019-07-08T20:53:41.840

Answers

280

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

Posted 2012-06-10T07:22:07.960

Reputation: 5 689

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 – 2016-11-04T04:34:38.000

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. – geeth – 2017-09-12T06:49:19.067

6Good 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 – 2012-06-10T16:33:59.267

31Cat doesn't really seem much better. – David Boike – 2012-06-10T16:51:37.430

1@PenguinCoder Why are you blaming MS? Seems to me they simply followed an existing convention from VMS. – Andy – 2012-06-10T17:28:43.457

4@DavidBoike Although cat is from con_cat_enate AFAIR. – Mark Hurd – 2012-06-10T17:55:16.857

1If I could go back in time I'd suggest "dump". – David Boike – 2012-06-10T18:01:08.337

10

@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 Can you say the same for the MS-DOS type command??

– PenguinCoder – 2012-06-10T18:21:02.377

13@PenguinCoder Except type doesn't concatenate files; it just types their contents to the screen. Its the piping in the example that is actually combining the files, not the type command. – Andy – 2012-06-10T19:50:56.997

@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 – 2012-06-10T19:52:22.580

1@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 – 2012-06-10T19:53:35.760

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.

– a CVn – 2012-06-11T11:07:51.440

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 – 2012-06-14T06:28:49.843

@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 – 2012-06-14T13:03:28.733

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 – 2012-06-14T14:35:30.623

22

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

Posted 2012-06-10T07:22:07.960

Reputation: 8 152

You can also use type command like type *.txt > file.merge – Riz – 2017-10-27T15:01:00.667

8PowerShell aliases cat to Get-Content too. It's designed to accept many basic Linux commands without much, if any, modification. – Bob – 2012-06-10T09:12:30.867

1Bob, except if switches and options are involved. – Joey – 2012-06-10T16:25:38.153

@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 – 2012-06-10T18:28:39.563

2Note 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 – 2012-06-10T22:24:41.807

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 – 2012-06-14T06:38:20.267

1

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.

Heidi

Posted 2012-06-10T07:22:07.960

Reputation: 27

5maybe 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 – 2016-05-09T14:22:37.137

1Windows CMD and MS-DOS are not the same thing. There's no cat command in both cmd and DOS, only cat alias in powershell – phuclv – 2017-06-16T01:15:07.913

you can also use type command type *.txt > file.merge – Riz – 2017-10-27T15:00:26.213