0

Is there an easy step to clear contents of all ".log" file using find command.

Currently i am using "echo -n > filename.log" i have tried echo -n > /var/application-logs/*.log but it is not working..

is there a better way to clear contents of multiple files?

hariharan
  • 3
  • 1
  • 3

3 Answers3

6

To clear everything find /var/application-logs -type f -name "*.log" finds, use this:

find /var/application-logs -type f -name "*.log" -exec tee {} \; </dev/null

If your version of find supports it, use + instead of \; to use a single run of tee for all of the files. Alternately, if a shell glob is sufficient:

tee /var/application-logs/*.log </dev/null
Gordon Davisson
  • 11,216
  • 4
  • 28
  • 33
  • 1
    POSIX stipulates that find supports {} +; I don't know of any that don't. – adaptr Oct 02 '12 at 09:59
  • @adaptr: I could've sworn I saw one that didn't support `+` recently, but maybe I'm imagining it. You're right, though, it is in [the Posix standard](http://pubs.opengroup.org/onlinepubs/009695399/utilities/find.html). – Gordon Davisson Oct 02 '12 at 22:14
  • Thanks a ton ! This saved me an hour worth of my time. – Varun Chandak Aug 07 '19 at 07:50
1

I was facing a similar situation where I had to delete Docker Container Logs with ".log" extension using a command or a shell script (it was scenario-specific)

Here's the script

#!/bin/bash

#SHELL SCRIPT TO DELETE THE CONTENT OF THE DOCKER CONTAINER LOG FILES

#Declaring Array
file_list=()

#While loop to read log file name
 while IFS= read -d $'\0' -r file ; do
     file_list=("${file_list[@]}" "$file")
 done < <(find /var/lib/docker/containers/ -type f -name "*.log" -print0)

#Printing log file names
 echo "${file_list[@]}"

#Deleting the content of the log files
 for f in "${file_list[@]}";do
    if [ -f $f ]
    then
      : > "$f"
    fi
 done

Explanation of Bash Elements


file_list=() is an array containing the results of find /var/lib/docker/containers/ -type f -name "*.log"

While loop is used to read the log file names

while IFS= read -d $'\0' -r file ; do
     file_list=("${file_list[@]}" "$file")
 done < <(find /var/lib/docker/containers/ -type f -name "*.log" -print0)

where the find subprocess is used by the while command via <

and IFS= read -d $'\0' -r file is the while condition

which reads one line of input from the find command.

echo "${file_list[@]}" is to print the file name stored in the array.

for f in "${file_list[@]}";do
    if [ -f $f ]
    then
      : > "$f"
    fi
 done

In the above for-(if)-loop snippet, the loop reads each file from the array ${file_list[@]} and the if statement then clears the content of the file using : > "$f"

1

If you want to stop your logs from growing, use logrotate. You should not be blindly wiping logs just because they are full. There are good HOWTOs available for many different distros.

Alternatively, consider using a syslog server such as rsyslog or syslog-ng.

Andrew
  • 8,002
  • 3
  • 36
  • 44
  • i am using logrotate,,,this is a special case where i want to perform benchmark on application,which i need to monitor logs.So it will be convinient to remove all logs relating to that.. – hariharan Oct 02 '12 at 05:25
  • @hariharan It would be worth updating your original question with this information so that your intent is clear. – Andrew Oct 02 '12 at 05:27
  • can u please check now...i have changed log location – hariharan Oct 02 '12 at 05:32