←
Command Line
/
Guided Lab Read and Join Files with cat
60:00
Click the virtual machine below to start practicing
>_ Tekhnikum | Lab
- Setting up virtual machine…
- Preparing lab files…
- Starting terminal services…
This lab machine has stopped
Your progress through the guide is saved. Start a fresh machine to keep practicing.
Introduction
cat (concatenate) prints files to the terminal, one after another. ~/project has a few small files to work with.
This lab is the hands-on companion to the cat lesson from Linux Journey (CC BY-SA 4.0).
Print a File
cat dogfile
Dogs bark.
Join Files into a New One
Give cat several files and it prints them in order. > sends that output into a file instead of the screen:
cat dogfile birdfile > animals
cat animals
> replaces the file's contents; >> would add to the end.
Write from the Keyboard
With no file to read, cat reads what you type. Redirect it into a file:
cat > shopping.txt
Type a few lines (for example eggs and bread), pressing Enter after each. Then press Ctrl+D on an empty line to finish.
cat shopping.txt
Number and Squeeze
-n numbers every line. -s squeezes runs of blank lines into one:
cat -n script.sh
cat -s messy.txt
Summary
cat file prints a file; cat a b prints both in order.
> writes output to a file (replacing it) and >> appends.
cat > file saves what you type until Ctrl+D.
-n numbers lines and -s squeezes blank lines.