How to View the Content of File in Linux | cat Command
The cat (concatenate) command in Linux is used to view, create, and combine file contents directly from the terminal.
- Primarily used to read and display the contents of files on the terminal.
- Can concatenate multiple files and display them as a single continuous output.
- Allows users to create new files or append data to existing ones.
- Useful for quick file inspection and merging without opening a text editor.
Practical Examples of cat Command in Linux
Now let’s see some practical ways to use the cat command to read, create, or manage text files in your Linux terminal.
1. How to View the Content of a Single File in Linux
The most basic use of 'cat' is to display the contents of a file on the terminal. This can be achieved by simply providing the filename as an argument:
Syntax:
cat file_nameExample: If our file_name = jayesh.txt
cat jayesh.txt
Note: ls command is used to display all files and directories in the current location.
2. How to View the Content of Multiple Files in Linux
This command will display the contents of both files one after the other in the terminal, starting with file1 followed by file2.
Syntax:
cat file_name1 file_name2Example: If we have two files , file1 and file2.
cat file1 file2
3. How to Create a file and add content in Linux Using `cat` Command
If you want to create a new file or overwrite an existing file with new content, you can use 'cat' with the output redirection (`>`):
Syntax:
cat > newfile_nameExample: If we want to create a newfile_name = jayesh1.
cat > jayesh1lsThis will allow you to type text directly into the terminal, and when you press Ctrl + D, the entered text will be saved to new_file.txt.
`ls` command is used to display all files and directories in the current location.

4. How to Copy the Contents of One File to Another File in Linux
As the name suggests, 'cat' can concatenate multiple files into a single file.This example illustrates how to copy the entire content of "file1" into "file2" using the cat command along with redirection (>).
Syntax:
cat file1.txt file2.txt > merged_file.txt This command combines the content of file1.txt and file2.txt into a new file named merged_file.txt.
Options of cat Command
Here are the commonly used options of the cat command in Linux:
Syntax of cat Command
The basic syntax of the cat command is as follows:
cat [OPTION] [FILE]Here,
- [OPTION] : represents various command-line options.
- [FILE] : the name of the file(s) to be processed. Let's explore some of the common uses of 'cat' along with examples.
Examples
Here are some examples of the cat command using various options:
1. Viewing Contents of a File Preceded with Line Numbers in Linux
Adding the -n option to cat introduces line numbers, making it convenient to identify and reference specific lines within the file.
Syntax:
cat -n file_nameCommand: If our file_name is file2.
cat -n file2
Here, the cat command, used with the redirection (>), allows you to create a new file named "jayesh1" and input content directly into it. The subsequent ls command lists all files in the current location.
2. Cat command can suppress repeated empty lines in output
The -s option comes in handy when dealing with files containing repeated empty lines. It suppresses these repetitions, providing a cleaner output.
Syntax:
cat -s file_nameOutput
Will suppress repeated empty lines in output3. Appending the Contents of One File to the End of Another File
If you want to add the content of one file to another, 'cat' can be used along with the append (>>) operator:
Syntax:
cat file_name1 >> file_name2Example:
cat file1 >> file2This will append the content of `file1` to the end of `file2`

4. How to Display Content in Reverse Order Using `tac` Command in Linux
The 'tac' command is the reverse of 'cat' and is used to display the content of a file in reverse order. The syntax is simple:
Syntax:
tac file_nameExample:
This command will print the content of 'file2' in reverse order, displaying the last line first, followed by the second-to-last line, and so on.
tac file2
9. How to Highlight the End of Line in Linux
The '-E' option in the 'cat' command is used to highlight the end of each line.
Syntax:
cat -E "filename"Output:

This will display the content of 'jayesh1' with a '$' character at the end of each line, indicating the line's terminate.
10. `-A` Command Line Option in `cat` Command in Linux
The '-A' option allows you to combine the effects of '-v', '-E', and '-T' options. Instead of writing '-vET' in the command, you can use '-A':
Syntax:
cat -A "filename"Explanation:
- The -v option creates non-printing characters visible (except for tabs and line breaks).
- The -E option emphasizes the end of each line with a $.
- The -T option means that if the file contains tab characters, they will be displayed as
^I
This will display the content of 'filename' with non-printing characters visible, line endings highlighted, and tabs displayed as '^I'.
11. How to Open Dashed Files in Linux Using `cat` Command
To open a file with a dash at the beginning of its name, use the '--' option:
Syntax:
cat -- "-dashfile"Example:
cat -- "-jayesh2"
This will display the content of a file named "-jayesh2"
12. Cat command if the file has a lot of content and can't fit in the terminal.
If the file is too long to fit on one screen, you can use the more command with cat to scroll through it page by page.
Syntax:
cat "filename" | moreOutput:
Will show that much content, which could fit in terminal and will ask to show more.13. Merge Contents of Multiple Files Using `cat` Command
To merge the contents of multiple files into a single file, use the redirection ('>')
Syntax:
cat "filename1" "filename2" "filename3" > "merged_filename"Example:
cat "file1" "file2" "file3" > "merged123"This will concatenate the contents of "file1" "file2" "file3" into "merged123".

14. Display Content of All Text Files in a Folder Using `Cat` Command
To display the content of all text files in a folder, use the wildcard ('*.txt'):
Syntax:
cat *.txt
Will show the content of all text files present in the folder.
15. Cat Command to Append to an Existing File:
To append text to an existing file, use the '>>' operator along with 'cat':
Syntax:
cat >> geeks.txt
The newly added text.
This will append the text "The newly added text." to the end of the 'geeks.txt' file.
Other cat Command Options
Here are some more options which is used in cat command:
| Option | Usage |
|---|---|
-b | Numbers only non-empty lines (unlike -n which numbers all lines). |
-T | Shows TAB characters as ^I, which helps in spotting actual tab spaces. |
-v | Makes non-printing characters visible (except tabs and line breaks). |
-u | Forces unbuffered output, mostly useful when using cat with real-time devices. |