1

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
0
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'
  • Hi 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 – Vinay Gowda Apr 16 '18 at 3:08
  • Thank you Bharat we are able to parse it with multiple paths by looping – Vinay Gowda Apr 16 '18 at 10:57
1

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
  • nice, using stat is good too. – Bharat Apr 16 '18 at 6:55
  • Thank you for your effort to answer this question it helped a lot i achiveing our result @oliv – Vinay Gowda Apr 16 '18 at 11:08
  • @VinayGowda Glad to see it works for you. Do not hesitate to upvote and/or accept this answer. – oliv Apr 16 '18 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.