Author's image | Midjourney and Canva
It should come as no surprise to anyone reading this that the Unix/Linux file system is organized in a hierarchical structure. The topmost directory is the root directory, denoted by /
All other directories and files are organized under this root. In addition to this, each user has a home directory, usually represented by ~
where personal files and directories are stored.
While much of file management can be done through GUI file managers on Linux and other Unix-like systems, using the Bourne-Again Shell (bash) command line provides a way to do it that is simple, flexible, fast, and has the ability to interact with a variety of command line utilities and applications.
Let's see how to navigate the Unix/Linux file system using bash.
He pwd
Domain
Aim: Displays the current working directory.
Use: He pwd
The command is easy to use. It displays the absolute path of your current location in the file system.
Example:
Sailing with cd
Aim:Change directories.
Basic use: He cd
The command allows you to move to a different directory.
Common options:
- Moving to the home directory:You can quickly navigate to your home directory using
cd ~
Or simplycd
. - Upload a directory:To go up to the main directory, use
cd ..
. - Navigate to a subdirectory:To enter a subdirectory within the current directory, simply use
cd subdirectory_name
.
List of files with ls
Aim:List files and directories.
Basic use: He ls
The command lists the files and directories in the current directory.
Common options:
- Detailed listing:For a detailed view, use
ls -l
This provides information such as file permissions, link count, owner, group, file size, and timestamp. - Including hidden files:To include hidden files (those that start with a
.
), wearls -a
. - Combining options:Combine options to obtain a more complete list with
ls -la
.
Viewing the directory structure with tree
Aim:Display directories in tree format.
Basic use: He tree
The command provides a visual representation of the directory structure.
Common options:
- Limited depth:To limit the depth of the directory tree, use
tree -L
followed by the desired level. - Show hidden files:Include hidden files in the tree with
tree -a
.
Practical example
Let's look at a practical example of how to navigate to a directory, list its contents, and display its structure.
cd ~/projects
pwd
ls -la
tree -L 1
Explanation:
cd ~/projects
: Navigate to theprojects
directory in your home directory.pwd
:Please confirm your current directory.ls -la
:List all files, including hidden ones, with detailed information.tree -L 1
:Displays the directory structure up to one level deep.
Combining commands for efficient navigation
You can chain commands using &&
to run multiple commands in sequence. This can speed up your workflow.
cd ~/projects && ls -la && tree -L 2
This script navigates to the projects
directory, lists all files with details and then displays the directory structure up to two levels deep.
Final Thoughts
Practicing the commands presented in this article will help you become more proficient in navigating the Unix/Linux file system. Once you are comfortable with these basics, you can explore more advanced file navigation and manipulation commands.
Matthew May (twitter.com/mattmayo13″ rel=”noopener”>@mattmayo13) holds a master's degree in computer science and a postgraduate diploma in data mining. As editor-in-chief of KDnuggets & Statologyand contributing editor at Mastering Machine LearningMatthew aims to make complex data science concepts accessible. His professional interests include natural language processing, language models, machine learning algorithms, and exploring emerging ai. His mission is to democratize knowledge in the data science community. Matthew has been coding since he was 6 years old.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>