WebMasterCampus
WEB DEVELOPER Resources

Linux cat Command

Learn Linux cat Command with examples


Linux cat Command

In Linux cat (short for “concatenate”) command is one of the most frequently used command.

cat command commonly used to display the contents of a single or multiple text files.

It can also combine files by appending one file’s contents to the end of another file, and create new files.

cat Command Syntax

~$  cat [OPTIONS] [FILE_NAMES]
  • OPTIONS - cat options . Use cat –help to view all available options.
  • FILE_NAMES - Zero or more file names

To display the contents of the /etc/pam.conf file on the terminal:

~$  cat /etc/pam.conf

Copy File Content to a New File

~$  cat file.txt > file_backup.txt

Copy Multiple Files Content to a New File

~$  cat file_1.txt file_2.txt > files_backup.txt

Append Multiple Files Content to a Another File

To append the contents of file_1.txt and file_2.txt and append the result to file_copy.txt to use the (») operator.

~$  cat file_1.txt file_2.txt >> file_copy.txt

Copy Contents of File Step by Step Guide

~$  touch file.txt

~$  ls -l

~$  echo "File content" > file.txt

~$  cat file.txt > file_copy.txt

~$  ls -l

# file_copy.txt will have the same content as fil.txt

cat -n Print Line Numbers

cat -n option displays line number to each line content of file.


~$ cat -n fileName

# fileName can be any file name

Suppress Repeated Empty Lines

cat -s option omits the repeated empty output lines.

~$  cat -s file.txt

cat -T Displays TAB characters

cat -T option allows you to visually distinguish between tabs and spaces.

~$  cat -T /etc/hosts

cat -e Displays End of Lines

cat -e displays the invisible line ending character. The Lines ending will be displayed as $.

~$  cat -e /etc/hosts

cat Concatenating Files

Passing two or more file names as arguments to the cat command, the contents of the files will be concatenated.

cat command follows the input files sequence given in its arguments for the output order.

For example, the following command will read the contents of file_1.txt and file_2.txt and display the result in the terminal:

~$  cat file_1.txt file_2.txt

Cat Command Creating File

~$  cat > myFile.txt

# Press Enter, type the text and once you are done, press the CRTL+D to save the file.

Cat Command Append Content to a File

If a file named file1.txt is present, it will be overwritten. Use the “»” operator to append the output to an existing file.

~$  cat >> myFile.txt

# Press Enter, type the text and once you are done, press the CRTL+D to save the file.

Cat Command with More & Less Options

Displaying a large content file scrolls up very fast, we can use more and less parameters with the cat command to display content.

~$  cat song.txt | more
~$  cat song.txt | less

Sorting Contents of Multiple Files in a Single File

cat | sort is use to sort content of all files. You can display in terminal or store sorted content in new created file.

~$  cat test test1 test2 test3 | sort > test4
Created with love and passion.