Shell Navigation: A Beginner's Guide

Shell Navigation: A Beginner's Guide

ยท

13 min read

Hello there! ๐Ÿ‘‹ Congratulations on taking your first step to learn about shell navigation. It's a valuable skill that allows us to efficiently manage and manipulate files and directories on our computer.

During your learning journey, you will come across some terms that can be confusing at first, such as shell, terminal, command line, command prompt, bash, Linux, Unix, Ubuntu, and operating system. But don't worry! In this blog, we will break down this confusion and understand the difference between these terms.

Additionally, we will explore the file system hierarchy, which is the structure of how files and directories are organized in our computer. We will also cover some of the most useful commands of shell navigation.

So, let's get started!

Clearing the Confusion: Understand the Key Terminologies

Before we dive into the world of commands and file systems, let's make sure we are on the same page by clearing up some key terminologies.

Operating system:

An operating system is a software, that is responsible for managing all the components or elements of a computer system, including the hardware, software, and user interactions. It controls how the programs and applications interact with each other and with the computer's hardware. It also provides a platform for users to run programs and perform tasks. Examples of operating systems are Microsoft Windows, macOS, Linux, Unix, Android, and iOS.

An operating system can be thought of as a manager of a company. Just like a manager oversees employees, resources, and operations to ensure the smooth functioning of a company, an operating system oversees the computer's hardware, software, and user interactions to ensure the smooth functioning of the computer system.

Linux Vs Unix

Some people get confused between Linux and Unix because they are both operating systems that share many similarities. Let's think of Linux and Unix as two different brands of cars. Just like different car brands have their unique characteristics and features, Linux and Unix have their unique features and characteristics that make them differ from each other. Linux is an example of an open-source operating system, whereas Unix is an example of a family of proprietary operating systems that are owned by different companies.

Note: "Open source" means that the source code of a software or operating system is freely available for anyone to view, modify, and distribute. "Proprietary" operating system is a software that is owned by a company and its source code is not available to the public.

As we previously mentioned Linux and Unix share many similarities, that is why Linux is called Unix-like. Those similarities are represented in the command-line interface, file system hierarchy, and networking capabilities. However, they have some differences in terms of their design, licensing, and software package.

Ubuntu:

Ubuntu is a popular distribution of Linux. Let's back to our car analogy, Linux is the car brand and Ubuntu is the car model. Just like a car model within a brand has its own features and characteristics that make it unique from other models within that brand. Ubuntu has unique features that differentiate it from other Linux distributions. Linux has other distributions such as Fedora, Debian, and Red Hat, each with its own unique set of features, software, and configurations.

Shell, Terminal, Command Line, Command Prompt:

There is often confusion around the terms terminal, shell, command line, and command prompt, leading some to believe they all mean the same thing. But, do they actually mean the same thing? Let's explore and clarify the relationship between these terms.

Shell is a program that acts as an intermediate between the user and the operating system. it interprets the user's commands and sends them to the operating system to execute them. There are several types of shells available in various operating systems, including Bash (Bourne-Again Shell), Korn Shell (ksh), and PowerShell.

Terminal and command prompt both refer to the program that provides a command-line interface(CLI) to communicate and interact with the shell. On the Windows operating system, the program that provides a command-line interface is called the "Command Prompt", which is often abbreviated as "cmd". On other operating systems like MacOS and Linux the program that provides the command-line interface is called the "Terminal". Command Line on the other hand refers to the area within the command line interface, where the user types commands. It is called "Command Line" because the user inputs commands one line at a time.

To summarize, the terminal provides a command line interface for the user to communicate with the operating system. It is a program that allows the user to type commands and receives text-based output. When the user type a command in the command line and presses Enter. The shell such as Bash or PowerShell, reads and interprets the command and sends them to the operating system to execute them. It acts as an intermediary between the user and the operating system. It also displays the output of the command back to the user through the command line interface.

Directories and files:

A directory is a folder that can contain files and other directories. Think of it like a container that holds other containers(directories) and items(files). A file, on the other hand, is a collection of data that can be stored on a computer. it can contain text, images, videos, or any other type of data.

