Compressing old files as zip file through script

I have below files in foler one/archive>

one. txt     6/21/2013
two txt      7/23/2013
three.txt    6/20/2013

I wanted to move all the old files (>30 days) compressing single .zip file into one/archive/ as below

two txt         7/23/2013
oldfiles.zip     6/21/2013

Please provide the script for the same

Thanks in advance.

you can find the files older than 30 days and archive them with the below commandline.

find . -mtime +30 | xargs tar cvzf archive.tar

remove files older than 30 days with the below commandline.

find . -mtime +30 | xargs rm
1 Like

Thanks for your reply

Problem facing here is that it is pulling the list of files from sub folder as well.

the gnu find has a maxdepth option that lets you restrict the search to the current directory.

can you try something like below.

find . -maxdepth 1 -mtime +30
1 Like

Got a error saying -maxdepth is invalid option, thanks

---------- Post updated 07-24-13 at 01:36 AM ---------- Previous update was 07-23-13 at 09:36 AM ----------

The above provided solution is not working for me

find . -mtime +30 | zip name -@ -

Here Files get zipped from sub folders as well. I wanted to zip only from current dir files. - maxdepth +1 option not working for me.

can someone help me on this issue.

okay if you are not using GNU find then i guess maxdepth will not be available.

You can do something like below using prune to not descending to and directory other than "dir".

find /full/path/dir \( ! -name dir -o -type f \) -prune -type f
1 Like