Steps to learn:

  1. Introduction to Basic Commands

    • Explanation of what basic commands are and their importance in Bash
    • List of commonly used basic commands
  2. Navigation Commands

    • Explanation of cd command and its usage to navigate through directories
    • Example story: Navigating to a specific directory to access a file
    • Comparison of cd command with ls command
  3. Listing Commands

    • Explanation of ls command and its usage to list files and directories
    • Example story: Listing all files in a directory
    • Comparison of ls command with dir command
  4. Creating and Deleting Directories

    • Explanation of mkdir command and its usage to create directories
    • Example story: Creating a new directory for a project
    • Explanation of rm command and its usage to delete directories
    • Example story: Deleting a directory that is no longer needed
    • Comparison of mkdir and rm commands
  5. File Manipulation Commands

    • Explanation of touch command and its usage to create empty files
    • Example story: Creating a new file for a project
    • Explanation of cp command and its usage to copy files
    • Example story: Making a backup copy of a file
    • Explanation of mv command and its usage to move or rename files
    • Example story: Renaming a file to make it more descriptive
    • Comparison of touch, cp, and mv commands

1. Introduction to Basic Commands

The objective of this section is to introduce you to the concept of basic commands in Bash, why they are important, and provide a list of commonly used basic commands.

Understanding Basic Commands

In Bash, a command is a directive to the computer to perform a specific task. Basic commands are those commands that are frequently used in Bash for performing day-to-day tasks. They are the building blocks of Bash scripting and form the backbone of any Bash program. It is essential to have a good understanding of basic commands before proceeding with more advanced Bash scripting.

Commonly Used Basic Commands

Here’s a list of some of the most commonly used basic commands in Bash:

cd

The cd command is used to change the current working directory in Bash. For example, to change the current working directory to the Documents directory, use the following command:

cd Documents

ls

The ls command is used to list the contents of a directory in Bash. It displays the names of all files and directories in the current directory. For example, to list the contents of the Documents directory, use the following command:

ls Documents

mkdir

The mkdir command is used to create a new directory in Bash. For example, to create a new directory named Photos in the Documents directory, use the following command:

mkdir Documents/Photos

rm

The rm command is used to remove files and directories in Bash. For example, to remove a file named example.txt in the Documents directory, use the following command:

rm Documents/example.txt

cp

The cp command is used to copy files and directories in Bash. For example, to copy a file named example.txt from the Documents directory to the Pictures directory, use the following command:

cp Documents/example.txt Pictures/

mv

The mv command is used to move files and directories in Bash. For example, to move a file named example.txt from the Documents directory to the Pictures directory, use the following command:

mv Documents/example.txt Pictures/

2. Navigation Commands

The objective of this section is to understand the basic navigation commands in Linux such as cd, ls, and their usage to navigate through directories.

Explanation of cd command

The “cd” command stands for “Change Directory” and is one of the most basic and frequently used commands in Linux. It is used to navigate between different directories in the file system.

The syntax for using the “cd” command is simple: cd [directory_name]

Here, “directory_name” is the name of the directory that you want to change to. For example, if you want to change the current working directory to the “Documents” directory, you can use the following command:

cd Documents

Example Story

Suppose you have a file named “my_file.txt” in the “Documents” folder of your Linux system. To access this file, you need to navigate to the “Documents” folder using the “cd” command.

cd Documents

Once you are in the “Documents” folder, you can use the “ls” command to view the list of files and directories in the current directory.

ls

This will display a list of all the files and directories in the “Documents” folder. You can now locate the “my_file.txt” file and open it.

Comparison of cd Command with ls Command

The “cd” command is used to change the current working directory, while the “ls” command is used to list the contents of a directory.

For example, if you want to change the current working directory to the “Documents” folder and then list its contents, you can use the following commands:

cd Documents
ls

This will display a list of all the files and directories in the “Documents” folder.

In conclusion, the “cd” command is used to navigate between different directories in the file system, while the “ls” command is used to list the contents of a directory. These two commands are fundamental to understanding the basics of Linux and Shell Scripting.

3. Listing Commands

The objective of this section is to understand the basic listing commands in Linux such as ls, dir and their usage to listing files and directories.

Explanation of ls command

One of the most basic tasks in Linux/Unix is listing files and directories. To do so, we use the “ls” command. The “ls” command lists the files and directories in the current directory by default.

Syntax:

ls [options] [directory]

Options:

  • “-a”: List all files, including hidden files
  • “-l”: List files with details like permissions, owners, size, and modification date/time
  • “-h”: Human-readable file sizes. For example, instead of “1024”, show “1K”
  • “-t”: Sort by modification time, newest first
  • “-r”: Reverse the order of the list
  • “-R”: List all files and directories recursively

