Create ZIP of files based on date, Linux
Solution 1
Do you want the latest files or the files that are older than 2 days? Either way you can use the same basic command:
zip -r /destination_path/media_backup.zip $(find /from_path/media -type f -mtime +2)
You just need to adjust the mtime
option according to your requirements. +X
means "last modified more than X days ago", -X
would be "last modified less than X days ago". If you need more resolution, you could use -mmin
instead, which checks against minutes rather than days.
Solution 2
Try this.
zip -rtt 2014-12-13 /destination_path/media_backup.zip /from_path/media
You can find more options using the zip man page.
-tt Do not operate on files modified after or at the specified date
Solution 3
this should work, M not sure and I had not tried.
find / -mtime +2 -ls > input.txt
cat input.txt | zip myzipfile.zip -@
user984003
Updated on June 13, 2022Comments
-
user984003 almost 2 years
I am creating a zip file of some files (image files), but need to limit it such that only the latest files are added to the zip file. I need files that are more than 2 days old. Exact time is not relevant.
This is what I have been doing, but how do I limit it based on date? This is Linux and is run from a batch .sh file.
zip -r /destination_path/media_backup.zip /from_path/media
-
Paul Ostrowski over 2 yearsOne problem I had using this method was when some of the files I wanted to zip up contained a space in their name. The zip process was treating them as separate files, and this failing. Using the method bhargav Modi suggested at the end solved this for me.
-
Paul Ostrowski over 2 yearsThis is close, but you should remove "-ls" from your find operation to make it work right, and only search for file types: find . -type f -mtime +2 > input.txt
-
Ranjit Chawla over 2 yearsIf you need to operate only on files modified after the specified date, just use -t instead of -tt
-
benjaminhull about 2 yearsPlease don't leave untested command line answers. The risk of causing chaos is high.