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 time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux more Command: View Text One Screen at a Time

Linux more Command: View Text One Screen at a Time

Learn how to use Linux more to read text one screen at a time, move and search interactively, start at a line or pattern, and choose between more and less.

Linux fmt Command: Reformat Plain-Text Paragraphs

Linux fmt Command: Reformat Plain-Text Paragraphs

Learn how to reformat Plain-Text Paragraphs with the Linux fmt command, including practical examples, key options, and important precautions.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.

Linux mount Command: Inspect and Mount Filesystems

Linux mount Command: Inspect and Mount Filesystems

Learn how to inspect and Mount Filesystems with the Linux mount command, including practical examples, key options, and important precautions.

Linux dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux Tutorial / Inspect, Pause, and Stop Processes

Linux Tutorial / Inspect, Pause, and Stop Processes

Inspect Linux processes with ps, top, and pgrep, then safely pause, resume, and terminate a practice process while distinguishing shell jobs from services.

Linux scp Command: Copy Files over SSH

Linux scp Command: Copy Files over SSH

Learn how to copy Files over SSH with the Linux scp command, including practical examples, key options, and important precautions.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

Trace the history of Linux from Unix and the GNU Project through the first Linux kernel, distributions, enterprise adoption, cloud computing, containers, and modern infrastructure.