What is the usage of the ‘cat’ command in Linux?

The “cat” command in Linux is used to concatenate files and print them to the standard output device, or to merge multiple files into one single file.

The typical usage of the “cat” command is as follows:

  1. Print the content of the file on the screen.
  2. Display the contents of the file named “filename”.
  3. For instance, using “cat file.txt” will display the contents of the file.txt on the screen.
  4. Merge multiple files:
  5. Merge the content of file1.txt and file2.txt into mergedfile.txt.
  6. This command merges the contents of file1.txt and file2.txt into one file named mergedfile.txt.
  7. Make a file:
  8. Redirect the output of the command “cat” to a file named “filename”.
  9. After inputting content, press Ctrl+D to finish input, and save the input content to a file named filename.
  10. Append additional content to the file.
  11. Append the content of the file to `filename`.
  12. After entering the content, press Ctrl+D to finish input and append the input content to the end of the filename file.
  13. Display line numbers:
  14. Display the contents of a file with line numbers.
  15. When using the option “-n”, line numbers will be added before each line when displaying the contents of a file.
  16. Display content of specific number of lines.
  17. Display the first N lines of a file with line numbers.
  18. By adding the option “-n” and piping it with the head command using the pipe symbol “|”, you can display only the first N lines of a file.

Here are some common usages of the cat command, for more usages you can refer to the help document of cat by entering the command “man cat”.

Leave a Reply 0

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