Learning Linux Basics
The pages can be downloaded via the green button at the top right of the page. The commands are shell commands and can be executed in a terminal, however, do not copy the %%bash magic command that is used to execute the commands in the Jupyter Notebook environment.
|
Navigating directories and files in Linux is a fundamental skill that allows you to fully exploit the power of the command-line interface (CLI). Mastering essential Linux navigation commands such as cd
, pwd
, ls
, and tree
enables you to effortlessly move through the filesystem, display the contents of directories, and understand your location within the system.
Basic Linux Navigation Commands
Here is a brief overview of some of the most common commands used for navigation in Linux:
cd
– Change Directory
The cd
command allows you to move between directories within the filesystem. It is one of the most frequently used commands when navigating the CLI.
Example:
%%bash
cd /tmp
pwd
This command will move you to the specified directory. If no path is specified, typing cd
on its own will take you to your home directory.
-
Absolute Path:
/tmp/documents
(moves directly to thedocuments
directory within/tmp
) -
Relative Path:
../documents
(moves up one directory and intodocuments
)
pwd
– Print Working Directory
The pwd
command prints the full path of your current directory. This is particularly useful when you’ve navigated through several directories and want to confirm your current location in the filesystem.
Example:
%%bash
mkdir /tmp/documents
cd /tmp/documents
touch file.txt
pwd
ls
– List Directory Contents
The ls
command lists all files and directories within the current directory. By default, it shows a simple list, but there are many options to customize what is displayed.
Basic usage:
%%bash
cd /tmp/documents
ls
Additional options:
-
ls -l
: Displays detailed information about each file, such as permissions, owner, size, and modification date. -
ls -a
: Lists all files, including hidden files (those starting with a dot). -
ls -lh
: Displays sizes in human-readable format (KB, MB, etc.).
%%bash
cd /tmp/documents
ls -l
%%bash
cd /tmp/documents
ls -a
tree
– Display Directory Structure
The tree
command visually displays the directory structure as a tree. It’s a useful tool for understanding the layout of files and directories.
Example:
%%bash
cd /tmp/documents
tree ..
This will output a nested tree structure representing the contents of the current directory and its subdirectories.
Practical Examples
Here are some practical examples of how you might use these commands:
-
Navigating to a specific directory:
%%bash
cd /var/log
-
Confirming your location in the filesystem:
%%bash
cd /var/log
pwd
-
Listing files in the current directory in detail:
%%bash
cd /var/log
ls -l
-
Viewing the directory structure:
%%bash
cd /var/log
tree