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
Six ways of pointing at things, three of which name the same file. The first works whatever your working directory is; the second and third do not.

The four shortcuts

WrittenMeansExample use
.The directory I am incp ~/file.txt .
..One level upcd ..
~My home directorycd ~/Downloads
/The top of the treels /
-The directory I was in beforecd -

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.

/home/ahmed/projects/site/index.html
Run the demonstration to see the same file named both ways, from two different starting points. The exercise worth doing is covering the answer and working out the relative path yourself — that is the skill, and it is the one thing here that does not come from memorising.

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.

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

Preview / المعاينة

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

1. You are in /home/ahmed/projects/site. What does ../notes.txt point at?

2. Why should a script use absolute paths?

3. A site works on your Mac and breaks on the Linux server, missing an image. Likely cause?

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 تؤكد أنك نفّذت كل واحد، لا أنك استنتجته على الورق
How do you want to submit? / كيف تريد التسليم؟