The cat
command stands for “concatenate.” It is commonly used to display the contents of files, combine multiple files into one, or create new files.
Initial Example
Using cat
to display the contents of a file called example.txt
:
cat example.txt
CAT Parameters
Parameter | Description |
---|---|
-A, --show-all | Equivalent to -vET , show all non-printing characters, ends of lines, and tabs as special characters |
-b, --number-nonblank | Number only non-empty output lines, overriding -n |
-e | Equivalent to -vE , show non-printing characters and display $ at the end of each line |
-E, --show-ends | Display $ at the end of each line to indicate line endings |
-n, --number | Number all output lines, including empty ones |
-s, --squeeze-blank | Suppress repeated empty output lines and display just one |
-t | Equivalent to -vT , show non-printing characters and tabs as ^I |
-T, --show-tabs | Display TAB characters as ^I |
-u | Ignored, retained for compatibility |
-v, --show-nonprinting | Display non-printing characters using ^ and M- notation, except for LFD and TAB |
--help | Display help information and exit |
--version | Show version information and exit |
Examples
1. Display the Contents of a File
To view the contents of a file:
cat filename.txt
2. Display the Contents of Multiple Files
To view the contents of multiple files sequentially:
cat file1.txt file2.txt
3. Combine Multiple Files into One
To concatenate the contents of multiple files and save them into a new file:
cat file1.txt file2.txt > combined.txt
4. Append the Contents of a File to Another File
To append the contents of one file to the end of another file:
cat file1.txt >> file2.txt
5. Create a New File with Text Input
To create a new file and add text to it (press Ctrl+D
to save and exit):
cat > newfile.txt This is the content of the new file. Ctrl+D
6. Display Line Numbers
To display the contents of a file with line numbers:
cat -n filename.txt
7. Display Non-Printing Characters
To display the contents of a file while showing non-printing characters:
cat -v filename.txt