Linux Tutorial / Read Files with cat, less, more, head, and tail

Use cat to print a short text file, less or more to page through a long one, and head or tail when you need only the beginning or end. Reading a file in the terminal is not the same as editing it. This lesson applies all five commands to one practice file so you can choose the right tool for each task.

Prepare a safe practice file

Run these commands in Bash as an ordinary user. If ~/linux-course-10-practice already exists, choose another name rather than touching existing data. Stop if mkdir fails; continuing could overwrite a file already there.

mkdir ~/linux-course-10-practice
cd ~/linux-course-10-practice
pwd

Only after pwd confirms the new practice directory, create a five-line file. In printf, \n represents a newline. The > operator saves standard output to a file and overwrites an existing file, which is why this must be a new directory.

printf 'alpha\nbeta\ngamma\ndelta\nepsilon\n' > sample.txt

Print a short file with cat

cat sends a file's contents to standard output, making it useful for a small text file. Running it on the practice file prints the five lines in order.

cat sample.txt

Expected output:

alpha
beta
gamma
delta
epsilon

cat does not divide the output into pages or edit the file. A very long file may scroll past too quickly, so use less instead. Avoid sending an unknown binary file directly to your terminal.

Browse long files with less and more

less is a pager that lets you move through and search a file. After running less sample.txt, press Space for the next screen, b for the previous screen, type /gamma and press Enter to search, and press n for the next match. Press q to quit.

less sample.txt

The practice file may fit on one screen, so page movement is easier to appreciate with a longer log or configuration file. Keys pressed inside less are not additional shell commands to append to the code block.

more also displays text a screen at a time. Space generally advances and q quits. Navigation features vary by implementation and environment; less is usually more convenient when you need to move backward and search.

more sample.txt

Read only the start or end with head and tail

Use head for the first part of a file and tail for the last part. With GNU utilities, -n 2 requests two lines.

head -n 2 sample.txt
tail -n 2 sample.txt

The first command prints alpha and beta; the second prints delta and epsilon.

alpha
beta
delta
epsilon

tail -f file prints the end and keeps waiting for newly appended lines. It is useful for a live log; press Ctrl+C to stop following it. When logs are replaced or rotated, GNU tail -F can be appropriate because it retries the file name. You still need permission to read the file.

Choose the command that fits the task

Task Command Why
Print a short file cat Immediate full output
Browse and search a long file less Paging and search
Basic page-by-page reading more Simple paging
Inspect the first few lines head -n Only the beginning
Inspect the end or new additions tail -n, tail -f Last lines or appended lines

If the name does not tell you whether a file is text, use file as described in the earlier file-types lesson. Control characters in a file can make terminal output surprising. The reading commands here do not change the original file, but the > used to create the practice file can overwrite one.

Official documentation

See the GNU Coreutils manual for cat, head, and tail, and the Bash redirection documentation for >. For keys supported by your installed pager, consult man less and man more.

More in This Category
Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Linux blkid Command: Find Block-Device UUIDs and Filesystem Types

Learn how to find Block-Device UUIDs and Filesystem Types with the Linux blkid command, including practical examples, key options, and important precautions.

Linux xz Command: Compress a File in xz Format

Linux xz Command: Compress a File in xz Format

Learn how to compress a File in xz Format with the Linux xz command, including practical examples, key options, and important precautions.

Linux type Command: Identify How a Command Is Resolved

Linux type Command: Identify How a Command Is Resolved

Learn how to use the Linux type command to identify aliases, functions, built-ins, keywords, and executable paths, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux pkill Command: Signal Processes by Name

Linux pkill Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux pkill command, including practical examples, key options, and important precautions.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux sed Command: Search and Replace Text Streams

Linux sed Command: Search and Replace Text Streams

Learn how to search and Replace Text Streams with the Linux sed command, including practical examples, key options, and important precautions.

Linux apt Command: Manage Packages on Debian and Ubuntu

Linux apt Command: Manage Packages on Debian and Ubuntu

Learn how to manage Packages on Debian and Ubuntu with the Linux apt command, including practical examples, key options, and important precautions.

Linux fold Command: Wrap Long Lines at a Fixed Width

Linux fold Command: Wrap Long Lines at a Fixed Width

Learn how to wrap Long Lines at a Fixed Width with the Linux fold command, including practical examples, key options, and important precautions.

Linux cp Command: Copy Files and Directories

Linux cp Command: Copy Files and Directories

Learn how to copy files and directories with Linux cp, control overwrites, preserve attributes, handle symlinks, and verify important copies.