0

I'm running into 2 problems when working with my logs and the find command.

  1. I am having problems with the mtime option. The 1st command below shows me a few files from March 5th till March 7th, like I believe it should. However, when I add +1 to mtime instead of +0, it only does an ls of the current directory I'm in when running the command. It's just listening my directories under /home/user/. Not sure why +1 is breaking it.
sudo find /logs/ -type f -name '*.log' -mtime +0 | xargs ls -l
  1. The second issue I'm running into is I want to find all logs that are older than 1 day and zip them up individually. So if my find command finds 1-1-13.log, 1-2-13.log, and 1-3-13.log, I want each one to be turned into log.gz. I tried this by doing the following command but it isn't working. I assume I may have to do a small bash script for this but was hoping to do it by one quick command.

sudo find /logs/ -type f -name '*.log' -mtime +0 | xargs gzip -9 *

Most of the examples I've seen involve zipping all files into 1 archive but I want each one individual and I want them to stay in the same location where it found them.

Thanks in advance.

1
  • 1. What is the output of ls -l /logs/?
    – quanta
    Mar 8, 2013 at 15:21

2 Answers 2

1

However, when I add +1 to mtime instead of +0, it only does an ls of the current directory I'm in when running the command.

Your xargs command in this example is weird:

sudo find /logs/ -type f -name '*.log' -mtime +0 | xargs ls -l

If you want to list the files, just use find by itself, e.g:

sudo find /logs/ -type f -name '*.log' -mtime +0

Or:

sudo find /logs/ -type f -name '*.log' -mtime +1

If that command produces the list of files you expect, the next step should be easy (below). If that find command does not produce the list of files you expect, it suggests that those files have actually been modified more recently than you think.

The second issue I'm running into is I want to find all logs that are older than 1 day and zip them up individually. So if my find command finds 1-1-13.log, 1-2-13.log, and 1-3-13.log, I want each one to be turned into log.gz.

You have a problem with your xargs command:

sudo find /logs/ -type f -name '*.log' -mtime +0 | xargs gzip -9 *

You need to get rid of that * in your xargs command; you expect the list of files passed to gzip to come from the find command, rather than via shell globbing. So this should work just fine:

find /logs -type f -name '*.log' | xargs gzip -9

You could also do this using find ... -exec ..., but this is typically a less performant solution.

1
  • Thanks for your help. The reason I did the xargs ls command was just to verify my xargs syntax was working correctly. I'm still seeing some weird issues from the +1 but I'll let it go over the weekend to 100% ensure it is older than 1 day. The gzip portion worked fine on my testing and I've redone my crons to try it over the weekend.
    – Eric
    Mar 8, 2013 at 16:07
2

2.

sudo find /logs/ -type f -name '*.log' -mtime +0 -exec gzip -9 {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .