CIS120Linux Fundementals
pwd, cd and tree Commands
The cd
, pwd
, and tree
commands are essential tools for navigating the Linux file system efficiently. The cd
(change directory) command allows users to move between directories, making it possible to access files and organize the system effectively. The pwd
(print working directory) command helps users confirm their current location within the file system, which is particularly useful when working with deeply nested directories. The tree
command provides a structured view of the directory hierarchy, displaying files and subdirectories in an easy-to-read format. Together, these commands enable users to move through the file system, verify their position, and visualize the directory structure, improving workflow and organization.
pwd Command
The pwd
command in Linux stands for "print working directory." This command is used to display the current directory path you are working in. It is especially useful for verifying your location within the file system, which can help avoid confusion when navigating complex directory structures.
The command below outputs the absolute path of the current working directory. For example, if you are in /home/user/Documents
, this command will output /home/user/Documents
.
pwd
Outputs:
$ pwd
/home/user/Documents
cd Command
The cd
command in Linux is used to change the current working directory. This command is fundamental for navigating the file system. Understanding how to use cd
effectively will help you move between directories quickly and efficiently.
What the cd Command Does
The basic usage of the cd
command changes the current working directory to the specified directory. It allows you to move to different directories, access files, and organize your work environment.
NOTE: In Linux directory structures, the single dot (".") represents the current directory, while the double dot ("..") refers to the parent directory, which is one level above the current directory. The dot (".") is often used to indicate files or commands within the current directory, such as "./script.sh" to run a script located in the directory you are currently in. On the other hand, the double dot ("..") is used to navigate up the directory tree. For example, using "cd .." will move you to the parent directory. These shortcuts provide an efficient way to reference and navigate directories in Linux.
Common Path Shortcuts for the cd Command
Here is a table of some of the most common path shortcuts you can use with the cd
command and their descriptions:
Shortcut | Description |
---|---|
.. or ../ |
Move up one directory level (parent directory) |
~ |
Change to the home directory |
- |
Change to the previous directory |
/ |
Change to the root directory |
. or ./ |
Stay in the current directory |
Relative and Absolute Paths
Understanding the difference between relative and absolute paths is crucial for effective navigation in the Linux file system.
Absolute Path
An absolute path is the complete path from the root directory to the desired directory or file. It always starts with a forward slash /
.
cd /home/user/Documents
This command uses an absolute path to change the current directory to /home/user/Documents
.
Relative Path
A relative path specifies a location relative to the current directory. It does not start with a forward slash/
.
cd Documents
If you are currently in /home/user
, this command uses a relative path to change the directory to /home/user/Documents
.
Examples:
Changing to a Specific Directory:
This command changes the current working directory to /home/user/Documents
.
cd /home/user/Documents
Moving Up One Directory Level (parent directory):
This command moves the current working directory up one level. For example, if you are in /home/user/Documents
, this command will take you to /home/user
.
cd ..
Changing to the Home Directory:
This command changes the current working directory to the home directory of the user. For example, if your home directory is /home/user
, this command will take you there from any location.
cd ~
Changing to the Root Directory:
This command changes the current working directory to the root directory of the file system.
cd /
Returning to the Previous Directory:
This command changes the current working directory to the previous directory you were in. It is useful for toggling between two directories.
cd -
Staying in the Current Directory:
This command keeps you in the current directory. It is often used in scripts to explicitly state that the current directory should remain unchanged.
cd .
tree Command
The tree
command in Linux is used to display the directory structure in a hierarchical format, making it easier to visualize the organization of files and subdirectories. By default, it starts from the current directory and recursively lists all files and directories in a tree-like structure. Users can modify its behavior with options such as -L
to limit the depth of recursion or -d
to show only directories.
The tree
command supports several options to customize its output, as shown in the table below:
Option | Description |
---|---|
-L N |
Limits the depth of directory traversal to N levels. For example, tree -L 2 would just go two levels deep. |
-d |
Displays only directories, excluding files. |
-a |
Shows all files, including hidden ones. |
-f |
Prints the full path for each file and directory. |
-h |
Displays file sizes in a human-readable format. |
--du |
Shows the cumulative disk usage of each directory. |
--dirsfirst |
Lists directories before files in the output. |
Below is a sample output of the tree
command. In this example, the user is in a directory that contains three subdirectories: Documents
, Pictures
, and Scripts
. The tree
command displays the hierarchical structure of these folders and their contents. The Documents
folder contains two files, report.docx
and notes.txt
. The Pictures
folder has a subdirectory named vacation
, which contains two image files, beach.jpg
and sunset.jpg
. The Scripts
folder includes two script files, script.sh
and backup.py
. This output provides a clear visual representation of how files and directories are organized within the current working directory.
.
├── Documents
│ ├── report.docx
│ ├── notes.txt
├── Pictures
│ ├── vacation
│ │ ├── beach.jpg
│ │ ├── sunset.jpg
├── Scripts
│ ├── script.sh
│ ├── backup.py