如何在Bash脚本中访问目标目录并压缩其中文件?
How to Compress a Target Directory in Shell Scripts
Hey there! Nice job getting the current directory compression working—you’re already halfway there! Let’s break down how to adapt your script to handle a specified target directory, with practical examples and beginner-friendly tips to make it robust.
Basic Target Directory Compression
If you’re using tar (the go-to tool for this on Unix-like systems), here’s a simple script that takes a target directory path as input and compresses it properly:
#!/bin/bash # Prompt for the target directory path read -p "Enter the target directory path to compress: " target_dir # First, check if the directory actually exists—avoids messy errors! if [ -d "$target_dir" ]; then # Create a timestamped archive (so you don't overwrite old files) archive_name="${target_dir##*/}_$(date +%Y%m%d_%H%M%S).tar.gz" tar -czf "$archive_name" "$target_dir" echo "Success! Compressed $target_dir into $archive_name" else echo "Oops: Directory $target_dir doesn't exist. Double-check the path!" exit 1 fi
Let’s break down the key parts:
[ -d "$target_dir" ]: This checks if the path you entered is a valid directory—critical for preventing the script from failing silently if you mistype a path.tar -czf <output-file> <directory>:-c: Creates a new archive-z: Uses gzip compression (swap with-jif you prefer .bz2 format)-f: Specifies the output file name
${target_dir##*/}: Extracts just the directory name from the full path (e.g., turns/home/user/photosintophotos)$(date +%Y%m%d_%H%M%S): Adds a timestamp to the archive name so you never overwrite older backups.
Alternative: Accept Target Directory as a Command-Line Argument
If you want to run the script directly with a path (like ./compress.sh /path/to/my/folder), use this version instead:
#!/bin/bash # Check if the user provided a directory argument if [ $# -ne 1 ]; then echo "Usage: $0 <target-directory-path>" exit 1 fi target_dir="$1" if [ -d "$target_dir" ]; then archive_name="${target_dir##*/}_$(date +%Y%m%d_%H%M%S).tar.gz" tar -czf "$archive_name" "$target_dir" echo "Successfully compressed $target_dir!" else echo "Error: $target_dir is not a valid directory." exit 1 fi
Bonus: Combine Current + Target Directory Compression
Since you already have the current directory part working, you can merge both into one script:
#!/bin/bash # Compress current directory first current_archive="current_dir_$(date +%Y%m%d_%H%M%S).tar.gz" tar -czf "$current_archive" . echo "Compressed current directory into $current_archive" # Now handle the target directory read -p "Enter target directory to compress: " target_dir if [ -d "$target_dir" ]; then target_archive="${target_dir##*/}_$(date +%Y%m%d_%H%M%S).tar.gz" tar -czf "$target_archive" "$target_dir" echo "Compressed $target_dir into $target_archive" else echo "Skipping target directory compression: $target_dir doesn't exist." fi
Quick Beginner Tips:
- Always quote variables like
"$target_dir"—this handles paths with spaces in them (super important if your directory names have spaces!) - Test the
tarcommand manually in your terminal first before adding it to a script (e.g.,tar -czf test.tar.gz /path/to/dir) - To exclude specific files/directories, add
--exclude='pattern'to thetarcommand (e.g.,tar -czf archive.tar.gz --exclude='*.log' /path/to/dir)
内容的提问来源于stack exchange,提问作者Usman Malik
相关产品推荐
相关产品推荐

