Files
scripts-admin-debian/miscellaneous/archive_files.sh
2024-12-26 17:51:01 +01:00

35 lines
1.2 KiB
Bash

#!/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."