Example Story

Suppose you have a directory named “documents” in your home directory, and you want to list all files in it. You can do so by opening a terminal and typing the following command:

cd ~/documents
ls

This will list all files and directories in the “documents” directory.

Comparison of ls Command with dir Command

The “dir” command is similar to the “ls” command, but it is used in Windows instead of Linux/Unix. The “dir” command displays a list of files and subdirectories in a directory. However, the options and syntax for “dir” are different from “ls”.

In conclusion, the “ls” command is a basic but essential command in Linux/Unix. It allows users to list files and directories quickly and efficiently. By understanding the options and syntax of the “ls” command, you can customize the output to suit your needs.

4. Creating and Deleting Directories

The objective of this section is to understand how to create and delete directories in Linux using mkdir, rm.

Creating a Directory using mkdir Command

The mkdir (make directory) command is used to create new directories in a Linux system. The syntax for using this command is as follows:

mkdir directory_name

Example Story

Suppose you are working on a new project and you need to create a directory to store all the project files. You can use the mkdir command to create a new directory for your project.

mkdir ~/project

This will create a new directory named “project” in your home directory.

Deleting a Directory using rm Command

The rm (remove) command is used to delete directories and files in Linux. The syntax for using this command is as follows:

rm -r directory_name

The “-r” option is used to delete directories recursively.

Example Story

Suppose you have completed a project and you no longer need the directory that contains all the project files. You can use the rm command to delete the directory and all its contents.

rm -r ~/project

This will delete the “project” directory along with all its contents.

Comparison of mkdir and rm Commands

The mkdir and rm commands are used for creating and deleting directories in Linux, respectively. The mkdir command is used to create new directories, whereas the rm command is used to delete directories and files.

It is important to note that the rm command is a powerful command and can delete directories and files permanently. Therefore, you should be careful while using this command and ensure that you are deleting the correct files and directories.

In this section, you learned about basic commands such as mkdir and rm for creating and deleting directories in Linux. The mkdir command is used to create new directories, whereas the rm command is used to delete directories and files. It is important to be cautious while using the rm command as it can delete files and directories permanently.

5. File Manipulation Commands

The objective of this section is to understand the basic file manipulation commands in Linux and how to use them efficiently.

Touch Command

The touch command is used to create new empty files or update the access and modification times of existing files. The syntax of the touch command is as follows:

touch [option] [filename(s)]

Example Story

For example, to create a new file called “myfile.txt”, you can use the touch command as follows:

touch myfile.txt

This will create a new empty file called “myfile.txt” in the current directory. You can also create multiple files at once by specifying their names separated by spaces:

touch file1.txt file2.txt file3.txt

This will create three new empty files called “file1.txt”, “file2.txt”, and “file3.txt” in the current directory.

Cp command

The cp command is used to copy files and directories from one location to another. The syntax of the cp command is as follows:

cp [option(s)] source_file(s) destination_file_or_directory

Example Story

For example, to make a backup copy of a file called “myfile.txt”, you can use the cp command as follows:

cp myfile.txt myfile_backup.txt

This will create a new file called “myfile_backup.txt” which is an exact copy of the original “myfile.txt” file. You can also copy multiple files at once by specifying their names separated by spaces:

cp file1.txt file2.txt file3.txt backup_directory/

This will copy the three files “file1.txt”, “file2.txt”, and “file3.txt” to a directory called “backup_directory”.

Mv command

The mv command is used to move or rename files and directories from one location to another. The syntax of the mv command is as follows:

mv [option(s)] source_file(s) destination_file_or_directory

Example Story

For example, to rename a file called “myfile.txt” to “mydocument.txt”, you can use the mv command as follows:

mv myfile.txt mydocument.txt

This will rename the original file “myfile.txt” to “mydocument.txt”. You can also move a file to a different directory by specifying the destination directory:

mv myfile.txt documents/

This will move the file “myfile.txt” to a directory called “documents”.

Comparison of Touch, Cp, and Mv Commands

The touch command is used to create new empty files or update the access and modification times of existing files. The cp command is used to copy files and directories from one location to another, while the mv command is used to move or rename files and directories from one location to another.

In summary, basic file manipulation commands like touch, cp, and mv are essential for managing files and directories efficiently in Linux. By understanding these commands and their usage, you can easily create, copy, move, rename, or delete files and directories as needed.

Conclusion

In this post, we introduced the concept of basic commands in Bash, why they are important, and provided a list of commonly used basic commands. We hope this post helps you understand the fundamental concepts of Bash and sets you on the path to mastering Bash scripting.

References