Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux Absolute And Relative Paths

In this tutorial, we will learn about the differences between absolute and relative paths in Linux and how to use them while navigating the filesystem. Understanding paths is important for working with the command line and managing files and directories.

  • Absolute paths

An absolute path is a complete path that describes the exact location of a file or directory in the filesystem, starting from the root directory /. Absolute paths always begin with a forward slash /.

Example of an absolute path:

/home/username/Documents/report.txt

In this example, the file report.txt is located in the Documents directory under the user's home directory /home/username.

To navigate to a directory using an absolute path, you can use the cd command. For example, to navigate to the /etc directory, you can use:

cd /etc
  • Relative paths

A relative path is a path that describes the location of a file or directory in relation to the current working directory. Unlike absolute paths, relative paths do not start with a forward slash /.

Example of a relative path:

Documents/report.txt

In this example, the file report.txt is located in the Documents directory relative to the current working directory.

To navigate to a directory using a relative path, you can use the cd command. For example, if your current working directory is /home/username, you can navigate to the Documents directory like this:

cd Documents
  • Special symbols for relative paths
  • . (dot): Represents the current directory.
  • .. (double dot): Represents the parent directory (one level up).

To navigate to the parent directory of the current working directory, use:

cd ..

To navigate to a sibling directory, first navigate to the parent directory using .., and then navigate to the sibling directory. For example, to navigate from /home/username/Documents to /home/username/Downloads, use:

cd ../Downloads
  • Combining absolute and relative paths

You can use a combination of absolute and relative paths when navigating the filesystem or working with files and directories. For example, if your current working directory is /home/username, you can use the following command to copy a file from /etc to the Documents directory:

cp /etc/somefile.txt Documents/

In this command, /etc/somefile.txt is an absolute path, and Documents/ is a relative path.

By understanding the differences between absolute and relative paths in Linux, you can effectively navigate the filesystem and manage files and directories using the command line.

  1. Navigating directories with relative paths in Linux: Relative paths are specified with respect to the current working directory. Use .. to move up one level.

    cd Documents/photos
    cd ../videos
    
  2. Linux directory structure and absolute vs relative paths: Linux directory structure is hierarchical. Absolute paths start from the root directory (/), while relative paths are relative to the current working directory.

    # Absolute path
    cd /home/user/Documents
    
    # Relative path
    cd Documents
    
  3. Working with absolute paths in Linux commands: Absolute paths specify the full directory path, starting from the root directory.

    rm /home/user/Documents/file.txt
    
  4. Using relative paths for file manipulation in Linux: Relative paths are handy for file manipulation within the current directory or its subdirectories.

    cp ../images/photo.jpg .
    
  5. Path separators in Linux and absolute paths: In Linux, / is used as a path separator in absolute paths.

    /home/user/Documents/file.txt
    
  6. Relative path examples in Linux: Relative paths are specified based on the current working directory.

    # Move to the parent directory
    cd ..
    
    # Move to the home directory
    cd ~
    
    # Move to the user's home directory
    cd ~/Documents
    
  7. Linux commands and specifying file paths: Various Linux commands accept both absolute and relative paths for file operations.

    cat /var/log/syslog
    tail -n 10 ../logs/error.log
    
  8. Resolving symbolic links in absolute paths in Linux: Symbolic links can be resolved in absolute paths.

    ls -l /path/to/symlink
    
  9. Linux cd command and changing directories with relative paths: The cd command is used to change directories with either absolute or relative paths.

    cd Documents/photos
    
  10. Path expansion and completion in Linux: Linux shells often support path expansion and completion for efficiency.

    cp ~/Documents/pic[TAB] /media/backup
    
  11. Using tilde (~) in Linux paths for home directory: The tilde (~) represents the user's home directory.

    cd ~
    cp file.txt ~/backup