0Pricing
Linux Command Line Mastery · บทเรียน

การดูเนื้อหาไฟล์: `cat`, `less`, `more`

สำรวจคำสั่งต่าง ๆ สำหรับแสดงและเลื่อนดูเนื้อหาไฟล์ในเทอร์มินัล

การดูเนื้อหาไฟล์: `cat`, `less`, `more` เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Welcome to File Viewing

When working in the Linux terminal, you'll often need to peek inside files to see their contents. Whether it's a small configuration file or a massive log, knowing how to view files is crucial.

In this lesson, we'll explore three fundamental commands: cat, less, and more. Each has its strengths for different scenarios.

`cat`: Displaying Whole Files

The cat command, short for 'concatenate', is primarily used to display the entire content of one or more files directly to your terminal's standard output.

It's best for small files, as it will print everything at once, potentially scrolling off your screen if the file is too large.

`cat` in Action

Let's say you have a small file named hello.txt. To view its content, you simply type cat followed by the filename:

cat hello.txt

# Expected Output (if hello.txt contains):
# Hello CoddyKit!
# This is a small file.

`cat` for Creating Files

A less common but useful trick is using cat to quickly create a small file directly from your terminal input. You'll type your content and then press Ctrl+D to save and exit.

cat > new_message.txt
This is a new message.
It was created with cat.
(Press Ctrl+D here)

# Then to view it:
cat new_message.txt

`cat`: Concatenating Files

As its name suggests, cat can also 'concatenate' or join multiple files. You can display them one after another, or redirect the combined output into a new file.

cat file1.txt file2.txt

# To combine into a new file:
cat file1.txt file2.txt > combined.txt

When `cat` Falls Short

While cat is quick and simple, it's not suitable for large files. Imagine a log file with thousands of lines; cat would just blast it all onto your screen, making it impossible to read.

For such cases, we need a 'pager' that displays content one screen at a time.

`less`: The Powerful Pager

The less command is your go-to tool for viewing large files. It displays content page by page, allowing you to scroll, search, and navigate efficiently without loading the entire file into memory.

less large_log.log

# This will open large_log.log in a special viewer.

Navigating in `less`

Once you open a file with less, you can use several keys to navigate:

  • Spacebar: Scroll forward one page.
  • b: Scroll backward one page.
  • g: Go to the beginning of the file.
  • G: Go to the end of the file.
  • /text: Search forward for 'text'.
  • ?text: Search backward for 'text'.
  • q: Quit less.

`less` with Piped Output

less is incredibly versatile and is often used with pipes (|) to view the output of other commands. This is perfect when a command generates a lot of output.

ls -l /etc | less

# This lists the contents of /etc, then pipes them to less for easy viewing.

`more`: The Older Pager

The more command is an older file pager, similar to less. It also displays content one screen at a time, but it has fewer features.

The main difference is that more typically only allows you to scroll forward, not backward. You press Spacebar to advance and q to quit.

more old_document.txt

# This will open old_document.txt, allowing forward-only paging.

Check Your Understanding

Let's test your knowledge of cat, less, and more.

Recap: Viewing File Content

Great job! You've learned the essentials of viewing file content in the Linux terminal:

  • cat: Best for quickly displaying small files or concatenating multiple files.
  • less: The go-to pager for large files, offering powerful navigation, searching, and backward scrolling.
  • more: An older pager, useful for viewing files page by page but typically limited to forward scrolling.

Choose the right tool for the right job to efficiently manage your files!

คำถามที่พบบ่อย

บทเรียน “การดูเนื้อหาไฟล์: `cat`, `less`, `more`” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การดูเนื้อหาไฟล์: `cat`, `less`, `more`” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การดูเนื้อหาไฟล์: `cat`, `less`, `more`”

สำรวจคำสั่งต่าง ๆ สำหรับแสดงและเลื่อนดูเนื้อหาไฟล์ในเทอร์มินัล คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การดูเนื้อหาไฟล์: `cat`, `less`, `more`” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม

ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การดูเนื้อหาไฟล์: `cat`, `less`, `more`
  2. การค้นหาไฟล์: `find` และ `locate`
  3. สิทธิ์และเจ้าของ: `chmod`, `chown`
  4. การสร้างและใช้งานลิงก์สัญลักษณ์
← กลับไปที่ Linux Command Line Mastery