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

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

ยท

3 min read

๐Ÿ“ Introduction:

Welcome to Day 2 of the #TWSBashBlazeChallenge! Today's challenge focuses on building an interactive script that explores files and directories, while also performing character counting in Bash. We'll create a script called explorer.sh that will provide you with a fun and interactive way to navigate through your files and directories, and even count characters in your input! Let's get started with some Bash magic! ๐Ÿง™โ€โ™‚๏ธ

๐Ÿ” Part 1: File and Directory Exploration ๐Ÿ‘ฃ

Upon running the explorer.sh script without any command-line arguments, it will display a warm welcome message and list all the files and directories in the current path. ๐Ÿ“‚ For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). We'll accomplish this by using the ls command with appropriate options. Let's see how it's done! ๐Ÿ•ต๏ธโ€โ™€๏ธ

๐Ÿ“ explorer.sh:

#!/bin/bash

# Part 1: File and Directory Exploration

# Welcome message
echo "Welcome to the Interactive File and Directory Explorer! ๐ŸŒŸ"

# Infinite loop until the user decides to exit
while true; do
    # Display files and directories in the current path using ls command with human-readable sizes
    echo "Files and Directories in the Current Path:"
    ls -lh

    # Ask the user to enter a line of text
    echo -n "Enter a line of text (Press Enter without text to exit): "

    # Read user input into the variable 'input'
    read input

    # Check if the user entered an empty string (i.e., pressed Enter without any text)
    if [[ -z "$input" ]]; then
        echo "Exiting the Interactive Explorer. Goodbye! ๐Ÿ‘‹"
        break   # Exit the loop if the user entered an empty string
    else
        # Count the number of characters in the input and display the count
        char_count=${#input}
        echo "Character Count: $char_count"
    fi
done

# End of script

๐Ÿ” Part 2: Character Counting ๐Ÿงฎ

After displaying the file and directory list, the script will prompt you to enter a line of text. ๐Ÿ“ The script will keep reading your input until you provide an empty string (i.e., press Enter without any text). For each line of text entered, the script will count the number of characters and show you the character count. It's an excellent way to play with the script and see how many characters are in your input! ๐ŸŽ‰

๐Ÿ” 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 powerful and interactive script that allows you to explore files and directories in your current path, along with a handy character-counting feature. ๐Ÿš€ I hope you had fun building this script and learning more about Bash scripting. Stay tuned for more exciting challenges in the upcoming days! Happy scripting and exploring! ๐ŸŽˆ๐ŸŒŒ

๐Ÿ” Checkout GitHub Repository for projects:

๐Ÿ”— github.com/sumanprasad007

๐Ÿ” Check out my YouTube channel - Prasad Suman Mohan:

๐Ÿ”— https://www.youtube.com/@sumanprasad007

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย