Skip to main content

How to Print a File in the Terminal with cat

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

To check what's inside a small text file, such as a settings file, a note or a generated password file, you don't need an editor. cat prints the file in the terminal. Give it two files and it prints one after the other, which is where its name comes from: concatenate. PowerShell's version is Get-Content.

Syntax​

In zsh and bash:

Syntax
cat <file> [<file> ...]
PartWhat it means
<file>The file to print. Give several to print them in order

The macOS manual page for cat says: "The cat utility reads files sequentially, writing them to the standard output." Standard output is your terminal.

In PowerShell:

Syntax
Get-Content <file>[, <file> ...]

Microsoft's documentation says "The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file". The same page lists its short names: gc and type on every platform, and also cat on Windows.

The Example​

Two small files hold a to-do list: note.txt with two lines and more.txt with one. You print the first, then both.

Step 1: Print One File, Then Two​

Terminal
cat note.txt
Output
Buy milk
Call the bank
Terminal
cat note.txt more.txt
Output
Buy milk
Call the bank
Pay rent

The zsh terminal after cat note.txt, which prints Buy milk and Call the bank, and cat note.txt more.txt, which prints those two lines and then Pay rent.

The second command printed note.txt and then more.txt, with nothing in between. Neither file changed: cat only reads.

Common Mistakes​

MistakeWhat happensDo this instead
A wrong name or foldercat: missing.txt: No such file or directory. In PowerShell: Cannot find path '...' because it does not exist.Check the name with ls
Giving cat a foldercat: reports: Is a directoryList a folder with ls
Printing a huge fileIt scrolls past faster than you can readUse less to page through it, or head for the first lines

Troubleshooting​

SymptomLikely causeFix
No such file or directoryThe file isn't in the current folderRun pwd and ls, or give the full path

FAQ​

Can cat create or change a file?​

Not on its own. It only reads. Redirection can write its output to a file, as in cat note.txt more.txt > all.txt, which writes the combined text to all.txt.

Is cat safe to use on a secrets file?​

It prints the secret to your screen, where others or a screen recording can see it. Print secrets only when you need them and nobody is watching.

Conclusion​

cat note.txt printed the file, and cat note.txt more.txt printed both in order. In PowerShell, Get-Content does the same. Use them to look into small text files without opening an editor.

Additional Resources​

  • Get-Content — PowerShell's command and its options.
  • man cat in your terminal — the manual page on macOS and Linux.