The main difference between files and directory is that the file stores data while the directory can store files and other directories.

Commands and flags:

A command is an instruction given to a computer program to perform a particular operation or task. For example, "ls" is a command that lists the files and directories in the current working directory.

A flag is an additional argument that can be added to a command to modify its behavior. Flags are typically represented by a hyphen ("-") followed by a letter or a word that indicates the specific option. For example, the "ls" command can be modified with the "-l" flag to show more detailed information about the files and directories in a directory.

The File System Hierarchy:

The File System Hierarchy is the way files and directories are organized on our computers.

In Linux, File System Hierarchy Standard(FHS) is a set of rules that defines the organization and structure of files and directories. It outlines where different types of files should be stored to ensure consistency and compatibility across different Linux distributions.

To understand the File System Hierarchy in Linux, let's use the analogy of a tree. The trunk of the tree represents the root directory, denoted by the forward-slash ("/"). All other directories and files branch off from the root directory like the branches of a tree.

This image provides a visual representation of the File System Hierarchy of Linux, showing the root directory and its sub-directories, and how they are organized in a hierarchical structure.

The difference between the / (root) directory, /root directory, and /home directory:

  • / (root) directory: This is the top-level directory in the Linux File System Hierarchy, and it is represented by the forward slash (/). This directory contains all other directories and files in the system and is the starting point for navigating the file system.

  • /root directory: A subdirectory of the / (root) directory. It is the home directory for the root user (the administrative user in Linux). It contains the root user's personal files and settings. It is only accessible by the root user.

  • /home directory: A subdirectory of the / (root) directory. It is used as the home directory for regular users on a Linux system.

In short, the / (root) directory is the top-level directory in a Linux file system hierarchy. The /root directory is the home directory for the root user. And the /home directory is the home directory for regular users on the system.

Essential Commands for Navigating the Shell:

Navigating the shell is an essential skill for anyone working in a Linux environment. It allows us to move between directories, create new files and directories, and manage existing ones. In this section, we will cover some common commands and their flags that you can use to navigate the shell with ease.

mkdir:

The mkdir command is used to create a new directory.

For example, to create a directory called "mydir" we type the following command:

We can also create multiple directories at the same time.

Take, for instance, creating "mydir1", "mydir2", and "mydir3" at the same time:

touch:

The touch command is used to create a new file.

For example to create a file called "file1" we type the following command:

And To create "file2", and "file3" at the same time, we use this command:

ls:

The ls command is used to list the contents of a directory.

Let's list the content of the /test directory and see the files that we have created using touch and the directories that we have created using mkdir:

As we can see in /test directory we have 4 directories and 3 files.

To list the content of the current working directory in a detailed, long format. We add "-l" flag to ls which displays additional information about each file, such as the file size, owner, and permissions(which will be covered in the next blogs).

Trying it on our /test directory will give us a better understanding:

Moreover, to display all files including hidden files in the current working directory, we use the following command:

ls -a

And to display all files including hidden files in the current working directory in long format. we use this command:

ls -la

Let's create some hidden files using the touch command and hidden directories using mkdir. Then try the ls command and ls with flags and observe the result. To create a hidden file or directory we add a (".") dot before the name of the file or directory.

From the example above we can clearly see that:

ls: Lists the contents of the current directory (in this case, /test) without showing hidden files and hidden directories.

ls -l: Lists the contents of the current directory in the long format, which includes details such as file permissions, ownership, size, and date modified, but does not show hidden files and hidden directories.

ls -a: Lists all contents of the current directory, including hidden files and hidden directories (whose names begin with a dot, e.g. .hidden_file1, .hidden_dir1).

ls -la: Lists all contents of the current directory in the long format, including hidden files and hidden directories.

pwd:

The pwd command is used to display the current working directory.

To demonstrate, the pwd displayed our working directory which is /test.

cd:

The cd command is used to change the current working directory. Here are some simple examples of how to use cd [directory], cd .., and cd -:

cd [dirname]: This command is used to move to a specific directory.

