0

I am working on a purging logic where i need to delete the logs older than 7 days.

Now the logs are of two types : 1) begins with developer_ and 2) ends with _c

The logs I am concerned about are only after /NAS/logs and not after any other subfolders in this path.

below are logs sample:

/NAS/logs/.snapshot/Enterprise_Primary.2019-11-17_1300/developer_score_20191113.log
/NAS/logs/.snapshot/Enterprise_Primary.2019-11-17_1300/trdcvf_03_00_c.log.20191113163858
/NAS/logs/trddffgc004_00_c.log.20191118030038
/NAS/logs/developer_run_score.log.20191118030039

Here I want to exclude all the logs which are in the hidden folder .snapshot as i dont have access on those.

I am trying to identify them using below command but it keeps giving me the .snapshot files as well.

I also tried -prune option but no help.

  find /NAS/logs/  -mindepth 1 -type f  \( -iname "*_c.log.*" -or -iname "developer_run_*"  \) -not -path "./.snapshot/*"

Final command

find /NAS/logs/  -mindepth 1 -type f  \( -iname "*_c.log.*" -or -iname "developer_run_*"  \) -not -path "./.snapshot/*"-mtime +7 -delete

Can anyone please help me to exclude that directory from the find?

  • Got the answer-- I was using mindepth 1 instead of maxdepth 1 – user3901666 Nov 18 '19 at 10:40
  • 1
    None of your files matches your criterion 2 "ends with _c". Did you mean "contains _c" perhaps? But even then you have no files matching criterion 1 and criterion 2. Perhaps that was also intended to be or? – roaima Nov 18 '19 at 11:21
  • EXPR -prune -o ... this reminds me of unix.stackexchange.com/questions/549274/…. (-prune can be important for performance reasons, nomen est omen, try -D tree debug option. – rastafile Nov 18 '19 at 11:49
  • @roaima, sorry for the confusion ..i meant to say _c.log.. Also, I was able to get the desired by using the maxdepth 1 option. is that fine? – user3901666 Nov 18 '19 at 12:28
  • 1
    In that case the solution is far simpler. I'll update my answer now that you've provided more information. – roaima Nov 18 '19 at 17:12
3

None of your example files satifies the second of the filename criteria you have specified. There are files containing _c but none ends with _c. Assuming you meant contains you can construct the find command like this:

find /NAS/logs -type d -path '/NAS/logs/.snapshot' -prune -o -type f -mtime +7 \( -name 'developer_*' -o -name '*_c*' \) -print

This can be broken down into two parts (the -o (or) condition):

  1. The tree path matching /NAS/logs/.snapshot is discarded (pruned) and no further action is considered there.
  2. Files modified more than seven days ago, with a name matching either developer_* or *_c*, are listed (printed).

You should replace -print with -delete, or simply append -delete to remove the files you've matched.

If you want to prune any instance of the directory .snapshot you could modify the first part of the matching criteria like this

find /NAS/logs -type d -name '.snapshot' -prune -o ...

Now that you have added extra information to your question, none of the pruning complexity is actually required any more. You can just use this:

find /NAS/logs -maxdepth 1 -type f -mtime +7 \( -name 'developer_*' -o -name '*_c*' \) -print

(This shows the importance of providing full information in your question rather than delivering it piecemeal - or not at all.)

  • Thanks @Roaima will make sure i put all the information going forward. – user3901666 Nov 19 '19 at 6:27
0

I also tried -prune option but no help

And what happened?

The link in my comment shows there is a problem with -prune and -delete together, because of -depth. find-and-prune-explanations

I think here you have to do something like

... -path '*/.snapshot/*' -o ... ... -delete

Only with massive data (and tests) you would miss the '-prune' action.


find kernel -path '*/sched/*' -o -name '???.c' -exec grep -H 'struct ta' {} +

finds lines in sys.c, pid.c and cpu.c.

The same without the -o (same as with -a) gives three lines in kernel/sched/psi.c only.

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.