Bash Relative vs. Absolute path

In Bash, paths are used to specify the location of files and directories. There are two types of paths: relative paths and absolute paths.

  1. Relative Path:
    A relative path specifies the location of a file or directory relative to the current working directory. It does not start with the root directory (represented by “/”) but is based on the current location in the directory hierarchy. Common symbols used in relative paths include:
  • . (dot): Represents the current directory.
  • .. (dot-dot): Represents the parent directory. For example:
  • ./file.txt: Refers to the file file.txt in the current directory.
  • ../folder/file.txt: Refers to the file file.txt inside the folder directory that is one level up from the current directory. Relative paths are useful when navigating and referencing files and directories in relation to the current working directory. However, the same relative path can have different meanings depending on the current working directory.
  1. Absolute Path:
    An absolute path specifies the complete path to a file or directory from the root directory. It starts with the root directory (“/”) and provides the full directory hierarchy to reach the target location. For example:
  • /home/user/file.txt: Refers to the file file.txt in the user directory inside the home directory, starting from the root directory. Absolute paths provide a fixed and unambiguous way to refer to files and directories, regardless of the current working directory. They are useful when you need to specify an exact location in the file system.

When working with paths in Bash, it’s important to understand the distinction between relative and absolute paths, as using the appropriate type ensures accurate referencing of files and directories.

Leave a Comment

Your email address will not be published. Required fields are marked *