Basic Cat Command Examples In Linux

15 Basic Cat Command Examples In Linux

The cat is short for the “concatenatecommand. It is mostly used in file management tasks like creating single or multiple files, viewing the content of a file, concatenating files, etc.

15 Basic Cat Command Examples In Linux

1. Cat Command To Display The Content Of  A File:

Run the following cat command that displays the contents of a file. In the following command, you can see the content of the file named filename.txt

Cat filename.txt

2. Cat Command To Display The Content Of  Two Files:

Run the following cat command that displays the contents of two files.

cat file1.txt file2.txt

3. Cat Command To Create A New File:

This command creates a new file named “file_name.txt” and allows you to enter text into it.

cat > file_name.txt

Press Ctrl+D to save and exit.

4. Cat Command To Appends The Contents Of One File To Another File:

The following cat command appends the contents of “file_name.txt” to the end of “file2_name.txt”.

cat file_name.txt >> file2_name.txt

5. Cat Command To Display The Contents Of One File That Matches The Search Term:

Run the following cat command to display the contents of “filename.txt” that match the search term “search_term”.

cat filename.txt | grep "search_term"

6. Cat Command To Display The Contents Of File With Line Numbers:

This cat command displays the contents of “filename.txt” with line numbers.

cat -n filename.txt

7. Cat Command To Display The Contents Of the File for nonblank lines.

The following cat command displays the contents of “filename.txt” with line numbers, but only for non-blank lines.

cat -b filename.txt

8. Cat Command To Display The Contents Of File for with multiple blank lines compressed into a single blank line

Run the following cat command in your terminal to display the contents of “filename.txt” with multiple blank lines compressed into a single blank line.

cat -s filename.txt

9. Cat Command To Display The Contents Of File with tabs represented as ^I.

Open your terminal and run the following command to display the contents of “filename.txt” with tabs represented as ^I.

cat -T filename.txt

10. Cat Command To Enter Commands via the terminal to the file

Run the following cat command to enter commands into the terminal that will be processed as if they were entered from a file named “file.txt”.

cat < filename.txt

11. Cat Command To displays the contents of the file with a $ symbol at the end of each line.

Run the following cat command that displays the contents of “filename.txt” with a $ symbol at the end of each line.

cat -E filename.txt

12. Cat Command To displays the contents of all files in the current directory that start with “file” and end with “.txt”.

Run the following command to display the contents of all files in the current directory that start with “filename” and end with “.txt”.

cat filename*.txt

13. Cat Command To displays the contents of file with non-printable characters represented as ^X.

This following cat command displays the contents of “filename.txt” with non-printable characters represented as ^X.

cat -v filename.txt

14. Cat Command To empties the contents of file.

Run the following cat  command to empty the contents of the file.

cat /dev/null > filename.txt:

15. Cat Command To display the version of the cat command.

The following cat command displays the version of the cat command.

cat --version
READ More Relevant Stuff:  Top Linux Interview Questions In 2021 [Updated]

Leave a Reply

Your email address will not be published. Required fields are marked *