cat Command in Linux with Examples

The cat command prints file contents to standard output. It is commonly used to view small text files, combine multiple files, create quick text files, and send file contents into pipelines.
When no file is given, cat reads from standard input. When multiple files are given, it prints them in the same order, which is why the name comes from concatenate.
This guide explains the most common cat command examples in Linux, including redirection, line numbers, hidden characters, and when to use tools such as less, head, or tail instead.
cat Command Syntax
The basic cat syntax is:
cat [OPTIONS] [FILE_NAMES]OPTIONS- Optional flags that change howcatprints the content.FILE_NAMES- One or more files to read. If no file is given,catreads from standard input.
Use cat --help or the cat manual page
to view all available options.
Displaying File Contents
The most basic and common usage of the cat command is to read the contents of files.
For example, the following command will display the contents of the /etc/issue file on the terminal:
cat /etc/issue
Redirect Contents of File
Instead of displaying the output to stdout (on the screen), you can redirect it to a file.
The following command will copy the contents of file1.txt to file2.txt using the (>) operator:
> operator overwrites the destination file. Use >> when you want to append instead.cat file1.txt > file2.txtcp
command to copy a file.If the file2.txt file does not exist, the command will create it. Otherwise, it will overwrite the file.
Use the (>>) operator to append the contents
of file1.txt to file2.txt:
cat file1.txt >> file2.txtSame as before, if the file is not present, it will be created.
Print Line Numbers
To display file contents with line numbers, use the -n option:
cat -n /etc/os-release1 NAME="Ubuntu"
2 VERSION="24.04 LTS (Noble Numbat)"
3 ID=ubuntu
4 VERSION_CODENAME=nobleThe -b option is similar, but it numbers only non-empty lines:
cat -b file.txtSuppress Repeated Empty Lines
Use the -s option to squeeze repeated empty lines into a single empty line:
cat -s file.txtDisplay Hidden Characters
The cat command can show characters that are usually invisible in the terminal.
To display tab characters as ^I, use -T:
cat -T /etc/hosts127.0.0.1^Ilocalhost
127.0.1.1^IlinuxizeThe TAB characters will be displayed as ^I.
To show $ at the end of each line, use -E:
cat -E /etc/os-releaseNAME="Ubuntu"$
VERSION="24.04 LTS (Noble Numbat)"$
ID=ubuntu$
VERSION_CODENAME=noble$To show tabs, line endings, and other non-printing characters together, use -A:
cat -A file.txtConcatenating Files
When passing two or more file names as arguments to the cat command, the contents of the files will be concatenated. cat reads the files in the sequence given in its arguments and displays the file’s contents in the same sequence.
For example, the following command will read the contents of file1.txt and file2.txt and display the result in the terminal:
cat file1.txt file2.txtYou can concatenate two or more text files and write them to a file.
The following command will concatenate the contents of file1.txt and file2.txt and write them to a new file combinedfile.txt using the (>) operator:
cat file1.txt file2.txt > combinedfile.txtIf the combinedfile.txt file does not exist, the command will create it. Otherwise, it will overwrite the file.
To concatenate the contents of file1.txt and file2.txt and append the result to combinedfile.txt, use the (>>) operator:
cat file1.txt file2.txt >> combinedfile.txtIf the file is not present, it will be created.
You can combine concatenation with options from the previous section, for example cat -n file1.txt file2.txt to number lines in the combined output.
Creating Files
Creating small files with cat is often faster than opening a text editor.
To create a new file, use the cat command followed by the redirection operator (>) and the name of the file you want to create. Press Enter, type the text, and once you are done, press CTRL+D to save the file.
In the following example, we are creating a new file named file1.txt:
cat > file1.txtIf a file named file1.txt exists, it will be overwritten.
Use the >> operator to append the output to an existing file.
cat >> file1.txtUse CTRL+D on a new line to finish writing input and return to the shell.
Using cat with Pipes
The cat command is often used with pipes to pass file contents to another command. For example, you can display a log file and search for a pattern with grep
:
cat /var/log/syslog | grep "error"For a single file, this shorter form is usually better:
grep "error" /var/log/syslogTo count the number of lines in a file, you can pipe cat to wc
:
cat file.txt | wc -lThe direct form is simpler when you do not need a pipeline:
wc -l file.txtViewing Large Files
While cat is useful for small files, it is not ideal for large files because it prints all content to the terminal at once.
For large files, consider using:
less- View file contents one screen at a time with scrollingmore- Similar tolessbut with fewer featureshead- Display the first lines of a filetail- Display the last lines of a file
less /var/log/syslog
head -20 file.txt
tail -50 file.txtPrint File in Reverse with tac
The tac command is the reverse of cat. It prints the file contents in reverse order, starting from the last line:
tac file.txtThis is useful when you want to view log files with the most recent entries first.
Troubleshooting
Permission denied
Check the file permissions with ls -l file.txt. If the file requires elevated privileges, read it with sudo cat file.txt.
The output is too long
Use less, head, or tail instead of printing the entire file to the terminal.
The terminal shows strange characters
The file may be binary or contain non-printing characters. Check it with the file
command before printing it.
A file was overwritten
The > operator overwrites the destination file. Use >> to append, or enable shell noclobber if you want protection against accidental overwrites.
Hidden characters break a script
Use cat -A file or cat -vET file to show tabs, carriage returns, and other non-printing characters.
Quick Reference
For a printable quick reference, see the cat cheatsheet .
| Command | Description |
|---|---|
cat file.txt | Display file contents |
cat file1 file2 | Display multiple files in order |
cat -n file.txt | Display all lines with line numbers |
cat -b file.txt | Number only non-empty lines |
cat -s file.txt | Suppress repeated empty lines |
cat -A file.txt | Show hidden characters |
cat file1 > file2 | Copy file contents and overwrite destination |
cat file1 >> file2 | Append to file |
cat > file.txt | Create or overwrite a file |
cat >> file.txt | Append typed input to a file |
tac file.txt | Display file in reverse |
Conclusion
The cat command is best for viewing small text files, combining files, and sending file contents into simple pipelines. For long files, use less
, head
, or tail
so the output stays manageable.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page