Navigating the File System
Understanding the Linux file system is crucial for effectively using the terminal. Linux organizes files and directories in a hierarchical structure, similar to a tree, with the root directory (/
) at the top.
Current Working Directory
When you open the terminal, you start in a specific directory, known as the current working directory. You can view the current working directory using the pwd
(Print Working Directory) command:
## Example of viewing the current working directory on Ubuntu 22.04
pwd
/home/username
Changing Directories
To navigate to a different directory, you can use the cd
(Change Directory) command. For example, to change to the /home/username/Documents
directory:
## Example of changing to the Documents directory on Ubuntu 22.04
cd /home/username/Documents
You can also use relative paths to navigate, such as cd Documents
to move into the Documents directory from the current working directory.
Listing Directory Contents
To view the contents of a directory, you can use the ls
(List) command. This will display the files and subdirectories within the current working directory.
## Example of listing the contents of the current directory on Ubuntu 22.04
ls
file1.txt file2.txt subdirectory/
The ls
command also supports various options to customize the output, such as ls -l
to display detailed file information, or ls -a
to show hidden files.
Absolute and Relative Paths
In the Linux file system, you can refer to a file or directory using either an absolute path or a relative path. An absolute path starts from the root directory (/
) and specifies the complete path to the target. A relative path, on the other hand, is based on the current working directory.
graph TD
A[/] --> B[home]
B --> C[username]
C --> D[Documents]
C --> E[Pictures]
In the example above, the absolute path to the Documents directory is /home/username/Documents
, while the relative path would be Documents
.