Linux Series: Bash Programming & Operating Systems (Part-2)

Linux Series: Bash Programming & Operating Systems (Part-2)

Overview

  • Welcome to part 2 of our Linux Fundamentals series! In this section, we will be exploring Ubuntu, one of the most popular Linux distributions.

  • Throughout this part, we will be diving into the command line interface, a powerful tool that allows you to interact with the system using text commands.

  • We'll be covering some of the most useful and commonly used commands in Linux, helping you to build your knowledge and confidence.

Starting the Terminal

  1. What Is A Terminal?

    • A terminal, also known as a command-line interface (CLI) or console, is a text-based interface used to interact with a computer's operating system.

    • It provides a way for users to issue commands and receive output directly from the system.

    • Many experienced users prefer the terminal because it allows for more precise control and can automate repetitive tasks using scripts.

    • Additionally, some tasks can only be performed through the terminal, making it an essential tool for system administration and software development.

  2. What Is A Shell?

    • A shell is a program that provides an interface between the user and the operating system, enabling users to interact with the system through the command line interface (CLI) or terminal.

    • When a user enters a command in the terminal, the shell interprets that command and executes it on behalf of the user.

    • The shell is responsible for managing the input and output of commands, as well as handling things like environment variables, user permissions, and command history.

    • Most of the shells by default use bash as their programming language.

  3. Bash Programming

    • Bash (Bourne-Again Shell) is a popular shell in Linux and Unix-based operating systems. It's not only a powerful tool for interacting with the system but also a versatile programming language.

    • Overall, Bash programming is a useful skill for anyone who wants to work with Linux systems, automate tasks, or perform system administration tasks. With its simple syntax and powerful capabilities, Bash is a versatile tool that can help you achieve your goals efficiently and effectively.

  4. Shell Format

    • Most of the shells that are out there they have the following format

        username@hostname:~ $
      

Structure and Tree Hierarchy of Directory 📁

  • Let's understand this with images,

  • Now, visually you are familiar with the Structure and Tree Hierarchy of Directory.

Exploring Commands 💻

  • First, let's print "Hello World" into the console using Bash.

    echo Hello World!

  • Relative Path and Absolute Path- Relative path specifies the location of a file or directory relative to the current working directory, whereas an absolute path specifies the exact location of a file or directory from the root directory.

    • This is a very important topic, let me explain it to you with an amazing story,

      • Let's understand it, In Linux, each directory or folder is like a room and every room is a path.

      • The path guides you in reaching the directory, whether from the "home" directory or the current directory you're currently in. So here comes two paths, absolute and relative.

      • An absolute path is like giving the exact address of a room, starting from the front door of the building.

      • It includes the entire path from the root directory all the way to the destination directory.

      • For example, the absolute path to the "Bootcamp" folder in the "home" directory might be:

          /home/username/Bootcamp
        
      • A relative path, on the other hand, is like giving directions to a room from your current location.

      • It only includes the path from the current directory to the destination directory.

      • For example, if you're already in the "home" directory, the relative path to the "Bootcamp" folder might be:

          Bootcamp
        
      • This is because the "Bootcamp" folder is located in the "home" directory, which is your current directory.

  • Let's start exploring.

    1. pwd - It stands for "print working directory" which is the directory or folder that the user is currently in within the file system hierarchy.

    2. ls - This command lists the files and directories in the current directory.

    3. cd - This command changes the current working directory to the specified directory.

    4. mkdir - This command creates a new directory.

    5. mkdir -p - This command creates a new directory and any necessary parent directories.

    6. touch - This command creates a new empty file.

    7. gedit a.txt - This command opens the file "a.txt" in the Gedit text editor.

    8. file - This command provides information about the type of a file.

    9. cat - This command displays the contents of a file.

    10. history - This command displays a list of previously executed commands.

    11. Navigating History (up and down-button) - These keys are used to navigate through the command history.

    12. Ctrl + C - This key combination interrupts the currently running process.

    13. Ctrl + r - This key combination searches the command history for a previously executed command.

    14. Autocomplete (tab) - This key is used to autocomplete commands and file/directory names.

    15. cp - This command copies files or directories from one location to another.

    16. mv - This command moves files or directories from one location to another.

    17. rm and rmdir - These commands delete files and directories, respectively.

    18. rm -r - This command recursively deletes all files and directories in the specified directory.

    19. rm -rv - This command recursively deletes all files and directories in the specified directory and provides verbose output.

    20. find - This command searches for files and directories that match specified criteria.

    21. man - This command displays the manual page for a specified command.

    22. whatis COMMAND - The whatis command is useful when you're not sure what a particular command does or want to quickly find out what a new command you've discovered does.

  • example: whatis ls

      ls(1)               - list directory contents
    

Flags 🏴

  • In Bash, flags are also referred to as options or switches. They are additional parameters that can be passed to a command to modify its behavior.

Let's see some Flags::

  1. . : This flag represents the current directory.

    • For example, ls . would list the contents of the current directory.
  2. .. : This flag represents the parent directory of the current directory. It is often used to move up one level in the directory hierarchy. For example, cd .. would move the user up one level in the directory hierarchy.

  3. ~ : This flag represents the user's home directory. It is often used to refer to the home directory of the current user.

    • For example, cd ~/Desktop would change the current directory to the Desktop folder in the user's home directory.
  4. - : This flag represents the previous directory. It is often used to quickly switch between two directories.

    • For example, cd - would switch the user to the previous directory they were in.
  5. / : This flag represents the root directory of the file system. It is often used to refer to the top-level directory in the file system hierarchy.

    • For example, ls / would list the contents of the root directory.

You can find me on these platforms 🤝 :

After reading this blog, give your reviews and feedbacks for more improvements.

Did you find this article valuable?

Support Muhammad Noorani by becoming a sponsor. Any amount is appreciated!