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.

This lesson reads file content. Selecting and counting lines is covered separately in text processing with grep, cut, sort, uniq, and wc.

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.

Five lines fit on one screen. Create a separate 80-line file in the same new lab directory to observe paging. In less, use Space for the next screen, b for the previous screen, / to search, and q to quit. Page length depends on terminal height.

seq 1 80 > long-sample.txt
less long-sample.txt

The original sample.txt is unchanged. After comparison, remove only the additional file with rm -i long-sample.txt.

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.