EvilZone

General Tech => Operating System => : lucid March 05, 2013, 10:07:44 PM

: Clearing /var/log
: lucid March 05, 2013, 10:07:44 PM
For security's sake, I was looking into removing some logs at shutdown. Namely I was looking at the logs in /var/log.

Does anyone have any input on this? I want to make sure this won't damage anything if I erase all logs every shutdown.
: Re: Clearing /var/log
: ande March 05, 2013, 10:10:35 PM
Pretty sure you will be fine, just make sure files that are supposed to be there, empty or not, are there and that their permissions/owners are correct.
: Re: Clearing /var/log
: lucid March 05, 2013, 10:15:02 PM
I kinda thought so. Thanks for confirming  :D
: Re: Clearing /var/log
: wookie March 05, 2013, 10:49:44 PM
An alternative would be truncating them all?


Try something like...


:
cd /var/log
echo > $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)


That should truncate the files in /var/log that aren't gzip compressed and have the term "log" in the filename.  You may want to do some extra validation to make sure it's a file or a folder.


You could also look at doing some form of log rotation on a daily basis and either compress your log files or move them off disk?
: Re: Clearing /var/log
: lucid March 05, 2013, 10:59:00 PM
An alternative would be truncating them all?


Try something like...


:
cd /var/log
echo > $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)


That should truncate the files in /var/log that aren't gzip compressed and have the term "log" in the filename.  You may want to do some extra validation to make sure it's a file or a folder.


You could also look at doing some form of log rotation on a daily basis and either compress your log files or move them off disk?

That didn't work.

:
bash: $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2): ambiguous redirect
: Re: Clearing /var/log
: wookie March 05, 2013, 11:08:39 PM
That didn't work.

:
bash: $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2): ambiguous redirect


Sorry, should've tested it.


Try this in a bash script:


:

for file in $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)
do
        echo > $file
done
: Re: Clearing /var/log
: wookie March 05, 2013, 11:13:32 PM

:

for file in $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)
do
        echo > $file
done


You could wrap the echo > $file in something like this;


:

if [ -f $file ]
then
echo > $file
fi
[size=78%]


Which should validate that it isn't a system file or directory judging by http://tldp.org/LDP/abs/html/fto.html[/size]
: Re: Clearing /var/log
: lucid March 05, 2013, 11:22:39 PM
Well I found a pretty simple solution given that no log files in /var/log are not to be truncated. Simply:

cat /dev/null > whatever.log

Now to do it to the whole directory recursively...

EDIT: Wow, I have the perfect solution that I've tested and have no problems with.

:
find /var/log -type f exec sh -c '> "{}"' \;

Works like a charm.
: Re: Clearing /var/log
: wookie March 05, 2013, 11:37:07 PM
Well I found a pretty simple solution given that no log files in /var/log are not to be truncated. Simply:

cat /dev/null > whatever.log

Now to do it to the whole directory recursively...


Don't really understand what that does that what I've posted doesn't do?  I assumed you wanted to remove all logs, not just a specific log.


Regardless, both methods will truncate files.


Bash isn't really my strong point, but you could use PHP?


You could use opendir and readdir methods but you could also do something like this, which would allow you to recurse quite easily and parse it into a nice array for you to do some truncating with:


:

<?php


$_dir 
"/var/log";
$cmd "ls -lR $_dir";
$files=$out1=$out2=null;
exec($cmd$out1$out2);
foreach(
$out1 as $k=>$line){
        if(
substr($line08) == '/var/log'){
                
$total $out1[$k 1];
                
$total explode(" "trim($total));
                if(
$total[1] < 1) continue;
                
$dir substr($line8);
                
$dir substr($dir0, -1);
        }
        if(
substr($line08) != '/var/log' && substr($line05) != 'total'){
                
$file explode(":"$line);
                
$file explode(" "$file[1]);
                
$file $file[1];
                
$files[$dir][]="$_dir$dir$file";
        }
}


print_r($files);


?>

: Re: Clearing /var/log
: lucid March 06, 2013, 12:32:25 AM
I guess the two bash scripts do the same thing, one is just simpler. I like the PHP idea.
: Re: Clearing /var/log
: proxx March 06, 2013, 09:47:07 AM
Just wanted to state that actually deleting certain log files can cause problems.
And for security sake you could better overwrite them with garbage than deleting them.
Thats is if your paranoid.
: Re: Clearing /var/log
: lucid March 06, 2013, 05:43:51 PM
I'm not deleting any of them. I suppose I could look into overwriting them. Perhaps something like that could be done with the shred command, without actually deleting the files that is.