Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer
Table of contents
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.
3. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.
### Part 2: Character Counting
1. After displaying the file and directory list, the script will prompt the user to enter a line of text.
2. The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
3. For each line of text entered by the user, the script will count the number of characters in that line.
4. The character count for each line entered by the user will be displayed.
Solution:
#!/bin/bash
# Part 1: File and 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
# Part 2: Character Counting
read -p "Enter a line of text (Press Enter without text to exit): " input
# Exit if the user enters an empty string
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
ANOTHER CHALLENGE
# Directory Backup with Rotation
This is another challenge for Day 2 of the Bash Scripting Challenge! In this challenge, you will create a bash script that performs a backup of a specified directory and implements a rotation mechanism to manage backups.
## Challenge Description
Your task is to create a bash script that takes a directory path as a command-line argument and performs a backup of the directory. The script should create timestamped backup folders and copy all the files from the specified directory into the backup folder.
Additionally, the script should implement a rotation mechanism to keep only the last 3 backups. This means that if there are more than 3 backup folders, the oldest backup folders should be removed to ensure only the most recent backups are retained
The script will create a timestamped backup folder inside the specified directory and copy all the files into it. It will also check for existing backup folders and remove the oldest backups to keep only the last 3 backups.
Solution:
#!/bin/bash
# Function to display usage information and available options
function display_usage {
echo "Usage: $0 /path/to/source_directory"
}
# Check if a valid directory path is provided as a command-line argument
if [ $# -eq 0 ] || [ ! -d "$1" ]; then
echo "Error: Please provide a valid directory path as a command-line argument."
display_usage
exit 1
fi
# Directory path of the source directory to be backed up
source_dir="$1"
# Function to create a timestamped backup folder
function create_backup {
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S') # Get the current timestamp
local backup_dir="${source_dir}/backup_${timestamp}"
# Create the backup folder with the timestamped name
mkdir "$backup_dir"
echo "Backup created successfully: $backup_dir"
}
# Function to perform the rotation and keep only the last 3 backups
function perform_rotation {
local backups=($(ls -t "${source_dir}/backup_"* 2>/dev/null)) # List existing backups sorted by timestamp
# Check if there are more than 3 backups
if [ "${#backups[@]}" -gt 3 ]; then
local backups_to_remove="${backups[@]:3}" # Get backups beyond the last 3
rm -rf "${backups_to_remove[@]}" # Remove the oldest backups
fi
}
# Main script logic
create_backup
perform_rotation