Linux脚本开发:读取路径行从Zip复制文件至目标路径
新手友好的Linux文件复制脚本(从Zip包提取)
Hey there! Let's put together a simple, robust script that does exactly what you need—whether you're processing a list of files from an input text or copying a single file on the fly. I'll walk through each part so you understand what's going on, no confusing jargon included.
Step 1: The Complete Script
First, save this as copy_from_zip.sh (feel free to pick a name that makes sense to you):
#!/bin/bash # -------------------------- # CONFIGURE THIS FIRST! # Replace with your actual zip file's full path ZIP_FILE="/full/path/to/your/target_files.zip" # -------------------------- # Handle different input scenarios if [ $# -eq 1 ]; then # Case 1: Process all lines from an input text file INPUT_FILE="$1" # Check if the input file exists before proceeding if [ ! -f "$INPUT_FILE" ]; then echo "❌ Error: Input file '$INPUT_FILE' doesn't exist!" exit 1 fi # Read each line of the input file while read -r src_dst_line; do # Skip empty lines to avoid errors [ -z "$src_dst_line" ] && continue # Split the line into source path (from inside the zip) and destination path # This works even if the destination path has spaces! src_path=$(echo "$src_dst_line" | awk '{print $1}') dst_path=$(echo "$src_dst_line" | awk '{for(i=2; i<=NF; i++) printf "%s ", $i; print ""}' | xargs) # Create the destination directory if it doesn't exist mkdir -p "$(dirname "$dst_path")" # Extract the file from the zip and write it to the destination if unzip -p "$ZIP_FILE" "$src_path" > "$dst_path"; then echo "✅ Copied '$src_path' to '$dst_path'" else echo "❌ Failed to copy '$src_path' to '$dst_path'" fi done < "$INPUT_FILE" elif [ $# -eq 2 ]; then # Case 2: Copy a single file directly via command line arguments src_path="$1" dst_path="$2" # Create destination directory if needed mkdir -p "$(dirname "$dst_path")" # Extract and copy the file if unzip -p "$ZIP_FILE" "$src_path" > "$dst_path"; then echo "✅ Copied '$src_path' to '$dst_path'" else echo "❌ Failed to copy '$src_path' to '$dst_path'" fi else # Show usage instructions if the wrong number of arguments is passed echo "Usage:" echo " $ ./copy_from_zip.sh <input_file.txt> # Process all lines in the input file" echo " $ ./copy_from_zip.sh <zip_internal_path> <destination_path> # Copy a single file" exit 1 fi
Step 2: Make the Script Executable
Run this command in your terminal to give the script permission to run:
chmod +x copy_from_zip.sh
Step 3: How to Use It
Option 1: Process an Input File
If your input file (e.g., file_list.txt) looks like this:
docs/report.pdf /home/user/documents/my_report.pdf images/photo.jpg /home/user/pictures/vacation photo.jpg data/metrics.csv /home/user/analytics/monthly_data.csv
Run the script like this:
./copy_from_zip.sh file_list.txt
Option 2: Copy a Single File Directly
Just pass the path inside the zip and your destination path (wrap paths with spaces in quotes):
./copy_from_zip.sh "docs/report.pdf" "/home/user/documents/my_report.pdf"
Key Tips for Newbies
- Update the Zip Path: Don't forget to change the
ZIP_FILEvariable at the top to point to your actual zip file (use the full path to avoid "file not found" errors!). - Paths with Spaces: If your file paths have spaces, wrap them in double quotes in your input file or when passing them as arguments—the script is set up to handle this.
- Automatic Directory Creation: The script will make any missing folders for your destination path, so you don't have to run
mkdirmanually. - Error Feedback: You'll get clear success/failure messages for each file, so you know exactly what worked and what didn't.
内容的提问来源于stack exchange,提问作者obidyne
相关产品推荐
相关产品推荐

