DEV Community

Cover image for BashBlaze Day 2: Building an Interactive File and Directory Explorer (Part-1)
Muhammad Hanzala Ali
Muhammad Hanzala Ali

Posted on

BashBlaze Day 2: Building an Interactive File and Directory Explorer (Part-1)

Introduction

*Welcome to Day 2 of the BashBlaze - 7 Days of Bash Scripting Challenge! *
Today, we’re stepping up our bash scripting skills by creating an Interactive File and Directory Explorer that not only lists files and directories in the current path but also provides a character counting feature for any text entered by the user.

This challenge is a hands-on approach to working with loops, command-line interaction, and command chaining in bash. Let’s dive into the tasks and explore how each part of this interactive script works!

Challenge Breakdown

Our script has two main functions:

Part 1: File and Directory Exploration
Part 2: Character Counting
Let’s go over each of these parts in detail with code examples.

Part 1: File and Directory Exploration

In the first part of this challenge, we’re building an interactive explorer that displays all files and directories in the current path in a human-readable format.

Here's the solution for Part 1:

#!/bin/bash

# Part1: Files & Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"

while true; do
    # List all files and directories in the current path
    echo "Files and Directories in the Current Path:"
    ls -lh

Enter fullscreen mode Exit fullscreen mode

The ls -lh command lists all files and directories in the current path. The -l flag shows detailed information, and -h makes sizes human-readable (KB, MB, etc.).

Part 2: Character Counting

After displaying files and directories, the script moves to character counting for user input. It prompts the user to enter a line of text and counts the characters in each line until the user presses Enter without entering text, which exits the script.

Here’s the solution for Part 2:

  # Part2: Character Counting
    read -p "Enter a line of text (Press Enter without text to exit): " input

    # Exit if the input is empty
    if [[ -z "$input" ]]; then
          echo "Exiting the Interactive Explorer. Goodbye!"
          break
    fi

    # Calculate and print the character count for the input line
    char_count=$(echo -n "$input" | wc -m)
    echo "Character count: $char_count"
done

Enter fullscreen mode Exit fullscreen mode

Explanation:

read -p prompts the user for input.
if [[ -z "$input" ]]; then ... checks if the input is empty and breaks the loop if so.
echo -n "$input" | wc -m counts the characters in the input without counting the newline.

Full Solution

Here’s the complete solution for Day 2:

#!/bin/bash

# Part1: Files & Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"

while true; do
    # List all files and directories in the current path
    echo "Files and Directories in the Current Path:"
    ls -lh

    # Part2: Character Counting
    read -p "Enter a line of text (Press Enter without text to exit): " input

    # Exits when the input is empty
    if [[ -z "$input" ]]; then
          echo "Exiting the Interactive Explorer. Goodbye!"
          break
    fi

    # Calculate and print the character count for the input line
    char_count=$(echo -n "$input" | wc -m)
    echo "Character count: $char_count"
done

Enter fullscreen mode Exit fullscreen mode

Example Interaction:

$ ./explorer.sh
Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:
- file1.txt (100 KB)
- dir1 (2 MB)
- script.sh (3 KB)

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.
Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.
Character Count: 25

Enter a line of text (Press Enter without text to exit):
Exiting the Interactive Explorer. Goodbye!

Enter fullscreen mode Exit fullscreen mode

Conclusion

Today’s challenge was all about building an interactive experience using bash scripting. We learned how to:

  • Use loops to maintain an interactive session.
  • Display files and directories with human-readable sizes.
  • Capture and count characters in user input. This exercise has shown the power of bash scripting for real-world tasks. Stay tuned for Day 3, where we’ll explore even more advanced topics in bash scripting.

Top comments (0)