For example, let's move from /test to "mydir" directory, and use pwd to check our current directory.

"cd ..": This command is used to move one directory level up from the current working directory.

For instance, we are now in "mydir" to move up to its parent directory, we type "cd .."

"cd -": This command is used to move to the previous working directory.

As an illustration, we were previously in "mydir" and using "cd .." we move up to its parent directory /test, to switch back to the previous directory "mydir" we use "cd -".

less:

This command is used to view the contents of a file.

For example, let's use echo to write text in the "file1" inside /test directory, and use less to check if the file contains what we have written inside it.

when we press Enter this is what we get:

To return to the terminal we press the "q" key.

cat:

This command is used to print the content of the file in the terminal.

Let's print the content of file1 in the terminal:

The cat command is also used to concatenate files and print their contents in the terminal.

Let's add some contents to "file2" and use cat to print the contents of file1 and file2 at the same time.

cp:

The cp command is used to copy files and directories. Here are some examples of how to use the cp command:

- To copy a file's contents to another file. We use the following syntax:

cp source_file destination_file

As an example, Let's copy the contents of file1 to file3 and use the cat command to compare their contents:

- To copy a file to a directory, we use this syntax:

cp file_to_copy destination_directory

As an instance, let's copy "file2" inside "mydir"

- To copy a directory inside another directory along with its contents, we use the following syntax:

cp -r directory_to_copy destination_directory

For example, let's create some directories and files inside "mydir1" and copy "mydir1" inside "mydir".

mv:

This command is used to move or rename files or directories. Here are some examples of how to use the mv command:

- To move a file to another directory, we use the following syntax:

mv file_to_move destination_directory

To demonstrate, let's move "file3" to "mydir2":

- To move a directory inside another directory, we use the following syntax:

mv directory_to_move destination_directory

For instance, let's move "mydir2" to "mydir":

- To rename a file, we use the following syntax:

mv old_file_name new_file_name

For example, let's change the name of "file2" to "file2_updated":

Note: When renaming a file, it's important to note that if the new file name already exists in the current directory, the "mv" command will move the contents of the file inside the existing file with the same name. The same rule applies to directories as well. For instance, if you have two directories called "mydir" and "newdir" and you try to rename "mydir" to "newdir", the "mv" command will move "mydir" inside "newdir". To prevent this from happening, it's crucial to ensure that the new file or directory name is unique within the current directory. You can use the "ls" command to list the contents of the directory and check for existing names if you are unsure.

rmdir:

This command is used to delete an empty directory.

As an illustration, let's delete the empty directory mydir3.

Note: We can not delete a directory with contents using rmdir.

As an example, we could not delete "mydir" using rmdir because "mydir" is not empty.

rm:

This command is used to remove files and directories.

- To remove a file, we use the following syntax:

rm file_name

Let's try this command by removing "file1" from the "test" directory:

- To remove a nonempty directory called, we use use the syntax below:

rm -r directory_name

For example, let's remove "mydir" directory from "test" directory.

Note: It is important to note that using rm followed by the "-r" flag will delete all the files and directories inside that directory, without any confirmation prompt, this action can not be undone. To minimize the risk of accidentally deleting files, we can add the "-i" flag when using rm. To confirm the deletion, we type "y" and press Enter. If we don't want to delete the file or directory, we type "n" and press Enter to skip it.

Let's now try to remove "mydir" from the "test" directory using -i flag:

To sum up, this mind map provides a concise summary of the navigation commands that we have covered in this blog:

Excellent job on reaching this point! In this blog, we have covered the key terminologies that are often confusing for beginners, such as operating systems, Linux and Unix, Ubuntu, shell, terminal, command line, and command prompt. We have also discussed directories and files and the file system hierarchy, which is the structure of how files and directories are organized on our computer. By mastering the concepts covered in this blog, you will be well on your way to become proficient in shell navigation. Don't forget to practice and explore more commands to enhance your skills.

References:

Shotts, W. E. Jr. Learning the shell. Retrieved from linuxcommand.org/lc3_learning_the_shell.php

ย