I need to make a split zip packages for a folder and add a md5 check log into this archive then remove the original md5 file. I have below code but run in error. You need to correct it:
define the zip file name with the timestamp and timezone
zip_filename="${base_name}.zip"
Add MD5 checksum log to zip file
zip -m "zipfilename"md5_file
Create the zip file with a split size
zip -u -r -s splizip_filename" "$target_folder"
Assistant
Your script seems to have a good starting point, but there are a couple of issues that you may need to address:
The -m option in the zip command moves files into the zip archive. This means that your md5_file will be removed after being added to the zip file, which may be what you want, but might be unclear. You should add the MD5 log into the zip before you create the split zips.
User
zip warning: cannot update a split archive
Assistant
The warning "cannot update a split archive" appears because you cannot use the -u (update) mode on a split zip archive directly. Instead, you need to first create a standard zip file without splitting it, and then create the split zip files afterward.
Here's how you can modify your script to handle this correctly:
First, create a single zip file without splitting.
Then, split that zip file into smaller parts into the desired size.
User
how to use ls command to only show filename date and size?
Assistant
To use the ls command to only show the filename, date, and size, you can format the output using the -l (long listing) option along with the --time-style option, or by using a combination of other options. Here are a few ways to achieve this:
The first zip command should create the base zip file, and then the second zip command can split it.
Ensure that you're using variables correctly. You'll need to declare your variables (base_name, md5_file, target_folder, and split_size) appropriately.
The second zip command should reference the created zip file correctly.
Here's a corrected version of your script:
# Define variables
base_name="your_base_name" # Replace with your base name
md5_file="path/to/md5file.md5" # Replace with the path to your md5 file
target_folder="path/to/your_folder" # Replace with path to your target folder
split_size="1g" # Replace with desired split size (example: 100m for 100 MB)
# Define the zip file name with the timestamp and timezone
zip_filename="${base_name}.zip"
# Create the zip file and add the MD5 checksum log
zip "$zip_filename" "$md5_file" -r "$target_folder"
# Create the zip file with a split size
zip -s $split_size -u "$zip_filename"
# Remove the original MD5 file
rm "$md5_file"
# Complete zip
echo "Zip file created: $zip_filename"
echo "$(ls -ltr ${base_name}.z*)"
Explanation of Changes:
Variable Setup: Make sure to set your variables for base_name, md5_file, target_folder, and split_size.
Single Zip Command: The initial zip command now combines both actions, zipping the MD5 file and target folder within a single command.
Removing MD5 File: The MD5 file is deleted after being added to the zip file.
Splitting the Zip File: The split command uses the same created zip file to handle splitting.
Make sure to verify paths and values for your specific context, and adjust the script accordingly.
Below is the modified version of your script:
# Define variables
base_name="your_base_name" # Replace with your base name
md5_file="path/to/md5file.md5" # Replace with the path to your md5 file
target_folder="path/to/your_folder" # Replace with the path to your target folder
split_size="1g" # Replace with desired split size (e.g., 100m for 100 MB)
# Define the zip file name
zip_filename="${base_name}.zip"
# Create the zip file and add the MD5 checksum log
zip "$zip_filename" "$md5_file" -r "$target_folder"
# Split the zip file into smaller parts
zip -s $split_size --out "${base_name}_split.zip" "$zip_filename"
# Remove the original MD5 file
rm "$md5_file"
# Remove the original, un-split zip file if needed
rm "$zip_filename"
# Complete zip
echo "Split zip files created:"
echo "$(ls -ltr ${base_name}_split.z*)"
Explanation of Changes:
Create Standard Zip File: The first zip command creates a single zip file that includes both the MD5 file and the contents of the target folder.
Splitting the Zip File: The -s option is used with --out to specify the new naming pattern for the split zip files. It creates files with _split.z01, _split.z02, etc., based on the base name you provide.
Optional Cleanup: The script now removes the original MD5 file and the un-split zip file afterward if you don't need them.
Make sure you replace the placeholder paths and values with those that are applicable to your environment. This script should now function without generating the warning you encountered.
ls -lh --time-style
You can use the -l option for a long listing, -h for human-readable sizes, and customize the time format:
ls -lh --time-style=long-iso
This command will display:
File permissions
Number of links
Owner name
Owner group
Size of the file (in human-readable format)
Modification date (in YYYY-MM-DD HH:MM)
Filename
Method 2: Using ls -l with awk
If you only want the filename, date, and size, you can use awk to filter the output from ls -l:
$6, $7, $8: Month, day, and time (or year if applicable)
$5: Size of the file
Method 3: Using stat
You can use the stat command for more tailored output as well. Here’s how to do that:
for file in *; do stat --format='%n %y %s' "$file"; done
This command iterates over all files in the current directory and uses stat to display:
%n: Filename
%y: Last modification date and time
%s: Size in bytes
You can also modify %y if you want a different format. For example, to remove the time from the output:
for file in *; do stat --format='%n %y %s' "$file" | cut -d ' ' -f1-3,5; done
Method 4: Using find
If you want a more concise format, you can also use find:
find . -maxdepth 1 -printf '%f %TY-%Tm-%Td %k\n'
This command uses:
-maxdepth 1: Restricts the search to the current directory.
-printf to format output.
%f: Filename
%TY, %Tm, %Td: Year, month, and day of last modification
%k: Size in kilobytes (for other size units, you may need to adjust your command).
Summary
Choose the method that fits your preferences and requirements. The ls command combined with awk is versatile, while stat provides detailed and customizable file information.