How to Exit cat Command in Linux Terminal
In this article
Exiting the cat command in a Linux or Unix terminal is straightforward: press Ctrl+D (EOF) or Ctrl+C (interrupt). This guide covers both methods, when to use each, and what to do if the terminal seems stuck.
What You'll Need
- A terminal or console window (any Linux, macOS, or Unix-like system).
- Basic familiarity with typing commands and pressing Enter.
Step-by-Step
- Identify the situation. You are likely running
catin one of these scenarios:- You typed
catalone (no filename) and are now typing input lines. - You ran
cat filenameand the file content is being displayed. - You used
catin a pipeline (e.g.,cat file | grep foo) and it seems to hang.
- You typed
- To exit when
catis waiting for input (no filename): Press Ctrl+D at the beginning of a new line. This sends an end-of-file (EOF) signal, tellingcatthat there is no more input. It will then finish and return you to the shell prompt. - To exit immediately at any time: Press Ctrl+C. This sends an interrupt signal (SIGINT) that kills the
catprocess and returns you to the prompt. This works even ifcatis displaying a huge file or stuck in a loop. - If you are viewing a file with
catand it scrolls past: You cannot scroll back withcat; it only prints to the terminal. To exit, press Ctrl+C if it is still running, or simply wait for it to finish if the file is short. For long files, consider usinglessormoreinstead. - If
catis part of a pipeline: Press Ctrl+C to terminate the entire pipeline. If you only want to stopcatbut let the rest continue (unusual), you would need to kill the specific process, but that is rarely needed.
Common Mistakes
- Pressing Ctrl+D in the middle of a line: If you have typed text but not pressed Enter, Ctrl+D will not exit; it will just flush the input buffer. You must press Enter first, then Ctrl+D on an empty line.
- Using Ctrl+Z instead: This suspends the process rather than exiting. You will get a message like
[1]+ Stoppedand return to the prompt, butcatis still running in the background. To kill it, you need to runkill %1or bring it back withfg. - Assuming Ctrl+C won't work: It always works for
catunless the terminal is completely unresponsive (which is rare). If it doesn't, try pressing Ctrl+\ (quit signal) or open another terminal and kill the process.
Tips
- Use Ctrl+D for normal EOF: This is the clean way to end input when you are intentionally typing content (e.g., creating a file with
cat > file.txt). It allowscatto finish gracefully. - Use Ctrl+C for emergencies: If you accidentally start
caton a binary file or a huge log, Ctrl+C stops it immediately. - Remember that
catis not a pager: For reading files interactively, useless(exit with q) ormore(exit with q). - If you want to see line numbers while viewing, use
cat -n: But it still doesn't allow scrolling back; for that, useless -N.
Troubleshooting
What if Ctrl+D doesn't exit? Make sure you are on an empty line (press Enter first). If you are in a here-document or a shell script, Ctrl+D may not work; try Ctrl+C.
What if Ctrl+C doesn't exit? This is rare but can happen if the terminal is frozen. Try pressing Ctrl+\ (Ctrl + backslash) to send a SIGQUIT, which will also terminate the process. If that fails, open another terminal and run pkill cat or killall cat to kill all cat processes.
What if I see cat: write error: Broken pipe? This usually means the process reading from cat (e.g., head) has exited. cat will exit on its own after a moment. If it doesn't, press Ctrl+C.
What if I accidentally typed cat and now the terminal is full of garbage? Press Ctrl+C to stop, then type reset and press Enter to clear the screen and reset the terminal settings.
Frequently Asked Questions
Q: What does Ctrl+D do exactly? A: It sends an EOF (end-of-file) character. When a program like cat is reading from standard input, it interprets this as "no more input" and exits cleanly.
Q: Can I exit cat with the q key? A: No, cat does not respond to q. That is a feature of pagers like less and more. Use Ctrl+D or Ctrl+C.
Q: Why does cat sometimes not exit when I press Ctrl+D? A: Because you may have typed characters on the current line. Press Enter first, then Ctrl+D on a blank line.
Q: Is there a difference between Ctrl+C and Ctrl+D? A: Yes. Ctrl+C sends an interrupt signal that kills the process immediately. Ctrl+D sends an EOF that allows the program to finish gracefully. Use Ctrl+D when you are done typing input; use Ctrl+C to abort.
Q: What if I want to exit cat but keep the shell open? A: Both methods return you to the shell prompt, so you are fine. The shell remains open.
Q: Can I use exit command to leave cat? A: No, exit is a shell builtin that closes the terminal session. It will not work while cat is running. You must use the key combinations above.