Open In App
Related Articles

Cat command in Linux with examples

Improve Article
Improve
Save Article
Save
Like Article
Like

Cat(concatenate) command is very frequently used in Linux. It reads data from the file and gives its content as output. It helps us to create, view, and concatenate files. So let us see some frequently used cat commands. 

1) To view a single file 

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) To view multiple files 

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) To view contents of a file preceding with line numbers. 

Syntax:

cat -n file_name

Example: If our file_name is file2.

cat -n file2
cat -n file2

cat -n file2

4) Create a file and add content 

Syntax:

cat > newfile_name

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

cat > jayesh1
ls

 `ls` command is used to display all files and directories in the current location.

5) Copy the contents of one file to another file. 

Syntax:

$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

Output  

The content will be copied in destination file

6) Cat command can suppress repeated empty lines in output 

Syntax:

cat -s file_name

Output  

Will suppress repeated empty lines in output

7) Cat command can append the contents of one file to the end of another file. 

Syntax: 

cat file_name1 >> file_name2

Example:

cat file1 >> file2

8) Cat command can display content in reverse order using tac command. 

Syntax:

tac file_name

Example:

tac file2
tac file2

tac file2

9) Cat command can highlight the end of line. 

Syntax: 

cat -E "filename"

Output: 

Will highlight the end of line

10) If you want to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option. 

Syntax:

cat -A  "filename"

11) Cat command to open dashed files. 

Syntax:

cat -- "-dashfile"

Output: 

Will display the content of -dashfile

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) Cat command to merge the contents of multiple files. 

Syntax: 

cat "filename1" "filename2" "filename3" > "merged_filename"

Output: 

Will merge the contents of file in respective order and will insert that content in "merged_filename".

14) Cat command to display the content of all text files in the folder. 

Syntax:

cat *.txt

Output:  

Will show the content of all text files present in the folder.

15) Cat command to write in an already existing file. 

Syntax:

cat >> geeks.txt
The newly added text.

Output:

Will append the text "The newly added text." to the end of the 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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 25 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads