When would you use `.` vs absolute paths?

QuestionsQuestions8 SkillsProDec, 20 2025
099

That is an excellent question! Choosing between a Relative Path (starting with .) and an Absolute Path (starting with /) depends on your specific situation.

Here is a simple breakdown to help you decide:

Use Absolute Paths (Starting with /)

Use these when you want to be 100% certain the command works regardless of which folder you are currently in.

  • System Files: When accessing system folders like /etc, /tmp, or /var.
  • Automation/Scripts: When writing a script that will run automatically, you use absolute paths so it doesn't break if the script starts in a different folder.
  • Example: cat /tmp/hello will work whether you are in your home folder or deep inside a sub-directory.

Use Relative Paths (Starting with ./ or just the name)

Use these when you are already working inside a specific project and want to save time.

  • Convenience: It's much faster to type cat file1 than /home/labex/project/file1.
  • Portability: If you move a whole project folder to a new location, relative paths inside that folder will still work, whereas absolute paths would point to the old location and "break."
  • Running Local Programs: You must use ./ to run a program in your current folder (e.g., ./my_script).

In Short:

  • Absolute (/): "Go to this exact address on the map, no matter where I am standing."
  • Relative (.): "Look at the house right next to me."

In this lab, we use /tmp/hello (absolute) to ensure the command works for everyone, no matter if they opened the terminal in /home/labex or /home/labex/project.

0 Comments

no data
Be the first to share your comment!