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.
| Command | Does | Reach for it when |
|---|---|---|
| cat file | Prints the whole file at once | The file is short |
| less file | Opens it scrollable, quits with q | The file is long |
| head file | The first 10 lines | You want to see the shape of it |
| tail file | The last 10 lines | The newest entries in a log |
| tail -f file | The last lines, and keeps watching | A log, while something is happening |
| wc -l file | Counts the lines | You 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.
head وtail كلاهما -n: فـtail -n 50 يعطي آخر خمسين. والجدير بحفظ اسمه هو tail -f في السطر ما قبل الأخير.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 العادي يطبع نهاية الملف ويتوقف. أما tail -f فيطبع النهاية ثم يبقى، فيطبع كل سطر جديد ما إن يُكتب. صوّبه إلى سجل خادم، ثم حمّل الصفحة في متصفح، فتشاهد طلبك أنت وهو يظهر لحظة حدوثه. وهي أسرع طريقة لمعرفة ما يعتقد الخادم فعلًا أنه خطأ. واضغط Ctrl + C لتوقف المشاهدة.$ 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
less, and getting back out of it
-
q quits. Learn this one first — being stuck inside
lesswith no idea how to leave is a rite of passage worth skipping.q يخرج. تعلّم هذا أولًا — فالعلوق داخلlessدون معرفة كيفية المغادرة طقس عبور يستحق تخطّيه. -
Space moves a page down, b a page back, and the arrows move a line at a time. Space ينزل صفحة، وb يرجع صفحة، والأسهم تتحرك سطرًا سطرًا.
-
/ then a word searches forward; n jumps to the next match. / ثم كلمة يبحث إلى الأمام؛ وn يقفز إلى المطابقة التالية.
-
G jumps to the end, g back to the start. G يقفز إلى النهاية، وg إلى البداية.
cat أبدًا على ملف لم تتحقق من حجمه. فسجل بمليون سطر يغرق الطرفية دقائق، والملف الثنائي — صورة أو برنامج مترجَم — يطبع بايتات تفسّرها طرفيتك رموز تحكم، فيبقى العرض مشوّهًا وأحيانًا غير صالح. فإن حدث ذلك، اكتب reset واضغط Enter وإن كنت لا تقرأ ما تكتب.ls -lh يُظهر الحجم وwc -l عدد الأسطر. وفي أي شيء لست واثقًا منه، يكون head الفتحة الآمنة — فعشرة أسطر تخبرك هل هو نص أصلًا، ولا تكلّف شيئًا إن لم يكن.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. You want to watch a server log while you reload a page. Which command?
-f keeps the command alive, printing each new line as it is written. The others all read the file once and stop.
-f الأمر حيًا، فيطبع كل سطر جديد ما إن يُكتب. أما البقية فتقرأ الملف مرة وتتوقف.
2.
You are inside less and cannot get out. What do you press?
less ولا تستطيع الخروج. ماذا تضغط؟q quits. Ctrl + Z would suspend it instead, leaving it running in the background — lesson 19 explains why that is different.
3.
Why check a file's size before running cat on it?
cat عليه؟A binary file prints bytes the terminal reads as control codes. head is the safe way to find out what you are dealing with.
head هو الطريق الآمن لمعرفة ما تتعامل معه.Score / النتيجة: 0 / 3
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 شيء أخبرتك به النهاية ولم تخبرك به البداية