Open In App

How to View the Content of File in Linux | cat Command

Last Updated : 03 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The cat command in Linux is more than just a simple tool; it’s a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let’s delve into the details of some frequently used cat commands, understanding each example along the way.

Basic 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.

Practical Examples of Cat Command in Linux

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_name

Example: If our file_name = jayesh.txt

cat jayesh.txt
cat 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

Syntax:

cat file_name1 file_name2

Example: If we have two files , file1 and file2.

cat file1 file2
cat file1 file2

cat file1 file2

3. How to View Contents of a File preceding 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_name

Example: If our file_name is file2.

cat -n file2
cat -n 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.

4. 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_name

Example: If we want to create a newfile_name = jayesh1.

cat > jayesh1
ls

This 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.

creating file using cat command in linux

creating file using cat command in linux

5. 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.

6. 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_name

Output  

Will suppress repeated empty lines in output

7. How to Append 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_name2

Example:

cat file1 >> file2

This will append the content of `file1` to the end of `file2`

8. 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_name

Example:

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
tac file2

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: 

Displaying $ in the end of line

Displaying $ in the end of line

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 shows tabs 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"
displaying content inside a file starting with `-`

displaying content inside a file starting with `-`

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. 

Syntax: 

cat "filename" | more

Output:

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”.

merging content of multiple files into single file

merging content of multiple files into single file

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
Displaying all file with extension ".txt"

Displaying all file with extension “.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.

Conclusion

In this article we have discussed the `cat` command in Linux which is a versatile tool used for various file-related operations. We also discussed that it allows users to view, concatenate, create, copy, merge, and manipulate file contents. It is commonly used to display the content of a single file, multiple files, or add content to an existing file. Overall, we can say that `cat` command is an essential utility for managing and manipulating files in Linux.

View the Content of File in Linux | cat Command – FAQs

How to view the content of a file in Linux?

To view the content of a file in Linux, use the cat command:

cat filename.txt

Other commands include less, more, and head for different viewing options:

less filename.txt
more filename.txt
head filename.txt

How to view file details in Linux?

To view detailed information about a file in Linux, use the ls -l command:

ls -l filename.txt

For more detailed information, use stat:

stat filename.txt

How to view a file in Unix?

In Unix, you can use the cat, less, more, and head commands to view a file:

cat filename.txt
less filename.txt
more filename.txt
head filename.txt

How to check file content type in Linux?

To check the content type of a file in Linux, use the file command:

file filename.txt

This command displays the file type and other related information.

How to display file type in Linux?

To display the file type in Linux, use the file command:

file filename.txt

This command identifies the file type based on its content, not just its extension.



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: