DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Using `cat` in Linux: Read, Combine, and Redirect Files Safely

Need to inspect a small config file, combine text files, or pass file contents to another command? Linux’s cat can do all three. The key is understanding that cat writes to standard output, while the shell decides where that output goes.

Read files and join their contents

The name cat is short for “concatenate.” It reads the files you name, in order, and writes their contents to standard output—usually your terminal.

cat notes.txt
cat part1.txt part2.txt
Enter fullscreen mode Exit fullscreen mode

The second command prints part1.txt immediately followed by part2.txt. cat doesn’t add filenames or separators, so if one file doesn’t end with a newline, the next file’s first line may run right after it.

With no filenames, cat reads from standard input instead. In a terminal, type some text and press Ctrl+D to signal that input is finished. This is useful to know if cat seems to be hanging: it may simply be waiting for input.

Use options to inspect formatting

A few options help when you need to refer to lines or investigate whitespace:

cat -n config.txt   # Number every line
cat -b config.txt   # Number non-empty lines only
cat -s notes.txt    # Squeeze repeated blank lines in the output
cat -T table.txt    # Show tabs as ^I
cat -E notes.txt    # Mark line endings with $
Enter fullscreen mode Exit fullscreen mode

On GNU cat, -A displays tabs, line endings, and other non-printing characters. For example:

cat -A config.txt
Enter fullscreen mode Exit fullscreen mode

This can help reveal formatting that’s otherwise hard to see. These display options don’t modify the file. Implementations can differ across systems, so check cat --help or man cat if an option isn’t recognized.

If you’re trying to locate a word or pattern in a file, use grep directly rather than piping the whole file through cat first:

grep -n 'error' application.log
Enter fullscreen mode Exit fullscreen mode

The -n option includes matching line numbers. For more ways to search files and patterns, see this guide to using grep in Linux.

Redirect output—but watch for overwrites

The shell handles redirection before cat runs. A single > creates a destination file or truncates it if it already exists:

cat header.txt body.txt > combined.txt
Enter fullscreen mode Exit fullscreen mode

Use >> to append instead:

cat new-entries.txt >> archive.txt
Enter fullscreen mode Exit fullscreen mode

That distinction matters when the destination contains data you need to keep. In particular, don’t redirect a file’s contents back into the same file:

cat notes.txt > notes.txt
Enter fullscreen mode Exit fullscreen mode

The shell opens and truncates notes.txt before cat reads it. The original contents can be lost. Choose a different destination when replacing is intended, and use >> only when you mean to append.

For a short, new text file, you can also type input into cat and redirect it:

cat > notes.txt
Remember to check the deployment log.
Enter fullscreen mode Exit fullscreen mode

Press Ctrl+D when you’re done. This replaces the destination’s contents; it isn’t an editing method. Use a text editor to revise existing text. For other ways to create files in Linux, including how their overwrite behavior differs, choose the method that fits the job.

Don’t use cat for every file task

cat is handy for short files and for passing data through a pipeline. For a long file, printing everything directly into the terminal can make it difficult to find what you need. Pick a tool based on the task:

  • Use less file.log to scroll through and search a large file. Press q to quit.
  • Use head file.log to inspect the beginning.
  • Use tail file.log to inspect the end.
  • Use grep 'error' file.log to find matching lines.

You also don’t need cat just to give a filename to a command that already accepts one. For example, prefer sort names.txt over cat names.txt | sort. Pipes are useful when one command’s output needs to become another command’s input, but unnecessary cat processes usually make a command harder to read.

A couple of quick troubleshooting checks

If you see “No such file or directory,” check the filename and path. pwd shows your current directory, and ls can help you check what’s there. If a filename in the current directory starts with a hyphen, write it as a path so it can’t be mistaken for an option:

cat ./-draft.txt
Enter fullscreen mode Exit fullscreen mode

In practice, the useful rule is simple: use cat to print or combine data, use redirection deliberately, and switch to a pager or a more specific command when you need to navigate or search.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)