Lesson 5 / الدرس 5

Looking inside files / النظر داخل الملفات

Four commands for reading a file without opening an editor — and the reason there are four is that a log with two million lines needs different handling from a note with six.

أربعة أوامر لقراءة ملف دون فتح محرر — وسبب كونها أربعة أن سجلًا بمليوني سطر يحتاج معاملة غير التي تحتاجها مذكرة بستة أسطر.

Opening an editor to glance at a file is slow, and on a server there may not be a graphical one to open. Reading a file is common enough that it has its own small family of commands, and choosing between them is entirely about how big the file is and which part you want.

CommandDoesReach for it when
cat filePrints the whole file at onceThe file is short
less fileOpens it scrollable, quits with qThe file is long
head fileThe first 10 linesYou want to see the shape of it
tail fileThe last 10 linesThe newest entries in a log
tail -f fileThe last lines, and keeps watchingA log, while something is happening
wc -l fileCounts the linesYou only want to know how big

head and tail both take -n: tail -n 50 gives the last fifty. The one worth remembering by name is tail -f, in the last row but one.

tail -f, and why it is the one to remember

Ordinary tail prints the end of the file and stops. tail -f prints the end and then stays, printing each new line as it is written. Point it at a server log, then load the page in a browser, and you watch your own request appear as it happens. It is the fastest way to find out what a server actually thinks is wrong. Press Ctrl + C to stop watching.

$ tail -f /var/log/apache2/error.log
[Sun Aug 03 14:02:11] PHP Warning:  Undefined variable $name in /home/site/index.php on line 12
[Sun Aug 03 14:02:11] PHP Fatal error:  Uncaught Error: Call to undefined function
                                        ← the cursor sits here, waiting for the next line
What a live log looks like. Run the demonstration for a readable version — this is exactly how a blank white page on a server gets diagnosed, and it is worth being comfortable with before you need it.

less, and getting back out of it

  • q quits. Learn this one first — being stuck inside less with no idea how to leave is a rite of passage worth skipping.
  • Space moves a page down, b a page back, and the arrows move a line at a time.
  • / then a word searches forward; n jumps to the next match.
  • G jumps to the end, g back to the start.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. You want to watch a server log while you reload a page. Which command?

2. You are inside less and cannot get out. What do you press?

3. Why check a file's size before running cat on it?

Your task / مهمتك

Find a genuinely long text file on your machine — a log under /var/log, or any file with hundreds of lines. Report on it as a page: how many lines it has, its first few and last few, and one thing you learned from reading the end that the beginning did not tell you. Say which command you used for each and why that one.

ابحث عن ملف نصي طويل فعلًا على جهازك — سجل تحت /var/log، أو أي ملف بمئات الأسطر. وأبلغ عنه في صفحة: كم سطرًا فيه، وأوائله وأواخره، وشيء واحد تعلمته من قراءة النهاية لم تخبرك به البداية. وقل أي أمر استخدمت لكل واحد ولماذا ذاك.

  • A real file, with its line count ملف حقيقي بعدد أسطره
  • Its beginning and its end, shown separately بدايته ونهايته معروضتان منفصلتين
  • The command used for each, with the reason الأمر المستخدم لكل واحدة، مع السبب
  • One thing the end told you that the start did not شيء أخبرتك به النهاية ولم تخبرك به البداية
How do you want to submit? / كيف تريد التسليم؟