Linux cat Command With Examples

Spread the love

cat (short for concatenate) command is one of the most frequently used commands in Linux. It is used to view the content of a file, create single or multiple files, copy content from one file to another and concatenate files.

Most Common Use of cat Command

  • Create a new file
  • Read file
  • Display file
  • Modify file
  • Concatenate file

Syntax used for cat command:- Cat Option Filename

1. Display Content of Single File

Syntax: cat filename

It will show the content of a given file.

cat file1.txt


linux cat command

2. Display Content of Single File Along With Line number

Syntax: cat -n filename

It will show the content of the file preceding with line number.

cat -n file1.txt

linux cat command cat -n

3. Create a New File

Syntax: cat > new_filename

It will create a new file of the given filename. While you use cat > new_filenameon terminal, it will wait for input from the user. After typing desired text, press CTRL+D for exit.

cat > file3.txt

linux cat command

4. Display Content of Multiple File

Syntax: cat file1_name file2_name

It will show the content of a given file.

cat file1.txt file2.txt

linux cat command

5. Display Content of Multiple File Along With Line number

Syntax: cat -n file1_name file2_name file3_name

It will show the content of the file preceding with line number.

cat -n file1.txt file2.txt file3.txt

linux cat command

6. Copy the Contents of One File to Another File

Syntax: cat file1_name file2_name

file1_name: It should be the source filename whose content will be copied.

file2_name: It should be the destination filename where content will be copied.

Note:- If file2_name is not a new file then the content of file2_name will be overwritten by the contents of the  file1_name.

Example:

cat file1.txt > file4.txt

cat command in Linux / ubuntu

7. Append the Content of One File to End of Another File

syntax: cat file1_name >> file2_name

Using this command content of file1 will be appended at the end of file2.

Example:

cat file1.txt >> file2.txt

cat command in Linux / ubuntu

8. Show $ at the End of Line

Syntax: cat -E filename

Use “-E” option to show $ at the end of each lines and spaces between lines.

Example:

cat -E file1.txt

cat command in Linux / ubuntu

9. Show file’s content in reverse order

Syntax: tac filename

To display the file’s content in reverse order by lines use tac command. (using tac command First line will show at last line and vice-versa, Top 2nd line will show at bottom 2nd line and vice-versa, …………)

Example:

tac file1.txt

cat taccommand in Linux / ubuntu

 

Leave a Comment

Your email address will not be published.

Disclosure: This website is supports by its reader. This means, if you buy any product / service through link on this website, we may earn a small commision withought costing you extra. Learn how you can support Us and why we need this?