Lesson 4 / الدرس 4
Paths: absolute, relative, and the shortcuts / المسارات: المطلق والنسبي والاختصارات
Every command that touches a file needs to be told which one. There are two ways to say it, and knowing which you are using is the difference between a command that works anywhere and one that only works where you happen to be standing.
كل أمر يمسّ ملفًا يحتاج أن يُقال له أيّه. وثمة طريقتان لقول ذلك، ومعرفة أيهما تستخدم هي الفرق بين أمر يعمل في أي مكان وأمر لا يعمل إلا حيث تقف.
An absolute path starts at / and gives the full route from the top of the tree. It means the same thing typed anywhere, by anyone, at any time. A relative path starts from wherever you currently are — shorter to type, and meaningless without knowing the working directory.
/ ويعطي الطريق الكامل من أعلى الشجرة. وهو يعني الشيء نفسه مكتوبًا في أي مكان، من أي أحد، في أي وقت. أما المسار النسبي فيبدأ من حيث أنت الآن — أقصر كتابةً، وبلا معنى دون معرفة المجلد الحالي.You are in /home/ahmed/projects
/home/ahmed/projects/site/index.html absolute — works from anywhere
site/index.html relative — only from here
./site/index.html the same thing, said explicitly
../notes.txt up one level, then notes.txt
~/projects/site/index.html from your home directory
/ the top of the tree
The four shortcuts
| Written | Means | Example use |
|---|---|---|
| . | The directory I am in | cp ~/file.txt . |
| .. | One level up | cd .. |
| ~ | My home directory | cd ~/Downloads |
| / | The top of the tree | ls / |
| - | The directory I was in before | cd - |
The last one is less known and unreasonably useful: cd - jumps back to where you just were, so you can bounce between two directories without retyping either.
cd - يقفز عائدًا إلى حيث كنت للتو، فتتنقل بين مجلدين دون إعادة كتابة أي منهما./home/ahmed/projects/site/index.html
Which to use, and when
Relative paths for what you are working on right now — they are shorter and they survive the whole project being moved somewhere else. Absolute paths whenever the command must not depend on where it is run: in a script, in a scheduled job, in anything you will paste into a server. A script that assumes a working directory breaks the first time someone runs it from elsewhere.
/ ليس "داخل المجلد الحالي" — بل من أعلى النظام كله. وكتابة rm -r /tmp وأنت تقصد مجلد tmp الذي أمامك تحذف مجلد النظام بدلًا منه. والفرق بين tmp و/tmp حرف واحد ونتيجة شديدة الاختلاف، فاقرأ أول حرف من كل مسار في أمر يحذف.Assets/logo.png والمجلد في الحقيقة assets يعمل تمامًا على جهازك ويفشل لحظة نشره. طابق حالة الأحرف بالضبط دائمًا — فهذا من أشيع أسباب "لكنه يعمل على جهازي".Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
You are in /home/ahmed/projects/site. What does ../notes.txt point at?
/home/ahmed/projects/site. إلامَ يشير ../notes.txt؟.. goes up exactly one level, to projects, and then names notes.txt inside it.
.. مستوى واحدًا بالضبط إلى projects، ثم يسمّي notes.txt بداخله.2. Why should a script use absolute paths?
A relative path silently means something different depending on where the script was started. In a scheduled job, that is rarely where you expect.
3. A site works on your Mac and breaks on the Linux server, missing an image. Likely cause?
Case sensitivity differs between the two systems, so Assets and assets are one folder on your Mac and two different names on the server.
Assets وassets مجلد واحد على جهازك واسمان مختلفان على الخادم.Score / النتيجة: 0 / 3
Your task / مهمتك
Pick one real file on your own machine that is at least three levels deep. Write a page giving its absolute path, then the relative path to it from three different starting directories — including one that has to go up before it goes down. Check each one by actually running ls on it, and say so.
اختر ملفًا حقيقيًا واحدًا على جهازك عمقه ثلاثة مستويات على الأقل. واكتب صفحة تعطي مساره المطلق، ثم المسار النسبي إليه من ثلاثة مجلدات بداية مختلفة — بينها واحد يضطر للصعود قبل النزول. وتحقق من كل واحد بتنفيذ ls عليه فعلًا، وقل ذلك.
- One real file, with its absolute path ملف حقيقي واحد بمساره المطلق
- Three relative paths from three different places ثلاثة مسارات نسبية من ثلاثة مواضع
- At least one uses .. واحد منها على الأقل يستخدم ..
- You confirm you ran each one, not worked it out on paper تؤكد أنك نفّذت كل واحد، لا أنك استنتجته على الورق