Lesson 13 / الدرس 13
Redirection: sending output somewhere else / إعادة التوجيه: إرسال المخرَج إلى مكان آخر
Every command writes to somewhere and reads from somewhere, and both ends can be repointed with a single character. One of those characters silently empties an existing file.
كل أمر يكتب إلى موضع ويقرأ من موضع، ويمكن إعادة توجيه الطرفين بمحرف واحد. وأحد تلك المحارف يُفرِغ ملفًا موجودًا بصمت.
A running command has three connections open. Standard input is where it reads from — normally your keyboard. Standard output is where its results go — normally the screen. Standard error is where its complaints go — also the screen, which is why the two look mixed together. Each can be pointed at a file instead.
ls > files.txt write the output to a file, REPLACING it
ls >> files.txt append to the end of the file instead
sort < names.txt read input from a file
command 2> errors.txt send only the errors to a file
command > out.txt 2>&1 send output and errors to the same file
command > /dev/null 2>&1 throw both away entirely
2 is standard error, which is numbered 2; standard output is 1 and is the default, which is why > on its own means 1>. The last line is a phrase you will meet constantly and can now read: run this and discard everything it says. 2 هو مُخرَج الأخطاء، ورقمه 2؛ أما المُخرَج القياسي فرقمه 1 وهو الافتراضي، ولهذا كان > وحده يعني 1>. والسطر الأخير عبارة ستقابلها باستمرار وتستطيع الآن قراءتها: نفّذ هذا واطرح كل ما يقوله.One arrow replaces, two append
echo "first line" > log.txt log.txt now contains one line
echo "second line" > log.txt log.txt now contains ONE line — the first is gone
echo "third line" >> log.txt log.txt now contains two lines
> empties the file before writing, without asking and without a backup — so a command you meant to add to a log instead wiped it. Run the demonstration to see both side by side.
> يُفرِغ الملف قبل الكتابة، دون سؤال ودون نسخة احتياطية — فالأمر الذي قصدت به الإضافة إلى سجل محاه بدلًا من ذلك. شغّل العرض لترى الاثنين جنبًا إلى جنب.Why errors are separate
Keeping results and complaints on two different channels is what lets you save one without the other. find / -name "*.conf" > found.txt puts the results in a file while the "permission denied" lines still go to your screen — so the file is clean and you can still see what went wrong. Mixing them, which is what 2>&1 does, is a deliberate choice for when you want the whole story in order.
find / -name "*.conf" > found.txt يضع النتائج في ملف بينما تظل أسطر "permission denied" تذهب إلى شاشتك — فيكون الملف نظيفًا وتظل ترى ما اختلّ. أما خلطهما، وهو ما يفعله 2>&1، فاختيار مقصود لحين تريد القصة كاملة بترتيبها.sort names.txt > names.txt يدمّر الملف: إذ تُفرِغ الـshell الوجهةَ قبل أن يقرأ sort سطرًا واحدًا، فلا يجد الأمر شيئًا وتبقى بملف فارغ ولا نسخة. اكتب إلى اسم جديد، وتحقق منه، ثم انقله إلى موضعه.
/dev/null deserves a name. It is a real path that discards everything written to it — the system's waste bin. Sending output there is how you run something for its effect rather than its chatter, and 2> /dev/null is how you silence the errors from a find across directories you do not own.
/dev/null يستحق أن يُسمّى. فهو مسار حقيقي يطرح كل ما يُكتب إليه — سلة مهملات النظام. وإرسال المخرَج إليه هو كيف تنفّذ شيئًا لأثره لا لثرثرته، و2> /dev/null هو كيف تُسكِت أخطاء find في مجلدات لا تملكها.tee: فـls | tee files.txt يطبع ويحفظ في آن. وهو يستخدم الأنبوب من الدرس التالي، وهو الطريقة المعتادة لحفظ سجل تثبيت طويل مع مشاهدته.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What does > do to a file that already exists?
> بملف موجود أصلًا؟No prompt and no backup. >> is the one that adds, and the difference between them is one character.
>> هو الذي يضيف، والفرق بينهما محرف واحد.
2.
Why does sort names.txt > names.txt lose the file?
sort names.txt > names.txt الملف؟Redirection is set up first, by the shell, before the command starts. By the time sort opens the file it is already empty.
sort الملف يكون فارغًا بالفعل.
3.
What does 2> /dev/null mean?
2> /dev/null؟Channel 2 is standard error and /dev/null discards what it receives. The results still come to your screen.
/dev/null يطرح ما يصله. وتظل النتائج تصل إلى شاشتك.Score / النتيجة: 0 / 3
Your task / مهمتك
Demonstrate the difference between the two arrows on your own machine, then use redirection for something genuinely useful: save the output of a real command to a file, and separately save only the errors from a command that produces some — find / -name "*.conf" is a reliable source of both. Show each file afterwards and explain what ended up where, and why.
اعرض الفرق بين السهمين على جهازك، ثم استخدم إعادة التوجيه لشيء نافع فعلًا: احفظ مخرَج أمر حقيقي في ملف، واحفظ منفصلًا الأخطاء فقط من أمر ينتجها — وfind / -name "*.conf" مصدر موثوق للاثنين. وأظهر كل ملف بعدها واشرح ما الذي انتهى إلى أين ولماذا.
- The two arrows demonstrated, with the file shown after each السهمان معروضان، مع الملف بعد كل واحد
- Output and errors separated into two files المخرَج والأخطاء مفصولان في ملفين
- An explanation of which channel went where شرح لأي قناة ذهبت إلى أين