how to read a files and folders from MinIO

neupane parlad
2 min readAug 28, 2024

here is an example,

#!/bin/bash

# Check if a parameter is provided
if [ -z "$1" ]; then
echo "Usage: $0 <bucket/folder-path>"
exit 1
fi

# Define the alias and full path from the input parameter
ALIAS="your-alias"
FULL_PATH="$1" # The input parameter in the format "bucket-name/folder/"

# List all subfolders inside the given path
folders=$(mc ls "$ALIAS/$FULL_PATH" --json | jq -r 'select(.type == "folder") | .key')

# Initialize total counters
total_files=0
total_size=0
folder_count=0

# Check if there are any folders
if [ -z "$folders" ]; then
# No subfolders, count files in the main directory
file_details=$(mc ls --recursive --json "$ALIAS/$FULL_PATH")

# Loop through files
for line in $(echo "$file_details" | jq -c '. | {size: .size, key: .key}'); do
file_size=$(echo "$line" | jq '.size')
total_size=$((total_size + file_size))
total_files=$((total_files + 1))
done

# Convert size to a human-readable format using `numfmt`
readable_size=$(numfmt --to=iec-i --suffix=B "$total_size")

# Print results for the main folder with no subfolders
echo "Folder Path: $ALIAS/$FULL_PATH"
echo "Total Folders: 0"
echo "Total Files: $total_files"
echo "Total Size: $readable_size"
else
# There are subfolders, iterate through each
for folder in $folders; do
# Reset counters for each subfolder
subfolder_files=0
subfolder_size=0
folder_count=$((folder_count + 1))

# List files inside the current subfolder recursively
file_details=$(mc ls --recursive --json "$ALIAS/$FULL_PATH$folder")

# Loop through files
for line in $(echo "$file_details" | jq -c '. | {size: .size, key: .key}'); do
file_size=$(echo "$line" | jq '.size')
subfolder_size=$((subfolder_size + file_size))
subfolder_files=$((subfolder_files + 1))
done

# Convert size to a human-readable format using `numfmt`
readable_size=$(numfmt --to=iec-i --suffix=B "$subfolder_size")

# Print results for each subfolder
echo "Folder Path: $ALIAS/$FULL_PATH$folder"
echo "Total Files: $subfolder_files"
echo "Total Size: $readable_size"
echo "--------------------------"
done
fi

# Print summary if subfolders exist
if [ $folder_count -gt 0 ]; then
echo "Total Folders: $folder_count"
fi

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

neupane parlad
neupane parlad

Written by neupane parlad

Follow me for Data Science, Data Engineering and Data Analysis Articles. Follow me on twitter at @parladN

No responses yet

Write a response