Fundamentals 12 min read

Master Linux File Viewing: head, tail, more, less, and cat Explained

This guide walks through essential Linux commands for viewing file contents—head, tail, more, less, and cat—detailing their syntax, common options, and practical examples for extracting specific lines, paging through output, and combining with other tools.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux File Viewing: head, tail, more, less, and cat Explained

1. head command

The head command displays the first ten lines of a file by default. Syntax: head [options] [file(s)]. Example to show the first ten lines of /etc/passwd: # head /etc/passwd When multiple files are provided, head shows the first ten lines of each file. Use -n with a number to specify a different line count, e.g., # head -n5 /var/log/yum.log shows the first five lines.

2. tail command

The tail command displays the last ten lines of a file by default. Syntax: tail [options] [filenames]. Example to show the last ten lines of /etc/passwd: # tail /etc/passwd Like head, it works with multiple files. Use -n to change the number of lines, e.g., # tail -n20 /var/log/yum.log. The -f option follows a file in real time, useful for logs: # tail -f /var/log/messages.

3. more command

The more command pages through a file one screen at a time. Syntax: more [options] [filenames]. Example: # more /var/log/messages Press Enter to scroll line by line, Space for the next page, and q to quit. Useful options include -num to set lines per page, +num to start at a specific line, and /pattern to search for a pattern.

4. less command

The less command also pages through files but allows both forward and backward navigation and searching. Syntax: less [options] [filenames]. Example: # less /var/log/messages Key controls: Space (down a page), b (up a page), j / k (line down/up), /pattern (search), n (next match), and q (quit). Options such as -num, +num, and /pattern work similarly to more.

5. Using less with pipelines

less

can view the output of other commands via a pipe, e.g., cat urls.txt | less, enabling paging through generated content.

6. cat command

The cat command concatenates and displays file contents. Syntax: cat [options] [filenames] [-] [filenames]. Basic usage to view a file: # cat /etc/passwd It can also combine multiple files: # cat 1 2 3 4 > 5, then display the combined file with # cat 5. Redirecting output creates new files, e.g., # cat > devops.txt. A here‑document can define custom end markers:

# cat > test.txt << end
I am Avishek
Here i am writing this post
Hope your are enjoying
end

Reading the created file: # cat test.txt Finally, cat can copy files: # cat name.txt > name1.txt and then view the copy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxCATlesstailFile Viewingheadmore
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.