A Linux Beginner's Guide: Shell Navigation
How to Navigate the Shell and Complete Tasks
Table of contents
- Pre-requisites
- What is Shell?
- Why learn Shell?
- Shell-like terminologies
- Shell Basics
- Shell Commands
- Shell Navigation
- Basic tasks in Shell
- Creating a file or directory
- touch
- mkdir
- Copying a file or directory
- cp <filename> <filename>
- cp <filename> <dirname>
- cp -r <dirname> <dirname>
- Moving/renaming a file or directory
- mv <filename> <directoryname>
- mv <filename1> <filename2>
- mv <directoryname1> <directoryname2>
- Removing a file or directory
- rm <file1> <file2>
- rm -r <dirname1> <dirname2>
- Conclusion
This guide will discuss the Shell, how to navigate in the Shell, and the basic tasks you can carry out in the Shell interface in Linux.
Pre-requisites
This guide requires the following Tools/Skills:
Installation of a terminal
Basic understanding of operating a terminal
What is Shell?
Shell is a computer interface that links you and the operating system(OS) by taking your commands and generating an output through communication with the OS. The OS exposes its services to human users or other programs. It's called a "Shell" because it acts as the outer layer around the OS.
In the past, users performed tasks on the operating system through Shell because it was the only available interface. Nowadays, Graphical User Interfaces (GUIs) coexist alongside.
Why learn Shell?
You might wonder why it is necessary to learn how to use the Shell despite the existence of GUIs for navigating around your OS. Using Shell as a programmer/software engineer can enhance your productivity and save you time.
Learning offers several benefits:
Control & Flexibility: The Shell enables far greater control over your device. You can execute commands to interact with databases, initiate servers, modify permissions, view hidden files, etc.
Speed: Once you master the basics, performing tasks within the Shell allows you to complete them faster than a GUI. For example, you can create 5,000 files with a single line of commands.
Automation: You can automate repetitive tasks, which saves you time.
Availability: The Shell is available on all major operating systems. It's readily available on Mac & Linux out of the box. With a few minor tweaks, It also works on Windows.
Job Requirement: Jobs related to Software Engineering, Sysadmin, DevOps, Security, Machine Learning, etc., require a basic understanding of the Shell.
Shell-like terminologies
You may confuse some shell-like terminologies with Shell. Examples are Terminal, Bash, and Command-line Interfaces (CLIs).
Terminal
The terminal program runs a shell. You can interact with the Shell and type your commands through a terminal. Initially, physical devices with a screen and keyboard operated them, but nowadays, they exist as software.
Bash
Bash is the default shell program on most Unix-like systems, such as Linux. The name bash is an acronym for Bourne-Again SHell, which steve bourne wrote as the improved version of sh. Other shell programs exist. Examples are ksh, fish, and zsh. Zsh serves as the shell program for Apple's Mac.
Command-Line Interface
The command line interface is a text-based user interface that interacts with a computer or program through commands from a user. An example is the Shell.
Shell Basics
Open a terminal of your choice to access the Shell on your device. If you use Linux or Mac, your system should have a default terminal application that can access the terminal by simply searching on your device.
If you are using a Windows device, running Linux commands requires a few steps:
Windows Subsystem for Linux (WSL): WSL enables you to run Linux on Windows. It's not a virtual or segregated machine but rather a way to directly enter Linux commands within the command line on Windows.
Installation through the Web: To install WSL through the Internet, search for WSL on your browser and follow the installation guidelines provided.
Installation through Powershell: To install WSL, open Powershell on your device and input
wsl --install
. This action will install Ubuntu on your system upon reboot.
Set a username and password: The terminal prompts you to set a username and password. Although you won't see the password as you type, it's being recorded. Retype the password as prompted and proceed.
Install Windows Terminal (optional): You can utilize the newly installed Ubuntu terminal or install a Windows terminal, which enables you to run different programs such as Powershell, Ubuntu, etc.
Linux shell environment
This guide will use the Windows Terminal to access and explore the Linux Shell environment. Upon successful installation and login, the Shell will show your username and the device name, followed by a dollar ($) sign. The appearance of your shell environment should resemble the following image:
The highlighted section of the image represents your Command-Line. This is where you input commands to interact with your machine and execute various tasks.
Shell Commands
You can use basic commands to interact with your machine within the Shell. These commands include:
whoami
In your Shell, you enter the command whoami
and press Enter
, to print the username currently logged in. An example is shown in the following image:
man
You enter man
in your Shell, and it displays the manual page for commands. The man
command is the abbreviated form for manual. To learn more about any Shell command, press man <command>
. The following image shows the example, man whoami
:
After pressing Enter
,
The preceding image provides you with more information about the whoami
command. The name and synopsis with a detailed description explaining whoami
are shown.
clear
You enter clear
to clear all previous commands executed in the Shell. The shortcut Ctrl+L
offers an alternative to inputting the clear
command.
After pressing Enter
,
Shell Navigation
You can access and navigate your files and directories using the Shell. Using the Shell is faster compared to the GUI file management system. For example, accessing and navigating 1000 files using the Shell is less cumbersome than using GUI.
To navigate files, you need a basic understanding of file paths.
File Path
The file path in Shell allows you to navigate files and directories. The File path specifies the location of a file and directory on your system. The two main file paths are:
Root Directory (
/
)Home Directory (
~
)
The difference between the root and home directories is as follows:
Root Directory | Home Directory |
The root directory on your system contains all other directories and files. | Exist as a subdirectory in your root directory. |
The admin (root) user can change the files or settings. | The user cannot change the files or settings. |
The forward slash / represents the root directory. | The tilde ~ serves as a shorthand representation. For instance, the path home/mustapha/pictures/Giraffe.jpg transforms into ~/pictures/Giraffe.jpg |
The admin alone can create a user, such as a home directory user. | A home directory user cannot initiate another user. |
The root directory contains every data in the Linux file system. | The home directory has only a specific user's data. |
Navigating through directories
In the Shell, learning how to navigate through your files or directories becomes essential due to the absence of a graphical interface that allows you to see your file or directories like a GUI. Two commands that mainly perform these actions consistently are:
Print Working Directory (
pwd
)Change Directory (
cd
)
pwd
In the Shell, you exist in a working directory at any given moment. Within that directory, you can observe the pathway of the file, its parent directory, and any subdirectories it may have.
You use the pwd
command to see the name of the directory you are currently in.
When you log in to your Linux system, it sets the working directory as your home directory. The /home/user_name
represents the depiction of the home directory.
cd
In the Shell, you use cd
to move across your files or directories. The cd command enables you to enter a folder or directory, move from a parent directory to a subdirectory, and vice versa. To change a working directory, type cd pathname
to move into the stated directory. A pathname
represents the route you take to reach the desired directory.
They are two ways to specify pathnames:
Absolute pathname
Relative pathname
Absolute pathname
In an absolute pathname, the location of a file or directory is defined from its root directory. The absolute pathname is established by starting at a root directory and writing a slash /
after every directory name. For example,
cd /home/mustapha/Desktop
The absolute pathname /home/mustapha/Desktop
specifies that the initial root directory /
is the root. You descend into the home
directory at level one, then into the user mustapha
at level two, and then into Desktop
which is level three. Therefore, the preceding image depicts that Desktop
is four levels within the root.
Relative pathname
The relative pathname is the path that starts from your current working directory. Linux provides shortcuts for relative pathnames that utilize either the current or parent directory to specify its relative path. Here are shortcuts for stating a relative pathname:
.
(a single dot): Represents your current directory...
( two dots): Represents the parent directory.
For example, if you are currently in the /home/mustapha/Desktop
directory, you can now use the command cd ..
to move to the parent directory /home/mustapha
.
In the preceding image, the command cd ..
allows you to move one level up from /home/mustapha/Desktop
into /home/mustapha
.
To move two levels up, you can use cd ../..
to specify the relative pathname. This enables you to go from /home/mustapha/Desktop
into /home
.
ls
The ls
command lets you view the files and directories in your stated path. As the shell environment lacks a GUI, it presents only the names of the files without a graphical representation.
When you type ls
in the Shell, your device lists the files and directories in the following image.
ls
enables you to view the files and directories in the file path /home/mustapha
. Directories are highlighted in blue, while files are displayed in white.
Less
The less
command is used to display the content of a text file. To view the stated text file, you type less<filename>
. For example, you can view the content of the demo text file with less example
.
After pressing Enter
,
To exit from the file, press q
.
Basic tasks in Shell
You can perform most of these tasks in a GUI file manager by dragging and dropping files and utilizing the cut, paste, and copy shortcuts. While a GUI facilitates simple tasks, you can easily accomplish the same tasks in the command-line interface, such as Shell.
In the Shell, you can perform a few basic tasks and actions. Some of them include:
creating a file or directory
copying a file or directory
moving/renaming a file or directory
removing a file or directory
Creating a file or directory
The command touch
creates a file and mkdir
creates a directory/folder.
touch
To create an empty file, use touch
. You accomplish this by typing touch<filename>
. For example, type touch Greenhouse
to initiate a file named Greenhouse
.
After pressing Enter
, I listed files and directories in the /home/mustapha
path using ls
.
You can observe the creation of the empty file Greenhouse
, which now exists in the /home/mustapha
file path.
mkdir
The command mkdir
creates a directory/folder. To create a directory, you can type mkdir<filename>
. For example, type mkdir Emissions
to initiate a directory named Emissions
.
After pressing Enter
and list with ls
,
You can observe the creation of the directory Emissions
in the path /home/mustapha
, which is highlighted in blue. The blue highlights show it's a directory, not a file.
Copying a file or directory
You can carry out a few tasks while copying, such as copying the content of a file into another, copying a file into a directory/folder, or copying a directory into another.
cp <filename> <filename>
The command cp
copies a file or directory. To copy a file into another, you need to specify the file you want to copy and the name of the new file.
For example, to copy the file Greenhouse into Climate, you type cp Greenhouse climate
After pressing Enter
and listing with ls
,
Any content in Greenhouse
copies to Climate
. If Climate
doesn't exist prior, it is created as seen above; otherwise, it silently overwrites climate
with the content of Greenhouse
.
cp <filename> <dirname>
To copy a file into a directory/folder, you need to specify the file you want to copy and the name of the directory.
For example, to copy the file Greenhouse
into the Emissions
directory, you should type cp Greenhouse Emissions
.
After pressing Enter
, you move into the directory using cd
and list with ls
,
In the preceding image, you can observe a file named Greenhouse
inside the directory named Emissions
.
cp -r <dirname> <dirname>
To copy a directory/folder into another, specify both the source directory you want to copy and the destination directory. cp
stands for copy, -r
denotes recursive, and <dirname>
represents the directory's name.
For example, to copy the directory Emissions
into the directory Pollution
, you should type cp -r Emissions Pollution
.
Press Enter
, and list the content of Pollution
with ls
,
In the preceding image, you can observe that a file Greenhouse
exists in the directory Pollution
. This is because the directory Emissions
had a file named Greenhouse
, which was copied into Pollution
with cp -r Emissions Pollution
.
Moving/renaming a file or directory
The command mv
executes a few tasks, like moving a file into a directory, renaming a file or directory, and moving a directory/folder into another.
mv <filename> <directoryname>
The command mv
moves or renames your file or directory. To move a file into a directory, specify both the filename and the directory's name.
For example, you move a file named waste
into a directory named Pollution
with mv waste Pollution
.
Press Enter
and list with ls
In the preceding image, you can observe that the file waste
is no longer visible because it has been moved into the directory Pollution
.
After being moved, the file waste
now exists in the directory Pollution
.
mv <filename1> <filename2>
The command mv
renames a file if a condition is met. The condition states:
if filename2
doesn't exist, then filename1
is renamed filename2
. If filename2
exists, its content is replaced with the content of filename1
.
For example, to rename Climate
as Environment
, you use mv Climate Environment
After pressing Enter
and listing with ls
,
You can observe that the file Climate
no longer exists because it was renamed Environment
. This renaming was successful because the filename Environment
did not exist prior.
If Environment
existed prior, Climate
would replace the content of Environment
.
mv <directoryname1> <directoryname2>
The command mv
can move one directory/folder into another by specifying both the targeted directory to be moved and the destination directory.
For example, you move a directory named Emissions
into a directory named Pollution
by using the command mv Emissions Pollution
After pressing Enter
and listing the content of Pollution
with ls
,
You can observe that the directory Emissions
now exist in the directory Pollution
because it has been moved.
Removing a file or directory
The command rm
removes/deletes a file or directory. You can execute a few tasks, such as removing a file or multiple files and removing directories.
rm <file1> <file2>
You can use the command rm
to remove multiple files. You can specify and remove or delete as many files as you want.
For example, rm Greenhouse Environment
removes the file Greenhouse
and Environment
.
After pressing Enter
and listing with ls
,
You can observe that the file Greenhouse
and Environment
has been removed.
rm -r <dirname1> <dirname2>
The command rm -r
removes multiple directories. You need to specify the directories you want to remove.
For example, executing rm -r Desktop Pollution
removes Desktop
and Pollution
along with all their content.
After pressing Enter
and listing with ls
,
You can observe that the directory Desktop
and Pollution
has been removed.
rm
as deleting something means it's gone forever.Conclusion
In this guide, you have learned about the Shell, how to navigate in the Shell and the basic tasks you can perform in the Shell. Additionally, we have explored examples of basic actions and demonstrated how to execute them in the Shell.
The use case of the Shell is essential and vast. we've only covered how to navigate and carry out basic tasks. The Shell can perform advanced tasks like I/O Redirection, Expansion, Permissions, etc.., which are useful for a software engineer.