In Linux, paths are used to specify the location of files and directories within the file system. Understanding how paths work is essential for navigating the system, managing files, and executing commands effectively.
A path is a string that represents the location of a file or directory in the Linux file system. Paths are used in commands to tell the system where to find or place files. There are two types of paths in Linux:
- Absolute Path
- Relative Path
An absolute path specifies the location of a file or directory from the root of the file system, which is denoted by a forward slash /
. An absolute path always begins with /
, regardless of your current working directory.
Example:
/home/username/Documents/report.txt
/etc/nginx/nginx.conf
In these examples, /home/username/Documents/report.txt
is the full path to the report.txt
file, starting from the root directory (/
).
A relative path specifies the location of a file or directory relative to your current working directory. Unlike absolute paths, relative paths do not start with a /
.
Example:
Documents/report.txt
../username/Documents
If your current directory is /home/username
, the relative path Documents/report.txt
refers to the file /home/username/Documents/report.txt
. The ..
in ../username/Documents
indicates moving up one directory level.
You can combine absolute and relative paths in commands to navigate the file system effectively. For example, you can use cd /etc
to move to the /etc
directory (absolute path), and then use cd ../home
to move to the /home
directory relative to your current location.
Linux uses several special directories and path symbols that are important to understand:
-
Root Directory (
/
): The top-level directory in the Linux file system hierarchy. All other directories and files reside under this directory. -
Home Directory (
~
): A shortcut for the current user's home directory. For example,~/Documents
refers to/home/username/Documents
. -
Current Directory (
.
): Represents the current working directory. For example,./script.sh
refers to a file namedscript.sh
in the current directory. -
Parent Directory (
..
): Represents the parent directory of the current directory. For example,../
moves up one level in the directory hierarchy. -
Root User's Home Directory (
/root
): The home directory of the root (superuser) account.
Here are some practical examples to illustrate how paths work in Linux:
-
Moving to a Directory Using an Absolute Path:
cd /var/log
This command changes the current directory to
/var/log
, where system logs are typically stored. -
Moving to a Directory Using a Relative Path:
cd ../Downloads
If you are currently in
/home/username/Documents
, this command moves you to/home/username/Downloads
. -
Copying a File Using an Absolute Path:
cp /home/username/picture.jpg /tmp
This command copies the file
picture.jpg
from the/home/username/
directory to the/tmp
directory. -
Moving a File Using a Relative Path:
mv ./report.txt ../Documents
This command moves the file
report.txt
from the current directory to theDocuments
directory, which is one level up.
In Linux, environment variables can also store path information, making it easier to access frequently used directories or executables. One of the most important environment variables related to paths is PATH
.
-
PATH
Variable: A colon-separated list of directories where the system looks for executable files when you type a command. You can view it using:echo $PATH
Adding a directory to your
PATH
allows you to run scripts or programs from that directory without needing to specify the full path.export PATH=$PATH:/home/username/scripts
This command adds
/home/username/scripts
to yourPATH
, allowing you to execute scripts from this directory directly.
Understanding Linux paths is fundamental to navigating the file system, managing files, and executing commands effectively. By mastering the use of absolute and relative paths, as well as special directories and environment variables, you can work more efficiently in the Linux environment.
Next: File System Overview
Previous: Terminal Basics