diff --git a/miscellaneous/archive_files.sh b/miscellaneous/archive_files.sh new file mode 100644 index 0000000..5e56094 --- /dev/null +++ b/miscellaneous/archive_files.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# This script archives each file and directory in the current directory into its respective 7z archive. +# It ensures that the created archives are not included in the loop to avoid an infinite loop. +# It also checks if 7z is installed and installs it if necessary. + +# Usage: +# 1. Save this script to a file, e.g., archive_files.sh. +# 2. Make the script executable: chmod +x archive_files.sh. +# 3. Run the script in the directory containing the files and directories to be archived: ./archive_files.sh. + +# Check if 7z is installed +if ! command -v 7z &> /dev/null; then + echo "7z is not installed. Installing 7z..." + sudo apt-get update + sudo apt-get install -y p7zip-full +fi + +# Check if the current directory contains any files or directories +if [ -z "$(ls -A .)" ]; then + echo "No files or directories to archive in the current directory." + exit 1 +fi + +# Loop through each item in the current directory +for item in *; do + # Check if the item is not a 7z archive + if [[ ! "$item" =~ \.7z$ ]]; then + # Create a 7z archive for the file or directory + 7z a "${item}.7z" "$item" + echo "Archive created for $item: ${item}.7z" + fi +done + +echo "All files and directories have been archived." \ No newline at end of file