The cat command (short for concatenate) is one of the most frequently used utilities in Linux. It lets you view file contents, create files, combine multiple files, and copy data directly in the terminal. This guide walks through the most common uses with clear examples, so you can start using cat confidently.
What You’ll Need
- A Linux system (or a terminal emulator like WSL on Windows or Terminal on macOS).
- Basic familiarity with opening a terminal window.
- Permission to read or write the files you plan to use (most user files in your home directory are fine).
Step-by-Step
- Open a terminal. Press
Ctrl+Alt+Ton most Linux distributions, or search for “Terminal� in your applications menu. - View a file’s contents. The simplest use is
cat filename. For example, to see the contents of a file namednotes.txt, type:cat notes.txt
The entire file is printed to the screen. If the file is long, it will scroll by quickly; consider using
lessinstead for long files. - Create a new file. Use
cat > newfile.txtto create a file and type content into it. After typing, pressCtrl+Don a new line to save and exit. Example:cat > todo.txt Buy groceries Call dentist Ctrl+D
This creates
todo.txtwith the two lines you typed. - Append to an existing file. Use
cat >> file.txtto add content to the end of a file without overwriting it. For instance:cat >> todo.txt Write report Ctrl+D
Now
todo.txthas three lines. - Combine multiple files. To concatenate two files into a new one, list them in order:
cat file1.txt file2.txt > combined.txt
This creates
combined.txtwith the content offile1.txtfollowed byfile2.txt. - Copy a file. You can use
catto copy a file by redirecting its output:cat original.txt > copy.txt
This creates
copy.txtwith the same content asoriginal.txt. - Display line numbers. Use the
-noption to show line numbers:cat -n script.sh
This is helpful when referencing specific lines in a file.
- Show non-printable characters. Use
-Ato display tabs as^Iand mark end-of-line with$:cat -A data.txt
This helps spot hidden characters that might cause issues in scripts.
- Suppress repeated blank lines. Use
-sto squeeze multiple blank lines into one:cat -s longfile.txt
This makes output easier to read.
- Use cat with pipes. Pipe
catoutput into other commands. For example, to search for a word:cat notes.txt | grep "meeting"
Or to count lines:
cat notes.txt | wc -l
(Though
grepandwccan take filenames directly, piping is common in longer chains.)
Common Mistakes
- Using
>instead of>>to append. The single>overwrites the file completely, which can destroy data if you meant to add to it. - Forgetting
Ctrl+Dwhen creating a file. If you pressCtrl+Cinstead, the file may be empty or partially written. - Using
caton binary files. It will print gibberish and may mess up your terminal. Usefileto check a file type first. - Viewing huge files with
cat. It floods the screen. Uselessorhead/tailinstead. - Not quoting filenames with spaces. If a file is named
my notes.txt, you must quote it:cat "my notes.txt".
Tips
- Use
cat -nwhen you need to reference line numbers in a discussion or error report. - Combine
catwithsortoruniqto process file contents:cat list.txt | sort | uniq. - To create a file with a here-document, you can use
cat << EOF > file.txtand type untilEOF– this avoids theCtrl+Dstep. - Remember that
catis not meant for editing; usenanoorvimfor that. - If you want to view a file page by page, use
lessinstead ofcat– it’s more efficient for long files.
Troubleshooting
- “No such file or directory� error. Check the spelling and path. Use
lsto see what files are in the current directory, and usepwdto confirm where you are. - “Permission denied� error. You may not have read permission. Use
ls -lto check permissions, and consider usingsudoonly if you truly need to (but be careful with system files). - File appears empty when you know it has content. The file might contain only null bytes or binary data. Use
file filenameto check its type. - Terminal displays garbage after viewing a binary file. Run
resetor close and reopen the terminal to fix the display. - Redirected output is empty. If you used
cat > fileand then pressedCtrl+Cinstead ofCtrl+D, the file may be empty. Recreate it with the correct ending.
Frequently Asked Questions
What does “cat� stand for?
It stands for “concatenate,� which means to link things together in a chain. The command was designed to concatenate files, but it’s also used to display them.
How do I view a file without scrolling?
Use less filename instead. It lets you scroll with arrow keys and quit with q.
Can I use cat to create a file with multiple lines?
Yes, type cat > file.txt, press Enter, type each line, and finish with Ctrl+D on a new line.
What’s the difference between cat and tac?
tac is the reverse of cat – it prints files in reverse order (last line first).
Why does cat show line numbers with -n but not with -b?
-n numbers all lines, including blank ones. -b numbers only non-blank lines, which is often more useful.
Is there a way to see hidden characters like tabs?
Yes, use cat -A to show tabs as ^I and mark line endings with $.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Google Desktop Search Widget on Windows 11
- How to Build a Computer Stand (Saturday Workshop)
- Customize Touch Keyboard Themes in Windows 11
- Excel Pivot Table: Change Multiple Field Settings
- WhatsApp: How to Block Unknown Numbers
- How to Exit Vi in Linux: A Simple Guide