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:
-
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 namedfile.txtin the same directory would simply be:file.txt -
Using
..to Move Up: You can use..to refer to the parent directory. For example, if you are in/home/user/testdirand want to access a file in/home/user, you can use:../file.txt -
Combining Paths: You can combine relative paths to navigate through directories. For example, if you want to access a file in a subdirectory called
subdirwithin 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.
