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
The 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.

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
Read the second line again. > 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.

/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.

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

Preview / المعاينة

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

1. What does > do to a file that already exists?

2. Why does sort names.txt > names.txt lose the file?

3. What does 2> /dev/null mean?

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 شرح لأي قناة ذهبت إلى أين
How do you want to submit? / كيف تريد التسليم؟