Read a Short File with cat

Read a Short File with cat

A teacher wants a quick look at a short student list. There is no need to open an editor, scroll through a long interface or save any changes. cat can read the file and write its contents to standard output, which your terminal normally displays.

The name comes from concatenation: the utility can also join the contents of several inputs. Here we will keep it simple and read one small file. For a report longer than the screen, a pager will usually be a more comfortable choice.

What you’ll learn

  • Display a small text file with cat.
  • Avoid redirecting output into the same file being read.

Saved text flows into terminal output

  1. Input file

    The class list contains two lines of text.

    Diya; Saira
  2. Read

    cat reads the file and writes its bytes to standard output.

    cat students.txt
  3. Display

    The terminal displays the two lines; cat has not edited the file.

    Diya then Saira
cat is convenient for short, known text and for connecting file contents to other commands.

Print a two-student class list

Prepare the example

Use a new practice folder. The first line creates a two-line student list, so the next command has an actual input to read. We are displaying known text; do not use the same approach blindly for arbitrary binary files.

Bash · Linux terminal

printf 'Diya\nSaira\n' > students.txt
cat students.txt

Expected result

Diya
Saira

printf saves Diya and Saira on separate lines in students.txt. cat students.txt reads those bytes and writes them out in the same order. It does not open an editing session or change the student list merely by reading it.

Be careful about similar-looking commands. cat > students.txt waits for input and directs it into that file, replacing its existing contents. cat students.txt > students.txt is worse for preservation: the shell truncates the file before cat reads it. The command name alone does not describe the operation; the redirection changes the job.

Read, concatenate and number short text

cat prints file contents directly to standard output. It is ideal for short text files that fit comfortably in your terminal window.

What is cat?

cat stands for “concatenate” — but don't let that word scare you. Most of the time, we use cat to simply view the contents of a file.

Basic Usage

Let’s start with a very simple example. Imagine you have a file called notes.txt. To view its contents, you just type:

cat notes.txt

And the output might look something like this:

This is my Linux learning journal.
Day 1: Learned about files and directories.
Day 2: Discovered the magic of the cat command!

Create a Sample File (if you don’t have one)

If you don’t have a file yet, let’s make one quickly:

echo "Hello, Linux world!" > hello.txt

Now view it:

cat hello.txt
Hello, Linux world!

View Multiple Files

You can also view multiple files at once. Just separate them by space:

cat file1.txt file2.txt

Number the Lines in the Output

To number each line, use the -n option:

cat -n notes.txt
     1  This is my Linux learning journal.
     2  Day 1: Learned about files and directories.
     3  Day 2: Discovered the magic of the cat command!

Caution with Large Files

If the file is huge, cat will dump everything at once. In such cases, it’s better to use less or more. We’ll cover those in another tutorial!

Use cat for short files and simple output

So, whenever you want a quick look at what’s inside a text file — whether it's logs, notes, or config files — just say:

cat filename.txt

And you're done!

Try it, then check the worked answer

Your task: Read a small text file without editing it.

Use a new practice folder. The first line creates a two-line student list, so the next command has an actual input to read. We are displaying known text; do not use the same approach blindly for arbitrary binary files.

Bash · complete example for this question

printf 'Diya\nSaira\n' > students.txt
cat students.txt

Expected result

Diya
Saira

Worked answer

cat students.txt

Choose the plain read form, cat students.txt. There is no output redirection because the terminal is where you want the result. If you add > with the same filename, you are preparing a write destination rather than protecting the report for reading.

Check your decision

Read a small text file without editing it.
Choose one answer. You can try again.

Answer and explanation

cat students.txt

Choose the plain read form, cat students.txt. There is no output redirection because the terminal is where you want the result. If you add > with the same filename, you are preparing a write destination rather than protecting the report for reading.

Read the file without redirecting over it

cat is convenient for short, known text and for connecting file contents to other commands. Pay attention to redirection, which the shell handles before the utility starts. Use a pager for longer text and an appropriate inspection tool for data that is not meant to be displayed as text.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Read a Long File with less →