Shell Navigation in Linux Operating System

Shell Navigation in Linux Operating System

Today we will be going through how to navigate a shell in Linux operating system. Before diving deep into the various navigation commands let's first understand what a shell is.

What is a shell?

A linux shell is an program or software that allows a user to interact with the Unix OS. It provide a way to start program, manage files in a file system, and manage processes running in the Linux system. The core of the shell is the command prompt. The navigation in a shell is done using the terminal.

Creating a new Directory(mkdir)

To create a new directory we make use of the mkdir command followed by the directory name. lets create our first directory called hashnode to do this we simply type

  $mkdir hashnode

Creating a new file(touch)

The touch command is used to create a new file. The command begins with touch followed by the file name. Let's create a text file called author we simply write

$touch author.txt

Change to a directory(cd)

The cd command is used to change the working directory. The cd is followed by the name or path of the directory you are navigating to. If we want to change to the hashnode directory we created we use the command.

 $cd hashnode

The dot notation (.) is used in the cd command where . is the working directory and .. is the working directory’s parent directory. Therefore cd.. will take you to the parent directory of your working directory. Tying cd without anything following will the working directory will be changed to your home directory.

Print working directory(pwd)

If you want to know the current directory you are on then the pwd command comes in as it gives you the correct path of the directory you are currently on. To view our current directory we will type

$pwd

The result that follows will be /c/users/hashnode

Listing items in a directory(ls)

The ls command is used to list the content of a directory. If we create two more directories and two text files using the touch command inside your hashnode directory.

$ls

The result displayed will be

blog1/
blog2/
author.txt 
author-social.txt

Using only the ls command will not display the hidden file as it only displays names of files and directories.
Use case of the ls command.

ls: list all files and directories in the working directory.

ls directory-name: list all files and directories in the specified directory.

ls -l: list all files and directories in the working directory in long format.

ls -a: list all files and directories including hidden files. Hidden file names begin with a period or dot[.].

Copying files and directories(cp)

The cp command is used to copy files and directories. The command is followed by the file you want to copy and the destination of the file. I will be using two files (file1, file2) and directories (dir1, dir2) to show use cases of the cp command.

cp file1 file2: copy file1 to file2. If file2 does not exist it is created.

cp file1 dir1: copy file1 to dir1.

cp –r dir1 dir2: copy dir1 to dir2. If dir2 does not exist it is created if it exists dir1 is created inside dir2.

Moving files and directories(mv)

The mv command is used to move and rename files and directories. This command has some use case which are:

mv file1 file2: if file2 exist. Its content is replaced by file1 content. If it does not exist file1 is renamed
file2.

mv file1 dir1: file1 will be moved to dir1. If dir1 does not exist then it will display an error.

mv dir1 dir2: if dir2 does not exist dir1 is renamed as dir2, if dir2 exists then dir1 is moved to dir2.

Removing files and directories(rm)

The rm command is used to remove files, to remove the directory the rm –r is used. The command deletes the file and directory permanently so be careful when using it.

rm file1: remove file1

rm –r dir1: removes dir1

If you want to learn more about the Linux command line then I recommend this resources.