A Beginner's Guide to Shell, Navigation
Understanding how to navigate the shell to perform simple task
What is "the Shell"?
Before we go properly into the subject of how to navigate the shell, let us first look at what "the Shell" is.
This is a bit lengthy and well explained, in case you just want to get an overview of the Shell, Navigation, you can just move to the conclusion section.
The Shell which is also called a command interpreter, is a program that interprets and executes commands given to it by users. This program takes these commands from the keyboard and directs them to the computer system to perform.
The Shell is one example of Command Line Interfaces (CLIs), in the earliest days of computers, the CLIs were the only User Interfaces that were available to UNIX-based Systems such as Linux etc. unlike now that we also have Graphical User Interfaces (GUIs) alongside the Command Line Interfaces (CLIs).
On most Linux-based Operating Systems, such as Ubuntu, CentOS, Fedora etc. the shell program used is called the BASH (Bourne Again SHell), which is an enhanced/improved version of the original version of the UNIX Shell program, sh. This bash is written by Steve Bourne. Although bash is not the only shell that is available for UNIX-based systems, some others include; Zsh (Z Shell), Fish (Friendly Interactive SHell), Ksh(Korn SHell), Csh (C SHell), Tcsh (TENEX C Shell) and Dash (Debian Almquist SHell) etc.
On Windows-based Operating Systems, the shell program used is called PowerShell. There is also another with almost the same function as the PowerShell, it is called Cmd (Command Prompt) etc.
Another very important thing we need to mention and talk about before we go into the main Shell Navigation is "the Terminal".
What is "The Terminal"?
We have to talk about the terminal because the shell and the terminal work hand in hand. To interact with the shell, you need to use the terminal.
Thus, we can say that the Terminal is a software application that provides a text-based interface that displays the shell's output and also allows users to interact with the computer system.
We can then say that the Terminal is what receives our commands as input and returns output to us. While the shell is what takes those commands, interprets the commands and passes them to the computer to process, which afterward displays the result on the terminal.
In other words, the Terminal is what we use to interact with the shell.
Navigating The Shell
Navigating the shell might look strange if it is your first time experiencing a CLIs, most especially if you are used to the GUIs, hence as you get used to it, you begin to get excited as you will be able to achieve so much more. By just the strike of your keyboard you will be able to move across your system without moving your mouse and without even a single mouse click. Below are the basic commands you will need to be able to navigate/move around your shell.
Moving Across Directories (Folders)
Working on a computer necessitates that we move across various directories/folders to access files of different types. Hence the need for us to know how to move from one directory to another. In other to achieve this, we have majorly three commands to help us:
The List command (
ls
)The Change Directory Command (
cd
)The Print Working Directory Command (
pwd
)The List Command -
ls
The list command
ls
is a command that lists all the directories and files that is in your present file path/location on the computer system.ls
From the image above we can see that using the ls
command and press the enter key
returns all the directories and files in the current path/location in the computer system.
The Change Directory Command -
cd
- Generally, the command used to move across directories is the cd (change directory) command. This command is used to move from one directory to another, it can be used to move either forward or backward. You can move from a parent directory to a child directory or from a child directory back to a parent directory.
cd Desktop
From our image above, you can see that to move from where we are to Desktop, we have to use the cd command followed by the name of the directory we want to move to, which is Desktop.
Now to move backwards, that is from the Present directory back to our parent directory, we will have to use the command:
cd ..
that is the
cd
command followed by two dots/full stop.
Moving across directories in the shell can be done in two ways, either by using the relative position of the directories or by using their absolute position.
Relative Position
This is a way of specifying the path of the directory based on the current directory you are presently in. For Example;
From the image above, we moved to the directory with the name GideonBature
, before we were able to do that, we had to first move from the present path (the location where we are) to Desktop
, and then from Desktop
we moved to GideonBature
directory, simply because GideonBature
is inside of Desktop
, so to get to the directory GideonBature
, we needed to move to the Desktop
directory first. This is how the relative position work.
Absolute Position
This is a way of specifying the full path/location of a directory from the parent/root directory. For example;
From the image above, instead of going into the Desktop
directory first before going to the GideonBature
directory, we simply move directly into the GideonBature
from our present directory by specifying the full path of the GideonBature
directory from the root directory.
cd ./Desktop/GideonBature
the code here is telling the shell to move to the directory GideonBature under the Desktop directory that is present in the root directory.
The Print Working Directory Command -
pwd
This command does just one thing - it shows where you presently are in your computer system. It shows you the full path/location, that is from the root directory down to your current directory directory.
For example;
pwd
From the image above, you can see how it showed the path from the root folder /
down to the present directory which is GideonBature
.
How to Display the Content of a File
To display the content of a file use the less
command. You use the less command followed by the name of the file. For example, using the less README.md
shows the content of the README.md file.
less README.md
And it opens up the content of the file README.md
To exit the file, just use the q
key on your keyboard.
How to Create a File or Directory
- To create a file, use the
touch
command, which is the command touch followed by the name of the file. For example;
touch GideonBature
The command above creates a file called GideonBature
.
To create a directory/folder, use the
mkdir
command, use the command followed by the name of the directory you want to create. For example;mkdir GideonDirectory
This command creates the directory named
GideonBature
.
How to Delete a File or Directory
To delete or remove a file, we use the
rm
(remove) command, we use this command followed by the name of the file to remove a particular file. For example;rm GideonBature
This will remove the file GideonBature permanently from the computer system if it exists in that particular path.
To remove or delete an empty directory, use the
rmdir
(remove directory) command. For example;rmdir GideonDirectory
This command removes the directory named GideonDirectory.
Using the rmdir
to remove the directory which contains other files or sub-directories will not work. In this case, you can use the rm -r
command. For example, using the rmdir
command leaves us with the image below:
to remove the directory, use the rm -r
command, as shown below;
rm -r notEmpty
How to Copy or Move a File or Folder
To copy a file, use the cp
command, which involves copying a file from its present directory to another directory. For examples;
cp file1 dir1
this copies the file file1
to the directory dir1
To copy a directory/folder use the
cp -r
command. For example;cp -r dir2 dir1
This copies the dir2 to the dir1.
To move a file or directory from one directory to another, use the command
mv
. For example;mv file1 dir2
This command moves the file file1
to the directory dir2
. This can be seen in the image below;
mv dir2 dir1
This command moves the directory dir2
to the directory dir1
. This can be seen in the image below;
How to Rename a File or Directory
To rename a file or a directory, use the mv
command, it is the same command as the move command, the difference here is in the method of application. Here you use the mv
command followed by the name of the file or directory you want to change followed by the name you want to change it to.
mv dir1 renamedDir1
This renames the dir1
directory to renamedDir1
.
mv file2 renamedFile2
This renames the file2
file to renamedFile2
.
Summary
The shell is one of the many Command Line Interfaces (CLIs) that interprets and executes commands given to it by users, it is also called a command interpreter.
The Terminal is a program that helps users interact with the shell. It receives our input, passes it to the Shell and also prints out the result of the command we passed to the shell. Eg. the Bash (Bourne Again SHell).
There are many commands we can run on the Terminal which are executed by the shell, these commands are:
The
ls
command - lists files and folders.The
cd
command - moves from one directory to another.The
pwd
command - prints on screen the working directory.The
less
command - opens files, displaying the contents of the file.The
touch
command - creates files.The
mkdir
command - creates directories.The
rm
command - removes/deletes files.The
rmdir
command - removes/deletes empty directories.The
rmdir -r
command - removes directories containing files or other directories in them.The
cp
command - copies a file to another folder.The
cp -r
command - copies a folder to another folder.The
mv
command - moves a file to another directory, it also moves a directory to another directory.The
mv
command - this same command renames files and folders.