What are relative paths?

QuestionsQuestions8 SkillsProBasic Files OperationsDec, 03 2025
068

Relative paths are file or directory paths that are specified in relation to the current working directory. Instead of starting from the root directory (/), relative paths begin from the current location in the filesystem. This makes them shorter and often easier to use when navigating within a specific directory structure.

Key Points about Relative Paths:

  1. Current Directory Reference: A relative path does not start with a /. It is based on your current directory. For example, if you are in /home/user, a relative path to a file named file.txt in the same directory would simply be:

    file.txt
  2. Using .. to Move Up: You can use .. to refer to the parent directory. For example, if you are in /home/user/testdir and want to access a file in /home/user, you can use:

    ../file.txt
  3. Combining Paths: You can combine relative paths to navigate through directories. For example, if you want to access a file in a subdirectory called subdir within your current directory, you would use:

    subdir/file.txt

Example

Assuming your current directory is /home/user:

  • To access a file in the same directory:
    ls file.txt
  • To access a file in a subdirectory called documents:
    ls documents/file.txt
  • To access a file in the parent directory:
    ls ../file.txt

Summary

Relative paths are convenient for navigating the filesystem without needing to specify the full path, making them useful for scripts and commands when working within a known directory structure.

0 Comments

no data
Be the first to share your comment!