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
  • 34,080
  • 14
  • 96
  • 122
Hashmi
  • 205
  • 3
  • 12

2 Answers2

7

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
  • 34,080
  • 14
  • 96
  • 122
2

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

Moshe Gottlieb
  • 3,792
  • 24
  • 38