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?

1
  • 1
    What is your exact find command?
    – Andrew
    Oct 2, 2012 at 5:44

3 Answers 3

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
3
  • 1
    POSIX stipulates that find supports {} +; I don't know of any that don't.
    – adaptr
    Oct 2, 2012 at 9: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. Oct 2, 2012 at 22:14
  • Thanks a ton ! This saved me an hour worth of my time. Aug 7, 2019 at 7:50
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.

3
  • 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 2, 2012 at 5:25
  • @hariharan It would be worth updating your original question with this information so that your intent is clear.
    – Andrew
    Oct 2, 2012 at 5:27
  • can u please check now...i have changed log location
    – hariharan
    Oct 2, 2012 at 5:32
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
  • This is exactly the type of answer we love to see on Stack Exchange. Oct 2, 2020 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .