The Wayback Machine - https://web.archive.org/web/20240825191336/https://www.geeksforgeeks.org/cat-command-in-linux-with-examples/
Open In App

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

Last Updated : 12 Jul, 2024
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"

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.

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



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to View Kernel Messages in Linux | dmesg Command
The Linux kernel is considered as the one of the core components for the Linux operating system. It helps in establishing the communication between the hardware and managed system resources. To monitor and troubleshoot the kernel's interactions with the hardware and software components, the dmesg command comes as well-handy. In this article, we wil
8 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
file command in Linux with examples
file command is used to determine the type of a file. .file type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’). This command tests each argument in an attempt to categorize it. It has three sets of tests as follows: filesystem test: This test is based on the result which returns from a stat system cal
3 min read
How to Save Output of Command in a File in Linux?
When we run a command on a terminal on Linux it generates some output of that command. Sometimes we need the output result of the commands. Today we are going to see how to save the output of the command. Using Redirections in Linux: We can use the redirections operators to save the output of commands into files. Redirection operators redirect the
3 min read
How to Create a Text File Using the Command Line in Linux
There are a couple of quick ways to create a text file from the Linux Command Line or Terminal. Some of them have been explained in the following article. 1) touch commandThis is the most standard command to quickly create an empty text file. The command is quite simple to type and makes it quite easier to create several text files at once. The com
6 min read
Extract Emails From a Text File Using Grep Command in Linux
Let's consider we have a text file that contains lots of text and in that text file there are some email IDs present, and we have to find the all email IDs present in that text file. So what we can do? How can we find all email IDs present in that text file?. One way to find all email IDs manually, but this is a very time-consuming and boring proce
3 min read
How to compress file in Linux | Compress Command
Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive guide, we'll explore the ins and outs of the compr
5 min read
How to Mount File System in Linux | mount Command
All files in a Linux filesystem are arranged in form of a big tree rooted at '/'.These files can be spread out on various devices based on your partition table, initially your parent directory is mounted(i.e attached) to this tree at '/', others can be mounted manually using GUI interface(if available) or using mount command. mount command is used
6 min read
Use the Lines of a File as Arguments of a Linux Command
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let's say you have a text file with a list of 100 client
6 min read
How to Move File in Linux | mv Command
The `mv` command in Linux is like a superhero tool that can do a bunch of cool stuff with your files and folders. Think of it as a digital moving truck that helps you shift things around in your computer. Whether you want to tidy up your folders, give your files new names, or send them to different places, `mv` is the go-to friend for the job. It's
7 min read
Unmount File System in Linux Using the umount Command
Unmounting a file system is a vital procedure for securely deleting storage devices or rendering system resources accessible. A vital instrument in this procedure is the "umount" command, which enables users to softly unmount mounted devices or partitions. Before beginning the unmounting process, this step ensures data integrity and protects any po
7 min read
How to Display Path of an Executable File in Linux | Which Command
In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In this article, we are going to discuss on what is whi
6 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This article explores the prowess of the find command, sheddi
9 min read
How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The `chown` command, short for "change owner," is a powerful tool that allows users to change the owner of files and directories. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific r
9 min read
How to Create an Empty File in Linux | Touch Command
The touch command is a standard command used in the UNIX/Linux operating system which is used to create, change and modify the timestamps of a file. Basically, there are two different commands to create a file in the Linux system which are as follows: touch command: It is used to create a file without any content. The file created using the touch c
8 min read
How to Rename File in Linux | rename Command
Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting out or you're already good at working with files, t
8 min read
Check If File Exist Inside Tar File Using Tar And Grep Command
Linux users are familiar with the popular command-line tool Tar (tape archive), which is used to archive directories and files into a single file called a "tarball." Occasionally, you might need to verify whether a certain file is present in a tar archive without extracting the full archive. This detailed article will show you how to effectively se
4 min read
How to copy a file's content from Linux terminal?
This article shows the alternative method to copy the content of the file onto the clipboard, via the Linux terminal. In OSX, the commands pbcopy and pbpaste are available by default. Thus, to copy a file onto the clipboard via OSX terminal, type: pbcopy < 'path of the file' Since, in Ubuntu, pbcopy and pbpaste commands are not availab
2 min read
Different Ways to Empty or Delete a Large File Content in Linux
In this article, we will learn different methods to empty or delete large file content in the Linux/Unix system by overwriting the existing target file with a newly created empty file. Before we proceed with the given various ways, please make sure that the file we're emptying or deleting content is not important or system file because once the con
3 min read
Difference between vi Editor and cat Command
Both vi editor and cat command are used to input contents in a file. 1. vi Editor : vi editor stands for the visual editor which is an intelligent editing tool in UNIX Operating System used for reading, creating files, editing them, and more. It is the default editing tool in UNIX. Command to open a particular file : vi filename It has three modes
3 min read
Ccat - Colorize Cat Command Output
In this article, we will learn to use the Ccat tool to make our terminal output look good and vibrant. Ccat is a command line tool that works like the cat command in linux, where the cat is a standard command in linux used for various operations like printing the file content in standard output and concatenating files. But Ccat does something coole
5 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
3 min read
How to View Colored Man Pages in Linux?
Man pages or simply manual pages are the documentation that is available with every command-line tool or application present to us in a Linux System. These types of documentation include topics from command-line tools, system calls, programs, and even abstract concepts. To read a man page simply type man <package> Man pages are usually monoto
3 min read
How to View Configuration Files Without Comments in Linux?
We came across an extremely long configuration file with hundreds of thousands of comments, and all we wanted to do was filter out the important content or settings. As a result, we'll look at various methods for viewing configuration files in Linux without comments. The grep command can be used to do this. The following command will delete lines b
2 min read
How To View and Create Hidden Files in Linux
In the Linux framework, there are a ton of hidden records that incorporate for the most part the client setup document and the application documents. You can likewise hide your Files and Documents in your framework on the off chance that you are imparting them to another individual or because of any explanation. In any case, recall that hiding the
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
three90RightbarBannerImg