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.
Challenge Description
The script will have two main parts:
Part 1: File and Directory Exploration
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.
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.The list of files and directories will be displayed in a loop until the user decides to exit the explorer.
Part 2: Character Counting
After displaying the file and directory list, the script will prompt the user to enter a line of text.
The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
For each line of text entered by the user, the script will count the number of characters in that line.
The character count for each line entered by the user will be displayed.
###################################################################
# Author: Sasiram Beeke
# Date: 01/08/2023
# Description: Interactive File and Directory Explorer
# Tip: to execute shell give permision +x scriptname
###################################################################
#!/bin/bash
echo "Welcome to the Interactive File and Directory Explorer!"
echo "Files and Directories in the Current Path:"
ls -lh| awk '{print $9, "(" $5 ")"}'
while true; do
# Ask the user if they want to exit the explorer
read -p "Enter a line of text (press Enter without text to exit):" input
# Check the user's choice
if [[ -z "$input" ]];then
break
fi
echo "Character count:$(echo -n "$input" | wc -m)"
done
echo "Exiting the Interactive Explorer. Goodbye!"
Output :
Directory Backup with Rotation
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.
###################################################################
# Author: Sasiram Beeke
# Date: 01/08/2023
# Description: Directory Backup with Rotation
# Tip: to execute shell give permision +x scriptname
###################################################################
#!/bin/bash
# accept argument and assing it to source_dir variable
source_dir=$1
# create timestamp and store it to timestamp variable
timestamp=$(date +%Y-%m-%d-%H-%M-%S)
# Create the backup folder name using the timestamp
dir_name="backup_${timestamp}"
backup_folder="${source_dir}/${dir_name}"
mkdir "${backup_folder}"
#The script uses rsync to copy the contents of the source directory to the backup folder.
#The rsync command ensures that the backup folder itself is excluded from the copy.
rsync -av --exclude="${dir_name}" "$source_dir/" "$backup_folder"
# Find and remove older backups, retaining only the last 3 backups
find "$source_dir" -maxdepth 1 -type d -name "backup_*" | sort | head -n -3 | xargs rm -rf
# Print a success message
echo "Backup created: $backup_folder"
Output :
Inside the day2 directory I have created 3 folders test,test1,test3 .inside all directories .txt files are created. At the time of shell script execution, I am passing the test directory as an argument where my backup folder gets created. after the execution of a script, I have listed the test directory to show the backup folder.
After that, I created 3 backup folders, which are shown below
./backup_with_rotation.sh test
after executing the script 4th-time Old backup folder gets deleted and the latest one is added to the directory.