2

I have log files for many websites in a structure like /var/www/domainname.com/log/access.log and I am looking to clear their content. I need to find a way to do it in one command, but I do not seem to do it, I have tried with

find /var/www/ -type f -name "access.log" -exec cat /dev/null > access.log {} \;

But this actually creates a new access.log file copying contents of all access log files of all websites.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Hashmi
  • 211
  • 4
  • 14

2 Answers2

8

You can use truncate command so that we can truncate all files at once:

find /var/www/ -type f -name access.log -exec truncate -c -s 0 {} +
  • -c ensures that no files are created newly
  • -s 0 sets file size to zero
  • {} + makes sure all files are passed in one shot (up to ARG_MAX)

See:

UsingFind - Greg's Wiki

codeforester
  • 39,467
  • 16
  • 112
  • 140
2

How about:
find /var/www/ -type f -name "access.log" -exec rm {} \; -exec touch {} \;

Moshe Gottlieb
  • 3,963
  • 25
  • 41