unzip-rename-dir.sh
· 1015 B · Bash
Orginalformat
#!/bin/bash
# Loop through all ZIP files in the current directory
for zipfile in *.zip; do
# Check if the file exists (in case no .zip files are found)
[ -e "$zipfile" ] || continue
# Get the filename without the .zip extension
dirname="${zipfile%.zip}"
# Create a temporary directory for extraction
temp_dir=$(mktemp -d)
# Extract the ZIP file to the temporary directory
unzip -q "$zipfile" -d "$temp_dir"
# If the extraction was successful, rename the directory
if [ $? -eq 0 ]; then
# Remove existing directory if it exists
[ -d "$dirname" ] && rm -rf "$dirname"
# Rename the temporary directory to the ZIP filename (without .zip)
mv "$temp_dir" "$dirname"
echo "Extracted and renamed: $zipfile -> $dirname"
else
echo "Failed to extract: $zipfile"
# Clean up the temporary directory in case of failure
rm -rf "$temp_dir"
fi
done
echo "Extraction and renaming complete!"
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Loop through all ZIP files in the current directory |
| 4 | for zipfile in *.zip; do |
| 5 | # Check if the file exists (in case no .zip files are found) |
| 6 | [ -e "$zipfile" ] || continue |
| 7 | |
| 8 | # Get the filename without the .zip extension |
| 9 | dirname="${zipfile%.zip}" |
| 10 | |
| 11 | # Create a temporary directory for extraction |
| 12 | temp_dir=$(mktemp -d) |
| 13 | |
| 14 | # Extract the ZIP file to the temporary directory |
| 15 | unzip -q "$zipfile" -d "$temp_dir" |
| 16 | |
| 17 | # If the extraction was successful, rename the directory |
| 18 | if [ $? -eq 0 ]; then |
| 19 | # Remove existing directory if it exists |
| 20 | [ -d "$dirname" ] && rm -rf "$dirname" |
| 21 | |
| 22 | # Rename the temporary directory to the ZIP filename (without .zip) |
| 23 | mv "$temp_dir" "$dirname" |
| 24 | echo "Extracted and renamed: $zipfile -> $dirname" |
| 25 | else |
| 26 | echo "Failed to extract: $zipfile" |
| 27 | # Clean up the temporary directory in case of failure |
| 28 | rm -rf "$temp_dir" |
| 29 | fi |
| 30 | done |
| 31 | |
| 32 | echo "Extraction and renaming complete!" |