0

I have folder with subfolders and sessions logs. I need some commands or script to tar this folder without usage files and then delete it. I have this construction, but I'm not sure if this leave files with handling.

tar -zcvf /srv/log/prod/dms/espis/documentum/log/00029392-$(date +%F).tar /srv/log/prod/dms/espis/documentum/log/0002939
cd /srv/log/prod/dms/espis/documentum/log/00029396
rm -R *

Any ideas?

1

The rm -R will actually delete all the files (if it has enough permissions). If you want to leave only the currently opened files, you can check with lsof +D /srv/log/prod/dms/: it will list all the files located under /srv/log/prod/dms/ and that are currently opened.

Be careful, to script that, you should be sure that your daemon keeps its files handlers opened (I mean, be sure it doesn't open them only when it needs to write in it...).

So, after your tar, you could script something like that:

for f in $( find /srv/log/prod/dms/espis/documentum/log -type f -print ) ; do
  lsof +D /srv/log/prod/dms/espis/documentum/log | grep -w $f\$ >/dev/null
  if [ $? -eq 1 ] ; then
    echo "Delete $f"
  else
    echo "Keep $f"
  fi
done

EDIT: Of course, if your daemon operates log rotation, it would be easier to delete log files older than n days with something like find /srv/log/prod/dms/espis/documentum/log -type f -mtime +99 -exec rm -f {} \; (here, -mtime +99 means "not modifed since 99 days")

  • Thank you for help. I used the easier way. It was better in my case. Have a nice day. – Michell-Boy Mar 26 '15 at 14:17

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.