When you use the cat command in Linux, it concatenates files and prints their output to the screen. If you are wondering what does cat command do in linux, the answer is simple: it reads data from files and displays their content right in your terminal. This command is one of the most basic yet powerful tools for anyone working with the Linux command line.
Think of cat as your go-to for quickly peeking inside a file without opening a full editor. It works with text files, scripts, and even binary data, though it shines best with plain text. You will find yourself using it daily for checking logs, reading configs, or combining multiple files into one.
In this guide, we break down everything you need to know about the cat command. From basic usage to advanced tricks, you will learn how to make it work for you. No fluff, just practical steps you can use right away.
What Does Cat Command Do In Linux
The cat command gets its name from the word “concatenate.” Its primary job is to read files and output their contents to the standard output, which is usually your terminal screen. But it does much more than just display text.
Here is a quick list of what cat can do:
- Show the entire content of one or more files
- Combine multiple files into a single output
- Create new files directly from the terminal
- Copy content from one file to another
- Number lines in a file for easier reading
- Suppress repeated blank lines for cleaner output
When you run cat filename, it reads the file from start to finish and prints everything to your screen. This makes it perfect for small files or quick checks. For larger files, you might prefer tools like less or more, but cat remains the fastest option for short content.
Basic Syntax Of The Cat Command
The basic syntax is straightforward:
cat [options] [file1] [file2] ...
You can pass one or multiple files as arguments. If you do not specify a file, cat reads from standard input, which means it waits for you to type something. Press Ctrl+D to end input and see your text printed back.
Common options include:
-n: Number all output lines-b: Number non-empty lines only-s: Squeeze multiple blank lines into one-E: Show a dollar sign at the end of each line-T: Display tab characters as ^I
How To View File Contents With Cat
To view a single file, simply type:
cat /etc/hostname
This prints the hostname file directly to your terminal. For multiple files, list them in order:
cat file1.txt file2.txt file3.txt
Cat will concatenate them and show everything as one continuous output. This is useful for combining log files or reading related configs together.
You can also use wildcards to match patterns:
cat *.log
This displays all files ending with .log in the current directory. Be careful with large numbers of files, as the output can become overwhelming.
Creating Files With Cat Command
One of the most practical uses of cat is creating new files directly from the terminal. You do not need a text editor for simple files.
To create a file, use redirection:
cat > newfile.txt
After running this command, cat waits for your input. Type your content line by line. Press Ctrl+D on a new line to save and exit. This creates the file if it does not exist, or overwrites it if it does.
To append content to an existing file instead of overwriting it, use double redirection:
cat >> existingfile.txt
Now anything you type gets added to the end of the file. This is great for adding notes or log entries.
Copying Files With Cat
You can copy a file using cat and output redirection:
cat source.txt > destination.txt
This reads source.txt and writes its content to destination.txt. If destination.txt already exists, it gets overwritten. To copy without overwriting, use append mode:
cat source.txt >> destination.txt
This adds the content of source.txt to the end of destination.txt. It works like the cp command but gives you more control over how content is combined.
Combining Multiple Files Into One
Combining files is where cat really shines. Use redirection to merge several files into a single output file:
cat file1.txt file2.txt file3.txt > combined.txt
This creates combined.txt with the contents of all three files in order. You can use this for merging configuration files, combining chapters of a document, or aggregating log data.
For even more control, use wildcards:
cat chapter*.txt > full_book.txt
This merges all files matching the pattern into one big file. Just make sure the files are in the correct order, as cat processes them alphabetically by default.
Useful Cat Options And Examples
Let us explore some practical options that make cat even more useful. These are common tasks you will encounter daily.
Numbering Lines With -N And -B
The -n option adds line numbers to the output:
cat -n script.sh
This shows each line with a number, starting from 1. It is helpful for referencing specific lines in a file, especially when debugging scripts.
The -b option numbers only non-empty lines:
cat -b notes.txt
Blank lines get skipped in the numbering. This gives you cleaner output when the file has intentional empty sections.
Squeezing Blank Lines With -S
Files with many blank lines can be hard to read. The -s option collapses consecutive blank lines into one:
cat -s longfile.txt
This makes the output more compact while preserving the structure. It is great for logs that have large gaps between entries.
Showing Special Characters With -E And -T
Sometimes you need to see hidden characters. The -E option shows a dollar sign at the end of each line:
cat -E file.txt
This helps identify trailing spaces or line endings. The -T option displays tab characters as ^I:
cat -T tabbed_file.txt
Combining both options gives you full visibility into the file’s structure:
cat -ET file.txt
Piping Cat Output To Other Commands
Cat becomes even more powerful when you pipe its output to other commands. This is a core concept in Linux that lets you chain tools together.
For example, to search for a word in a file:
cat logfile.txt | grep "error"
This sends the file content to grep, which filters only lines containing “error”. You can pipe to any command that accepts standard input.
To count lines, words, and characters:
cat file.txt | wc
To sort the content alphabetically:
cat names.txt | sort
To view the first 10 lines:
cat longfile.txt | head
To view the last 20 lines:
cat longfile.txt | tail -n 20
Piping is efficient because it processes data on the fly without creating temporary files. You can build complex workflows by chaining multiple commands together.
Using Cat With Here Documents
Cat can work with here documents to create multi-line strings directly in scripts. This is useful for generating files or output from within a script.
Example:
cat << EOF > config.txt
server_name example.com
port 8080
enable_logging true
EOF
This creates config.txt with the three lines between the delimiters. The EOF marker can be any word, but it must appear alone on the final line.
Here documents are common in shell scripts for generating configuration files, HTML templates, or any structured text.
Common Mistakes And How To Avoid Them
Even experienced users make mistakes with cat. Here are the most common pitfalls and how to steer clear of them.
- Accidentally overwriting files: Using single
>redirection overwrites the destination file. Always double-check your command, or use>>to append. - Viewing binary files: Cat can display binary files, but the output will be gibberish and may mess up your terminal. Use
hexdumporxxdfor binary data. - Forgetting to close input: When creating files with
cat > file, you must press Ctrl+D to end input. If you forget, the command hangs waiting for more text. - Using cat unnecessarily: Many commands can read files directly. For example,
grep pattern fileworks without cat. Avoid useless use of cat. - Large files: Cat reads the entire file into memory and outputs it all at once. For huge files, use
lessortailto avoid overwhelming your terminal.
Alternatives To The Cat Command
While cat is versatile, other commands may be better suited for specific tasks. Here are some alternatives you should know.
- less: For viewing large files page by page. Allows scrolling both forward and backward.
- more: Similar to less but with fewer features. Good for basic paging.
- head: Shows only the first few lines of a file. Default is 10 lines.
- tail: Shows the last few lines. Great for checking recent log entries.
- tac: Like cat but reverses the order of lines. Useful for reading files backwards.
- rev: Reverses each line character by character. Niche but handy.
Choose the right tool based on your needs. For quick peeks, cat is fine. For browsing, use less. For monitoring logs, tail -f is your friend.
Practical Examples For Daily Use
Let us walk through some real-world scenarios where cat saves the day.
Checking System Files
Linux stores system information in plain text files. Use cat to read them:
cat /etc/os-release shows your OS version.
cat /proc/cpuinfo displays CPU details.
cat /proc/meminfo shows memory usage.
These files are updated in real time, so cat gives you a snapshot of current system state.
Combining Log Files For Analysis
If you have multiple log files from different servers, combine them for easier analysis:
cat server1.log server2.log server3.log > all_logs.txt
Then use grep to search across all logs at once:
cat all_logs.txt | grep "ERROR 404"
This saves time compared to checking each file individually.
Creating Quick Notes
Need to jot down a quick note without opening an editor? Use cat:
cat > todo.txt
Type your tasks, then Ctrl+D. Done.
To add more later:
cat >> todo.txt
Type new tasks, then Ctrl+D.
Displaying File With Line Numbers For Debugging
When debugging a script, line numbers help pinpoint issues:
cat -n script.sh
You can then reference line 42 when telling someone about an error. This is much clearer than saying “somewhere in the middle.”
Frequently Asked Questions
What is the main purpose of the cat command in Linux?
The main purpose is to concatenate files and display their contents on the terminal. It reads files sequentially and outputs them to standard output, making it ideal for viewing small files, combining multiple files, and creating new files quickly.
Can the cat command be used to edit files?
No, cat cannot edit files. It only reads and outputs content. To edit files, use text editors like nano, vim, or gedit. However, cat can create new files or overwrite existing ones using output redirection.
How do I stop the cat command if it outputs too much text?
Press Ctrl+C to interrupt the command and return to the prompt. For large files, consider using less or more instead, which allow paging through content without flooding your screen.
What does the cat command do when used without any arguments?
Without arguments, cat reads from standard input. It waits for you to type text, then echoes it back after you press Ctrl+D to end input. This is useful for quick tests or creating short files.
Is the cat command safe to use with binary files?
Technically yes, but the output will be unreadable and may corrupt your terminal display. Use hexdump, xxd, or od for binary files to see their contents in a readable format.
Tips For Mastering The Cat Command
Here are some final tips to help you use cat like a pro.
- Combine options for maximum effect. For example,
cat -ns file.txtnumbers lines and squeezes blanks. - Use cat with process substitution to compare outputs:
cat <(command1) <(command2). - Redirect output to a printer with
cat file.txt > /dev/lp0if you have a printer connected. - Use cat to display multiple files with headers:
for f in *.txt; do echo "=== $f ==="; cat "$f"; done. - Remember that cat is not always the best choice. For large files, use less. For the end of a file, use tail.
With practice, cat becomes second nature. It is a simple command, but its flexibility makes it indispensible for daily Linux work. Whether you are a beginner or a seasoned sysadmin, mastering cat will speed up your workflow and make you more efficient at the command line.
Now you know what does cat command do in linux. Go ahead and try these examples on your own system. The best way to learn is by doing, so open a terminal and start experimenting with cat today.