Day 2: Exploring Files and Directories with Bash - #TWSBashBlazeChallenge๐Ÿš€

Gauri ShirkandeGauri Shirkande
3 min read

๐ŸŒŸ Introduction

Welcome to Day 2 of the #TWSBashBlaze Challenge. In this blog, we will create a bash script that serves as an interactive file and directory explorer. We'll create a script that will allow you to explore the files and directories in the current path interactively and also provide a character-counting feature for the user's input. So without wasting any time let's dive in!

๐Ÿ“‚Part 1: File and Directory Exploration ๐Ÿ”

In this task, we are going to create a bash script explorer.sh which will display a welcome message and list all the files and directories in the current path upon execution. Our script will not only allow us to navigate through files and directories in the current path but also display their names and sizes in a human-readable format (e.g., KB, MB, GB). We'll achieve this functionality using the powerful ls command with appropriate options. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

๐Ÿ”คPart 2: Character Counting ๐Ÿ“

๐Ÿ”On displaying the file and directory list, the script will prompt the user to enter a text-based input. The script will keep running in a loop until the user enters an empty string (๐Ÿ‘‰the user presses Enter without any text). If the script is non-empty then it will count the number of characters present in the input. The character count will be displayed immediately after the input is passed. ๐Ÿ”ค

Now let's dive in and write a script that fulfills all the above requirements!๐Ÿ’ป๐Ÿš€

#!/bin/bash

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!๐Ÿ‘‹"

# Run the infinite loop of while until user wants to exit
while true; do

    # Display the list of files and directories with their names and sizes
    echo "Files and Directories in the Current Path:"
    for item in *; do
        if [ -e "$item" ]; then
            size=$(ls -lh "$item" | awk '{print $5}')
            echo "Name: $item  Size: $size"
        fi
    done

    # Take the input from user to decide whether to continue looping
    read -p "Enter a text input and press enter if you want to exit" input

    # Exit if the input is empty
    if [ -z "$input" ]; then
        echo "Exit the Interactive Explorer! Bbyee...See you!๐Ÿ‘‹"
        break
    fi

    # Count the number of characters in an input text and then display the count
    char_count=${#input}
    echo "Character count: $char_count"

done

๐Ÿ” Usage:

  1. Save the script in a file named explorer.sh.

  2. Make the script executable using the command: chmod +x explorer.sh.

  3. Execute the script using: ./explorer.sh.

๐Ÿ’ปOutput Screen:

๐Ÿ“ Conclusion:

Congratulations! You have successfully completed Day 2 of the #TWSBashBlazeChallenge. You now have a versatile script that lets you explore files and directories in your current path, and it also includes a useful character-counting feature. ๐Ÿš€ I hope you enjoyed building this script and learning more about Bash scripting. Stay tuned for more exciting challenges in the upcoming days!

Till then happy learning and keep exploring!๐ŸŽˆ๐ŸŒŒ

0
Subscribe to my newsletter

Read articles from Gauri Shirkande directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gauri Shirkande
Gauri Shirkande