Concatenating and viewing file contents directly in the terminal is what the cat command does in Linux. If you are new to Linux or just brushing up on command-line tools, understanding what does the cat command do in linux is a great starting point. It is one of the most basic yet powerful commands you will use daily.
The cat command stands for “concatenate.” It lets you read, combine, and create files right from your terminal. Think of it as a quick way to peek inside a file without opening a full editor.
In this guide, we will break down everything about the cat command. You will learn its syntax, common uses, and practical examples. By the end, you will feel confident using cat for everyday tasks.
What Does The Cat Command Do In Linux
At its core, the cat command reads files and outputs their content to the terminal. You can use it to view a single file, combine multiple files, or even create new ones. It is simple but versatile.
Here is the basic syntax:
cat [options] [file1] [file2] ...
When you run cat without any options, it just prints the file content to your screen. For example:
cat notes.txt
This shows everything inside notes.txt. If the file is long, the text scrolls fast. You can combine cat with other commands like less or more to control the output.
Common Uses Of The Cat Command
Let us look at the most frequent ways people use cat. These are the tasks you will perform almost every day.
Viewing File Contents
This is the simplest use. Just type cat followed by the file name.
- Quickly check a configuration file
- Read a short text file
- Verify the content of a script
For example:
cat /etc/passwd
This shows the user account information. It is a common way to inspect system files.
Creating New Files
You can create a file directly from the terminal using cat. This is handy for quick notes or scripts.
- Type
cat > newfile.txt - Press Enter
- Type your content
- Press Ctrl+D to save and exit
This overwrites the file if it exists. To append instead, use cat >> newfile.txt.
Concatenating Multiple Files
As the name suggests, cat can join files together. This is useful for combining logs or reports.
cat file1.txt file2.txt > combined.txt
This merges file1 and file2 into a new file called combined.txt. The order matters—cat reads files left to right.
Copying File Content
You can use cat with output redirection to copy files. It is like the cp command but with more control.
cat source.txt > destination.txt
This copies the content of source.txt to destination.txt. If destination exists, it gets overwritten.
Important Cat Options
Cat has several options that change its behavior. These are the ones you will use most often.
- -n: Number all output lines
- -b: Number non-blank lines only
- -s: Squeeze multiple blank lines into one
- -E: Show $ at the end of each line
- -T: Show TAB characters as ^I
For example, to view a file with line numbers:
cat -n script.sh
This is great for debugging or referencing specific lines.
Using Cat With Pipes
Cat works well with other commands through pipes. You can send its output to grep, sort, or wc.
cat log.txt | grep "error"
This finds all lines containing “error” in log.txt. It is a fast way to search files.
Another example:
cat data.txt | wc -l
This counts the number of lines in data.txt.
Practical Examples You Can Try
Let us walk through some real-world scenarios. These examples will help you understand cat better.
Example 1: Viewing A Configuration File
Suppose you want to check your SSH config:
cat ~/.ssh/config
This shows your SSH settings. If the file is long, use cat ~/.ssh/config | less to scroll.
Example 2: Combining Log Files
You have two log files from different servers. Combine them into one:
cat server1.log server2.log > all_logs.log
Now you can search through one file instead of two.
Example 3: Creating A Simple Script
Create a bash script quickly:
cat > hello.sh
#!/bin/bash
echo "Hello, World!"
Press Ctrl+D. Then make it executable with chmod +x hello.sh.
Example 4: Displaying With Line Numbers
When debugging code, line numbers help:
cat -n app.py
This prints the Python file with numbers. You can then reference line 42, for example.
Cat Vs Other Commands
You might wonder when to use cat versus other tools. Here is a quick comparison.
- cat: Best for small files or combining content
- less: Better for large files because it paginates
- head: Shows only the first few lines
- tail: Shows the last few lines
- tac: Reverse the output (cat backwards)
For example, if a file has 1000 lines, cat will dump it all at once. Use less to scroll through it comfortably.
When Not To Use Cat
Cat is not always the best choice. Avoid it in these situations:
- Very large files (use less or more)
- Binary files (output will be garbled)
- When you need to edit (use nano or vim)
Also, do not use cat with pipes unnecessarily. Some people write cat file | grep pattern when grep pattern file works fine. The latter is more efficient.
Advanced Cat Techniques
Once you master the basics, try these advanced uses.
Using Cat With Here Documents
You can create multi-line files without typing each line separately:
cat << EOF > script.sh
#!/bin/bash
echo "This is a script"
EOF
This is useful for generating files in scripts.
Concatenating With Standard Input
Cat can read from standard input and combine it with files:
echo "Header" | cat - file.txt
This prepends “Header” to the content of file.txt. The dash (-) represents standard input.
Numbering Only Non-Empty Lines
Use the -b option to skip blank lines:
cat -b notes.txt
This is cleaner for documents with many empty lines.
Common Mistakes With Cat
Even experienced users make errors. Here are pitfalls to avoid.
- Forgetting to redirect output, so it prints to screen instead of saving
- Using cat on binary files, which can mess up your terminal
- Overwriting files accidentally with a single > instead of >>
- Not using quotes around file names with spaces
For example, if you run cat file1 file2 without redirection, it just shows both files. To save, add > output.txt.
Cat And File Permissions
Cat respects file permissions. If you do not have read access, you get an error.
cat /etc/shadow
This usually fails because only root can read it. Use sudo if needed:
sudo cat /etc/shadow
Be careful with sensitive files.
Using Cat In Scripts
Cat is common in shell scripts. It can read configuration files or generate output.
#!/bin/bash
config=$(cat /etc/myapp.conf)
echo "Config loaded"
This loads the file content into a variable. You can then process it.
Reading Multiple Files In A Loop
You can iterate over files and cat each one:
for file in *.txt; do
cat "$file"
done
This prints all text files in the current directory.
Cat And Special Characters
Cat handles special characters like tabs and newlines. Use the -T and -E options to visualize them.
cat -TE file.txt
This shows tabs as ^I and marks line ends with $. It helps debug formatting issues.
Performance Considerations
Cat is efficient for small to medium files. For huge files, it uses memory and CPU. If you have a 1GB log file, consider using less or tail instead.
Also, avoid cat in pipelines when possible. For example, cat file | command can be replaced with command < file. This reduces overhead.
Cat On Different Linux Distributions
Cat works the same on all Linux distros. Ubuntu, Fedora, Debian, Arch—all have the same cat command. It is part of GNU coreutils, so it is always available.
Some minimal systems might have a simpler version, but the basics are identical.
Learning Cat With Practice
The best way to learn is by doing. Open your terminal and try these:
- Create a file with cat
- View it with cat
- Combine two files
- Use options like -n and -b
- Pipe output to grep
Experiment with different files. The more you use cat, the more natural it becomes.
Cat And Other Unix Tools
Cat pairs well with many commands. Here are common combinations:
- cat file | sort
- cat file | uniq
- cat file | awk '{print $1}'
- cat file | sed 's/old/new/g'
These let you process text quickly without opening an editor.
Common Questions About Cat
Here are answers to frequent queries. These will clear up any confusion.
Can Cat Edit Files?
No, cat only reads or creates files. It cannot edit existing content. Use nano, vim, or sed for editing.
Does Cat Work With Directories?
No, cat only works with regular files. If you try cat mydir, you get an error. Use ls for directories.
What Is The Opposite Of Cat?
The tac command reverses the output. It prints lines in reverse order.
Cat In The Real World
System administrators use cat daily. They check logs, combine reports, and create quick files. Developers use it to view code or generate test data.
For example, a sysadmin might run:
cat /var/log/syslog | grep "error" | tail -20
This shows the last 20 errors in the system log.
Alternatives To Cat
If cat does not fit your needs, try these:
- bat: A modern clone with syntax highlighting
- head/tail: For partial file viewing
- less: For interactive scrolling
- more: An older pager
Bat is especially popular because it adds colors and line numbers by default.
Summary Of Cat Command
To recap, cat is a simple but essential tool. It views, combines, and creates files. You can use it with options for numbering or special character display.
Remember these key points:
- Use cat for small files
- Redirect output to save
- Combine with pipes for power
- Avoid on binary files
With practice, cat will become second nature. It is one of the first commands any Linux user should learn.
Frequently Asked Questions
What Is The Main Purpose Of The Cat Command In Linux?
The main purpose is to concatenate and display file contents. You can view, combine, or create files directly from the terminal.
Can I Use Cat To Edit A File?
No, cat cannot edit files. It only reads or creates them. For editing, use a text editor like nano or vim.
How Do I Create A File With Cat?
Type cat > filename, then type your content, and press Ctrl+D to save. Use cat >> filename to append.
What Does The -N Option Do In Cat?
The -n option numbers all output lines. It is useful for referencing specific lines in a file.
Is Cat Safe To Use On Binary Files?
It is not recommended. Binary files will display garbled text and may mess up your terminal. Use hexdump or od instead.
Now you know exactly what does the cat command do in linux. Go ahead and try it out. Your terminal is waiting.