I want to compress and archive (e. g. zip or tar.gz) a lot of files which are all stored in /folder. It should create archive files like yyyy-mm-backup.zip including all original files which were modified in that time range.
Sample files:
fileA.txt (modified date 2020-12-01)
fileB.txt (modified date 2020-12-02)
fileC.txt (modified date 2021-01-01)
Should create archive files:
2020-12-backup.zip (include fileA.txt and fileB.txt)
2021-01-backup.zip (include fileC.txt)
Is this possible with a single zip or tar command? Or do I need a script or using logrotate?
Redirect the output of a find command into a while loop
while read line;
do
dattim=${line%%/*};
filpath=${line#*/};
echo "zip -r $dattim-backup.zip $filpath";
# zip -r "$dattim-backup.zip" "$filpath"
done <<< "$(find /path/to/directory -name "*.txt" -printf "%TY-%Tm/%h/%f\n")"
Find all files with the extension txt and print the date of modification in the format outlined along with a "/", the leading directories, "/" and the file name. Process the output through a while loop. Extract the date/time stamp into the variable dattim and the file path into the variable filpath. Use these variables to generate the commands to add the files to the dated zip files.
Verify that the echo displays the commands as expected and then remove the comment flag to execute the actual zip command.