Introduction

Why do you need to learn the shell? In the world of computers, the shell is like a crucial doorway to how operating systems really work. It's also called the command line interface (CLI), where users control their computers using text-based commands. Even though we see a lot of fancy graphical interfaces (GUIs) in modern computing, the command line is still super important for both beginners and experts.


Learning the command line is like learning the language of computers. It gives you a lot of power to do things quickly and efficiently, like managing software and controlling how your system operates. In today's tech world, being good at using the command line is a really valuable skill because it's used for things like automation and remote control.

There are many reasons to learn about the shell:

  • The shell makes your tasks less boring. In computers, you often have to do the same things over and over, especially with lots of files. Learning the shell lets you teach your computer to do those repetitive tasks automatically.
  • The shell makes your tasks less likely to have mistakes. When people do the same thing many times, they often make errors. But your computer can repeat the same task many times without making any mistakes.
  • The shell makes your work easier to repeat. When you do your work using the command-line (instead of a Graphic User Interface), your computer remembers every step you took(history). This means you can easily do the same work again when needed. It also lets you clearly explain(comment) what you did, so others can check or do the same thing with new information.

In this guide, we explore why learning about the command line is important. We'll cover the basics, practical uses, and the many benefits it offers to those who take the time to learn its ins and outs.

What is "the shell"?

A shell is a computer program that presents a command line interface (CLI) which allows you to control your computer using commands entered with a keyboard instead of controlling graphical user interfaces (GUIs) with a mouse/keyboard combination.


If you have a Mac or Linux computer, you can use a program called Terminal to access the shell. It's already on your computer. But if you're using Windows, you have to download a separate program to access the shell.

Looking Around

Now that we've mastered navigating the shell file system—like printing the working directory and switching between directories—it's time to delve into exploring the shell file system further.

Below are some of the basic commands to help us look around the shell:

  • ls - list files and directories (as discussed before)
  • less - view text files
  • file - classify a file's content

less - A program that lets us view text files:

[lebomadikane@lebo-MacBook-Pro]$ less text_file

file - A program that will examine a file and tell us what kind of file it is and data a file contains:

[lebomadikane@lebo-MacBook-Pro]$ file filename
Manipulating Files

In this lesson, we'll cover the following commands:

  • cp - copy files and directories
  • mv - move or rename files and directories
  • rm - remove/delete files and directories
  • mkdir - create a directory
  • touch - create a file

The cp command copies files and directories:

[lebomadikane@lebo-MacBook-Pro]$ cp file1 file2

The code above copies the content of file1 into file2. If file2 doesn't exist, it's created. Otherwise, file2 is silently overwritten with the contents of file1.


The mv command moves and renames files and directories. To rename a file, it is used like this:

[lebomadikane@lebo-MacBook-Pro]$ mv filename new-filename

To move files (and/or directories) to a different directory:

[lebomadikane@lebo-MacBook-Pro]$ mv filename directory

The rm command removes/deletes files and directories:

[lebomadikane@lebo-MacBook-Pro]$ rm file1 file2

To remove directories we have to use the flag(or option) -r:

[lebomadikane@lebo-MacBook-Pro]$ rm -r directory-name1 directory-name2

The mkdir command is used to create directories:

[lebomadikane@lebo-MacBook-Pro]$ mkdir directory-name

The touch command is used to create files:

[lebomadikane@lebo-MacBook-Pro]$ touch filename
TEXT Editors

We've done a lot of work with existing files, but what if we want to write our own files? There are many reasons we might want to do this, or to edit an existing file. To add text to files, we'll use a text editor.

Text editors are like digital notebooks where you can write and modify text. They are essential in shell because they facilitate working with text files. Some common ones include:

  • Vim
  • Emacs
  • Nano

They have helpful tools like finding words and making text look colorful to help you work better.

Bash

One great advantage of the command line is the ability to write scripts. Scripts enable you to save a list of commands in a single file for future execution and combine multiple commands together. While writing scripts may require some initial time investment, it can save you a lot of time in the long run, especially when you need to run them repeatedly.


To ensure your file functions as a bash script, here are some important steps to follow:

  • Include the "Shebang" line as the opening line of your file: #!/bin/bash.
  • Grant execution permissions to your file using the chmod command.
  • Optionally, you can use the .sh extension for your bash script file.

These steps help ensure that your file behaves as a bash script and can be executed properly.

Conclusion

In conclusion, mastering shell basics opens a gateway to efficient command-line navigation and automation. Understanding commands, file manipulation, and script writing empowers users to streamline tasks and optimize workflows. Embrace the command line as a versatile tool for enhanced productivity and control over your computing environment.