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:
- Print the content of the file on the screen.
- Display the contents of the file named “filename”.
- For instance, using “cat file.txt” will display the contents of the file.txt on the screen.
- Merge multiple files:
- Merge the content of file1.txt and file2.txt into mergedfile.txt.
- This command merges the contents of file1.txt and file2.txt into one file named mergedfile.txt.
- Make a file:
- Redirect the output of the command “cat” to a file named “filename”.
- After inputting content, press Ctrl+D to finish input, and save the input content to a file named filename.
- Append additional content to the file.
- Append the content of the file to `filename`.
- After entering the content, press Ctrl+D to finish input and append the input content to the end of the filename file.
- Display line numbers:
- Display the contents of a file with line numbers.
- When using the option “-n”, line numbers will be added before each line when displaying the contents of a file.
- Display content of specific number of lines.
- Display the first N lines of a file with line numbers.
- 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”.