Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer.

Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer.

Welcome to Day 2 of the Bash Scripting Challenge! In this challenge, you will create a bash script that serves as an interactive file and directory explorer. The script will allow you to explore the files and directories in the current path and provide a character counting feature for the user's input.

Part 1: File and Directory Exploration:

  1. Upon execution without any command-line arguments, the script will display a welcome message and list all the files and directories in the current path.
  2. For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). This information will be obtained using the ls command with appropriate options.

Below is a simple and clean shell script that meets your requirement:

mpsaps@Pradeep-PC:~/shell_script$ cat file_explorer.sh
heck if any command-line arguments were passed
if [ $# -eq 0 ]; then
echo "👋 Welcome to the Interactive File Explorer!"
echo "📂 Listing all files and directories in: $(pwd)"
echo
# List all files and directories with size in human-readable format
for item in *; do
if [ -d "$item" ]; then
type="Directory"
elif [ -f "$item" ]; then
  type="File"
else
  type="Other"
fi

size=$(du -sh "$item" 2>/dev/null | cut -f1)
printf "%-20s %-10s %s\n" "$item" "$type" "$size"
done
 else
    echo "❌ This script does not accept arguments."
    echo "Usage: ./file_explorer.sh"
    exit 1
fi


mpsaps@Pradeep-PC:~/shell_script$        

💡 How to Use

1 Save the script as file_explorer.sh

2. Make it executable:

chmod +x file_explorer.sh        

3. Run it without any arguments:

./file_explorer.sh        

📌 Output Example

mpsaps@Pradeep-PC:~/shell_script$ ./file_explorer.sh
./file_explorer.sh: line 1: heck: command not found
👋 Welcome to the Interactive File Explorer!
📂 Listing all files and directories in: /home/mpsaps/shell_script

day_0.sh             File       4.0K
day_01.sh            File       4.0K
day_02.sh            File       4.0K
day_03.sh            File       4.0K
file.txt             File       0
file_explorer.sh     File       4.0K
shell                Directory  8.0K
test.sh              File       4.0K
test1.sh             File       4.0K        

3.The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

#!/bin/bash

# Welcome message
echo "👋 Welcome to the Interactive File Explorer!"
echo "📂 You are currently in: $(pwd)"

while true; do
    echo
    echo "🔍 Listing files and directories..."
    echo "--------------------------------------------"
    
    # List files/directories with type and size
    for item in *; do
        if [ -d "$item" ]; then
            type="Directory"
        elif [ -f "$item" ]; then
            type="File"
        else
            type="Other"
        fi

        size=$(du -sh "$item" 2>/dev/null | cut -f1)
        printf "%-25s %-12s %s\n" "$item" "$type" "$size"
    done

    echo "--------------------------------------------"
    echo
    # Prompt user for next action
    read -p "🔁 Press [Enter] to refresh or type 'exit' to quit: " input

    if [ -z "$input" ]; then
        clear
        echo "🔄 Refreshing file list..."
    elif [ "$input" == "exit" ]; then
        echo "👋 Exiting the Interactive Explorer. Goodbye!"
        break
    else
        echo "❗ Invalid input. Please press [Enter] or type 'exit'."
    fi
done        

1.Save as file_explorer.sh

2. Make executable:

chmod +x file_explorer1.sh        

3. run it

./file_explorer.sh
then getting output
mpsaps@Pradeep-PC:~/shell_script$ ./file_explorer1.sh
./file_explorer1.sh: line 1: sage: command not found
👋 Welcome to the Interactive File Explorer!
📂 You are currently in: /home/mpsaps/shell_script

🔍 Listing files and directories...
--------------------------------------------
day_0.sh                  File         4.0K
day_01.sh                 File         4.0K
day_02.sh                 File         4.0K
day_03.sh                 File         4.0K
file.txt                  File         0
file_explorer.sh          File         4.0K
file_explorer1.sh         File         4.0K
shell                     Directory    8.0K
test.sh                   File         4.0K
test1.sh                  File         4.0K
--------------------------------------------

🔁 Press [Enter] to refresh or type 'exit' to quit:
when not exit still then write exit otherwise getting the something
🔁 Press [Enter] to refresh or type 'exit' to quit: quit
❗ Invalid input. Please press [Enter] or type 'exit'.

🔍 Listing files and directories...
--------------------------------------------
day_0.sh                  File         4.0K
day_01.sh                 File         4.0K
day_02.sh                 File         4.0K
day_03.sh                 File         4.0K
file.txt                  File         0
file_explorer.sh          File         4.0K
file_explorer1.sh         File         4.0K
shell                     Directory    8.0K
test.sh                   File         4.0K
test1.sh                  File         4.0K
--------------------------------------------

🔁 Press [Enter] to refresh or type 'exit' to quit:
then write the exit then getting the output
🔁 Press [Enter] to refresh or type 'exit' to quit: quit
❗ Invalid input. Please press [Enter] or type 'exit'.

🔍 Listing files and directories...
--------------------------------------------
day_0.sh                  File         4.0K
day_01.sh                 File         4.0K
day_02.sh                 File         4.0K
day_03.sh                 File         4.0K
file.txt                  File         0
file_explorer.sh          File         4.0K
file_explorer1.sh         File         4.0K
shell                     Directory    8.0K
test.sh                   File         4.0K
test1.sh                  File         4.0K
--------------------------------------------

🔁 Press [Enter] to refresh or type 'exit' to quit: exit
👋 Exiting the Interactive Explorer. Goodbye!        


I think this is great, especially since it's only the second day. Well done, buddy.

Like
Reply

To view or add a comment, sign in

Explore content categories