Methods of payment Abuse

Linux cat Command: Working with File Contents

12.11.2025, 20:01

If you work with Linux servers, you constantly deal with files. Configuration files, logs, scripts - there's always something you need to check. And that's where cat comes in, probably one of the first commands you learn when starting with Linux.

The name stands for "concatenate", meaning to join things together. But most people just use it to quickly view files in the terminal. Though it can do much more - create files, merge multiple documents into one, and plenty of other useful stuff.

What cat actually does

This utility is part of the standard GNU coreutils package, so it's available by default in every Linux distribution. Main tasks include:

  • Display file contents on screen
  • Combine multiple files into one
  • Create new files right from command line
  • Append content to existing files
  • Number the lines in a document
  • Remove excessive blank lines

Sounds simple enough, but once you dig into all the details, you realize how powerful this tool really is for daily work.

Basic usage

The syntax couldn't be simpler:

cat [options] [file...]

Most common variant - just show a file:

cat filename.txt

That's it. Content gets dumped to the terminal and you can immediately see what's inside.

Viewing files

Let's start with the most common use case - when you need to quickly check what's in a file.

Single file

Say you need to check the hosts file:

cat /etc/hosts

Or verify nginx configuration:

cat /etc/nginx/nginx.conf

You can also check logs:

cat /var/log/syslog

Though be careful with logs - if the file is huge, all that text will flood your terminal. For large files, better use less or tail, we'll get to that later.

Multiple files at once

You can specify several files in a row:

cat file1.txt file2.txt file3.txt

Cat will output them one after another, no separators. Sometimes convenient when you need to quickly scan through several configs.

Real world example - check all nginx sites:

