sodcalculator

10 Tech How-To

cat Command in Linux: How to Use It

Guide 10-13339 · Updated · Step by step

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

  1. Open a terminal. Press Ctrl+Alt+T on most Linux distributions, or search for “Terminal� in your applications menu.
  2. View a file’s contents. The simplest use is cat filename. For example, to see the contents of a file named notes.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 less instead for long files.

  3. Create a new file. Use cat > newfile.txt to create a file and type content into it. After typing, press Ctrl+D on a new line to save and exit. Example:
    cat > todo.txt
    Buy groceries
    Call dentist
    Ctrl+D

    This creates todo.txt with the two lines you typed.

  4. Append to an existing file. Use cat >> file.txt to add content to the end of a file without overwriting it. For instance:
    cat >> todo.txt
    Write report
    Ctrl+D

    Now todo.txt has three lines.

  5. 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.txt with the content of file1.txt followed by file2.txt.

  6. Copy a file. You can use cat to copy a file by redirecting its output:
    cat original.txt > copy.txt

    This creates copy.txt with the same content as original.txt.

  7. Display line numbers. Use the -n option to show line numbers:
    cat -n script.sh

    This is helpful when referencing specific lines in a file.

  8. Show non-printable characters. Use -A to display tabs as ^I and mark end-of-line with $:
    cat -A data.txt

    This helps spot hidden characters that might cause issues in scripts.

  9. Suppress repeated blank lines. Use -s to squeeze multiple blank lines into one:
    cat -s longfile.txt

    This makes output easier to read.

  10. Use cat with pipes. Pipe cat output 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 grep and wc can 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+D when creating a file. If you press Ctrl+C instead, the file may be empty or partially written.
  • Using cat on binary files. It will print gibberish and may mess up your terminal. Use file to check a file type first.
  • Viewing huge files with cat. It floods the screen. Use less or head/tail instead.
  • Not quoting filenames with spaces. If a file is named my notes.txt, you must quote it: cat "my notes.txt".

Tips

  • Use cat -n when you need to reference line numbers in a discussion or error report.
  • Combine cat with sort or uniq to process file contents: cat list.txt | sort | uniq.
  • To create a file with a here-document, you can use cat << EOF > file.txt and type until EOF – this avoids the Ctrl+D step.
  • Remember that cat is not meant for editing; use nano or vim for that.
  • If you want to view a file page by page, use less instead of cat – it’s more efficient for long files.

Troubleshooting

  • “No such file or directory� error. Check the spelling and path. Use ls to see what files are in the current directory, and use pwd to confirm where you are.
  • “Permission denied� error. You may not have read permission. Use ls -l to check permissions, and consider using sudo only 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 filename to check its type.
  • Terminal displays garbage after viewing a binary file. Run reset or close and reopen the terminal to fix the display.
  • Redirected output is empty. If you used cat > file and then pressed Ctrl+C instead of Ctrl+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

The whole Tech How-To bed →