Command Line · Lesson 7 of 19
cat
0 / 5 checkpoints answered
After learning to identify files, the next step is to read their contents. The cat command displays files and joins their contents; its name is short for "concatenate."
Viewing File Contents
The simplest use of cat displays a file directly in the terminal:
$ cat myfile.txt
The command writes the entire file to standard output. This works well for short text, but a long file may scroll past too quickly.
CheckWhich command displays all of myfile.txt in the terminal?
press 1/2/3 to answer · → next lesson
Concatenating Files
When you give cat several files, it reads them in operand order and writes their contents one after another:
$ cat dogfile birdfile
This displays dogfile first and birdfile second. To save the combined output in a new file, redirect standard output with >:
$ cat dogfile birdfile > animals
The shell creates animals or truncates it before running cat, then sends the combined output there. Do not use one of the input files as this destination, because it may be emptied before cat reads it.
CheckWhich command writes part1 followed by part2 into a new or replaced file named whole?
press 1/2/3 to answer · → next lesson
Reading Terminal Input into a File
When no input file is given, cat reads standard input. You can combine that behavior with > to enter text from the terminal and write it to a file:
$ cat > newfile.txt
After running the command, type the desired text. Press Ctrl+D to send an end-of-file signal and return to the shell. Be careful: if newfile.txt already exists, > truncates its previous contents.
Use >> to append new input instead of replacing existing contents:
$ cat >> notes.txt
CheckYou want to type more text at the end of an existing notes.txt. Which command starts that operation without truncating the file?
press 1/2/3 to answer · → next lesson
Formatting the Output
Several options make output easier to inspect:
-n: Number all output lines, starting from 1.-b: Number only non-empty output lines.-s: Squeeze multiple blank lines into one blank line.-A: Show nonprinting characters, tabs, and line endings.
Examples:
$ cat -n script.sh
$ cat -b notes.txt
$ cat -s messy.txt
CheckWhich command numbers only the nonempty output lines from notes.txt?
press 1/2/3 to answer · → next lesson
Choosing a Viewer for Long Files
Use cat when you want the entire output at once. For a long file, less is usually more convenient because it lets you scroll, search, and quit without flooding the terminal:
$ less /var/log/syslog
CheckWhich command is better suited to interactively reading a long log file?
press 1/2/3 to answer · → next lesson
To practice displaying and combining file content, try these hands-on labs:
- Linux cat Command: File Concatenating - Learn the
catcommand for viewing, concatenating, and manipulating text files, enhancing your command-line skills for efficient text file handling. - Viewing Log and Configuration Files in Linux - Practice using commands like
catto efficiently view and navigate text files, including system logs and configuration files, to extract critical information.
Summary
You can now use cat to display and combine file content while choosing safe redirection.
- Display the complete contents of a short file.
- Concatenate files in a chosen order.
- Replace or append to a destination deliberately.
- Number or simplify output lines.
- Choose
lesswhen interactive reading is more suitable.
Lesson complete
You finished cat. Nice work — keep the momentum going.