#!/bin/bash # Check if an argument is provided if [ $# -eq 0 ]; then # No argument provided, so search for the oldest file in the current directory file_to_delete=$(ls -t | tail -1) path_to_file="./$file_to_delete" else # Argument provided, use it as the pathname path_to_file="$1" # Check if the provided pathname exists and is a directory if [ ! -d "$path_to_file" ]; then echo "The provided pathname is not a directory." exit 1 fi # Search for the oldest file in the specified directory file_to_delete=$(find "$path_to_file" -type f -printf "%T+ %p\n" | sort | head -1 | awk '{print $2}') fi # Confirm with the user before deleting the file read -p "Delete the oldest file: $file_to_delete? (y/n): " user_input if [ "$user_input" = "y" ]; then # Delete the file rm "$file_to_delete" echo "File $file_to_delete has been deleted." else echo "File deletion cancelled." fi