Compress folders older than x days

In /opt/abc I have a path where a new folder gets created daily. In that folder a series of files gets created.

I would like to run a script every Sunday at 02:00 and compress each folder which is older than 2 days. I would not like to have everything compressed to one folder, but each folder to get compressed individually. If the compression is successful, then the original folder can be deleted.

I have tried to create a script but all it does is to compress them to a single file. also it does not delete the original folders.

How should I proceed?


Solution 1:

find /path/to/directory -mtime +2 -exec ls "{}" \;

Is a useful snippet to list files over 2 days old, though it only counts full days, and there's an element of rounding that happens there, so using minutes with the -mmin option may work better. I've also seen people relpace the -exec with print0 and pipe the output to xargs, handles unusual filenames better than echo would. You can specify the type of file find is looking for too, to make it only find directories, I think this is the -type option

You can replace the ls in the command with other commands, I often use ls to make sure I'm happy with the output, then replace it with mv /path/to/target when I'm removing files over a certain age. I suppose you could use a tar command in place of the ls to achieve the compression you want.

You could script this, and call the script as a cron job, cron is the script scheduler on Ubuntu (and other linux systems).

That should start you off!