cat /etc/nginx/sites-available/*

Line numbering

Often useful to see line numbers. There's the -n option for that:

cat -n /etc/passwd

You'll get something like:

  1. root:x:0:0:root:/root:/bin/bash
  2. daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
  3. bin:x:2:2:bin:/bin:/usr/sbin/nologin

If the file has lots of blank lines and you don't want to number those, use the -b option:

cat -b file.txt

It'll only number lines with actual content, skipping empty ones.

Creating files

This is where it gets interesting. Cat doesn't just read, it writes too.

Create file from keyboard

Quick way to make a text file:

cat > newfile.txt

After that just start typing. Each line with Enter. When you're done, hit Ctrl+D and the file saves.

Done, test.txt is created.

Heredoc syntax

When you need to create a file with a script or config, heredoc is more convenient:

cat > config.txt << EOF
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
}
EOF

Everything between << EOF and EOF goes into the file. Very handy in bash scripts when you need to generate configs on the fly.

Empty file

You can create an empty file through cat:

cat > empty.txt < /dev/null

Though it's simpler with touch:

touch empty.txt

But the cat variant works too, good to know.

Merging files

This is what cat got its name for - concatenate, to join together.

Join two files

Let's say you have file1.txt and file2.txt, need to combine them:

cat file1.txt file2.txt > combined.txt

All content from the first file, then the second - everything ends up in combined.txt.

Collect all logs

Common task - gather all logs from a directory into one file:

cat /path/to/logs/*.log > all_logs.txt

You get one big file with all logs, convenient for analysis or sending to someone.

With separators

If you need to distinguish where one file ends and another begins:

cat file1.txt <(echo "---") file2.txt <(echo "---") file3.txt > merged.txt

Separators with three dashes will appear between files.

Append to end

Important point - the > operator completely overwrites the file. If you need to append to the end, use >>:

cat file1.txt >> existing_file.txt

Content from file1.txt gets added to the end of existing_file.txt, nothing gets erased.

Useful options

Let's go through the main cat flags.

-n - number all lines

cat -n file.txt

-b - number only non-empty lines

cat -b file.txt

-s - squeeze multiple blank lines into one

cat -s file.txt

Useful when file has tons of extra blank lines.

-E - show dollar sign at end of each line

cat -E file.txt

Helps see where lines actually end.

-T - show tabs as ^I

cat -T file.txt

-A - show all invisible characters

cat -A file.txt

Will show line endings ($), tabs (^I), and carriage returns (^M). The last one is especially useful when file was created in Windows.

-v - show non-printable characters, except tabs and newlines

cat -v file.txt

Practical example - remove duplicate blank lines:

cat -s messy_file.txt > clean_file.txt

Or check file for hidden characters:

cat -A suspicious_file.txt

Large files

Here cat has a problem. If the file weighs several gigabytes, the command will dump it all to your terminal. Not convenient.

There are several solutions.

First option - use less for paginated viewing:

cat large_file.log | less

Or without cat at all:

less large_file.log

Second option - look at just the beginning with head:

cat large_file.log | head -100

Third - just the end with tail:

cat large_file.log | tail -100

You can combine them to extract a chunk from the middle:

cat large_file.log | head -1000 | tail -100

Shows lines 901 through 1000.

Combining with other commands

Cat works well with other utilities through pipes.

With grep

Find lines with word "error" in file:

cat /var/log/syslog | grep -i error

Though you can manage without cat:

grep -i error /var/log/syslog

Works the same, second variant is slightly more efficient.

With wc

Count how many lines in file:

cat file.txt | wc -l

Or words:

cat file.txt | wc -w

Or characters:

cat file.txt | wc -c

With sort

Sort the contents:

cat names.txt | sort

Sort and remove duplicates:

cat names.txt | sort | uniq

With sed

Replace text on the fly:

cat file.txt | sed 's/old/new/g'

All instances of "old" become "new".

With awk

Extract first column from file:

cat data.txt | awk '{print $1}'

Real examples

A few scenarios from admin life.

Quick check of SSH config:

cat /etc/ssh/sshd_config

Create nginx config:

cat > /etc/nginx/sites-available/mysite.conf << EOF
server {
    listen 80;
    server_name mysite.com;
    
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host \$host;
    }
}
EOF

Collect logs from several days:

cat /var/log/nginx/access.log.1 /var/log/nginx/access.log > all_logs.txt

Add entry to log:

echo "New entry at $(date)" >> /var/log/myapp.log

Though here cat isn't needed at all, echo handles it itself.

Copy a file:

cat source.txt > destination.txt

Works like cp, but through redirection.

Check encoding:

cat -v file.txt

If you see weird ^M at line ends - file is from Windows, needs conversion.

Create bash script:

cat > backup.sh << 'EOF'
#!/bin/bash
tar -czf backup-$(date +%Y%m%d).tar.gz /home/user/data
EOF
chmod +x backup.sh

Quotes around EOF prevent variables from expanding immediately.

Alternatives

Depending on the task, there might be a better command.

For viewing files:

  • less - when file is large and you need navigation
  • more - simple paginated viewing
  • head - just the beginning
  • tail - just the end
  • nano or vim - if you need to edit too

For merging:

  • cat - simple text file concatenation
  • paste - merge files by columns
  • join - merge by common field

Performance

Cat works fast, but there are nuances.

When cat is inefficient:

  • Large files - better use head, tail, less
  • Binary files - need hexdump or xxd
  • Searching in files - grep works directly
  • Editing - sed, awk, editors

There's a concept called UUOC - Useless Use of Cat.

Inefficient:

cat file.txt | grep "search"

Efficient:

grep "search" file.txt

Difference is that in first case you create an extra process. Grep can read files itself.

Same with wc:

Inefficient:

cat file.txt | wc -l

Efficient:

wc -l < file.txt

Though honestly, for small files the difference is negligible. And the variant with cat is often clearer for beginners.

When to use cat

Let's sum up when cat fits and when it doesn't.

Use cat when:

  • Quickly check a small file
  • Combine several files
  • Create simple text file
  • Append something to end of file
  • Pass file to another command through pipe

Don't use cat when:

  • File is huge - grab less
  • Need to edit - open vim or nano
  • Need search - use grep directly
  • Binary file - need hexdump

Conclusion

The cat command is one of the basic Linux tools. Simple, fast, universal. You learn it on day one working with the terminal, use it every day for years.

Main things to remember:

cat file.txt - view file

cat file1 file2 > merged - merge files

cat > newfile - create new file

cat -n file.txt - with line numbers

Combine with grep, wc, sort for data processing

Once you master cat, you'll immediately feel more confident on the command line.

Additional info

Full documentation in man:

man cat

Or quick reference:

cat --help

Related commands worth learning:

  • less - paginated viewing
  • head - file beginning
  • tail - file end
  • tac - output in reverse order
  • nl - line numbering
  • paste - merge by columns

Useful command combinations

Config without comments:

cat config.conf | grep -v "^#"

Sort without duplicates:

cat list.txt | sort | uniq

Count unique lines:

cat file.txt | sort | uniq | wc -l

Replace in all files:

cat *.txt | sed 's/old/new/g'

That's all. Now you know more about cat than 90 percent of Linux users.