-2

I'm trying to reproduce a log gzip cron in other servers,

The "template" I have is:

00 02 * * * cd ~/project_folder/application/logs; find . -type f -name "*.php" -mtime +4 -exec gzip {} \;

Does this gzip the .php's in that dir every day and deletes the uncompressed files?

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

2 Answers2

2

You can visit https://crontab-generator.org/ it's a good tool for generating crontab lines and you will understand what the numbers and asterisks mean. Now if you choose

  • 0 for minutes
  • 2am for hour
  • leave everything else as it is
  • for the command use cd ~/project_folder/application/logs; find . -type f -name "*.php" -mtime +4 -exec gzip {} \;

and click the generate crontab line you will see an example of the times it will run. Plus it will add this chuck >/dev/null 2>&1 don't worry about it, it's an output instruction for the command and good to have it in the crontab.

enter image description here

The argument to -mtime is interpreted as the number of whole days in the age of the file. -mtime +n means strictly greater than, -mtime -n means strictly less than.

So this command will find and gzip all the *.php files where the latest modification time (mtime) of those files are greater than 4 days.

Edit: I couldn't recall the -k argument of gzip too. So as per @chang-qian response.

Without -k, gzip implies that it will delete the original files after their compression.

NikoKyriakid
  • 610
  • 7
  • 14
1

It does compress them daily, and gzip automatically deletes source files afterwards. The 00 02 * * * at line beginning means 02:00 every day.

From man gzip:

-k, --keep Keep (do not delete) input files during compression or decompression.

That means, without -k, gzip implies rm.

Chang Qian
  • 1,091
  • 8
  • 15
  • But I don't understand why the old ones are not in the server – Toni Michel Caubet Nov 07 '17 at 09:31
  • @toni-michel-caubet Edited my answer. I was stupid not recalling that gzip removes source files by default. – Chang Qian Nov 07 '17 at 09:35
  • Ok, that makes sense now. Also, do you know the part '-mtime +4' ? I see in the server that there are still 4-5 uncompressed files. Does it check that they are +4 days old? Thanks! – Toni Michel Caubet Nov 07 '17 at 09:41
  • @toni-michel-caubet You're right, see https://stackoverflow.com/questions/27700943/unix-find-mtime-a-negative-or-postive-value . Actually a little Googling will give you that answer – Chang Qian Nov 07 '17 at 09:44