Want to Zip files based on modified date
10,595
Solution 1
find . -maxdepth 1 -mtime +15 -type f -printf "%TY%Tm%Td %p\n" | while read date name ; do zip $date $name; done
File's last modification time in Ymd format
To do it for all underneath directories, there are different ways to do it, giving below a few, make sure to use absolute path with find, for example I use "/home/user"
find /home/user -type d -print0 | while read -d '' -r dir; do cd "$dir" && pwd && find . -maxdepth 1 -mtime +15 -type f -printf "%TY%Tm%Td %p\n" | while read date name ; do zip $date $name; done; done
or
find /home/user -type d -print0 | xargs -0 -I {} sh -c 'cd '\"{}\"' && pwd && find . -maxdepth 1 -mtime +15 -type f -printf "%TY%Tm%Td %p\n" | while read date name ; do zip $date $name; done'
Solution 2
You can try the following:
while read zipfile files; do
zip ${zipfile}.zip $files
done <<< $(find -maxdepth 1 -type f | xargs stat -c "%y,%n" | awk -F, '{a[substr($1,1,10)]=a[substr($1,1,10)] " " $2} END{for(i in a){print i a[i]}}')
The while
loop expects strings with the following format:
zipfilename file1 file2 file3 ...
This is achieved by
- getting all regular files of the current directory:
find -maxdepth 1 -type f
- looking at the modification time using
stat
- formating the result using
awk
such that all files modified that day are listed in one line
Related videos on Youtube
![Vinay Gowda](https://lh5.googleusercontent.com/-Dxl7lBH7wO8/AAAAAAAAAAI/AAAAAAAAAQ8/K8WsFhcBTtE/photo.jpg?sz=256)
Author by
Vinay Gowda
Updated on September 18, 2022Comments
-
Vinay Gowda almost 2 years
A file (Paths.dat) which contains multiple Paths where it needs to find files which are 15 days old and zip it in same folder with modified date.
Paths.dat :- contains multiple paths in File system ( with delimiter ' | ' )
/docusr1/user01 | /docusr2/user02 | /home/user01
ex:- /docusr1/user01
-rwxrwxrwx. 1 docusr2 docusr2 0 Mar 30 10:52 vinay.txt -rw-rw-r--. 1 docusr2 docusr2 0 Mar 30 10:52 sathish.txt -rw-rw-r--. 1 docusr2 docusr2 625 Apr 2 10:57 demo1.xml -rw-rw-r--. 1 docusr2 docusr2 4430 Apr 2 11:09 sample.xml -rw-rw-r--. 1 docusr2 docusr2 48 Apr 2 14:04 20180402140454.log -rw-rw-r--. 1 docusr2 docusr2 48 Apr 2 14:39 20180402143917.log -rw-rw-r--. 1 docusr2 docusr2 39 Apr 2 14:41 20180402144159.log -rw-rw-r--. 1 docusr2 docusr2 84 Apr 2 14:46 20180402144651.log -rw-rw-r--. 1 docusr2 docusr2 279 Apr 2 14:48 archive.sh -rw-rw-r--. 1 docusr2 docusr2 84 Apr 2 14:48 20180402144814.log -rw-rw-r--. 1 docusr2 docusr2 1228 Apr 5 10:10 real.xml
search for files which are 15 days old and need to zip the files with modified date as zip file name(archive file)
o/p expected:-
20170330.zip -> it should contain all file which are modified on 2017-03-30 20170402.zip 20170405.zip
-
Vinay Gowda about 6 yearsHi Bharat, the above code is working perfectly fine, we have multiple paths in file system where it has to find and zip, we need to loop those paths can you give an approach how to archive it. Thanks in advance
-
Bharat about 6 yearsnice, using stat is good too.
-
Vinay Gowda about 6 yearsThank you Bharat we are able to parse it with multiple paths by looping
-
Vinay Gowda about 6 yearsThank you for your effort to answer this question it helped a lot i achiveing our result @oliv
-
oliv about 6 years@VinayGowda Glad to see it works for you. Do not hesitate to upvote and/or accept this answer.