LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-22-2017, 10:55 PM   #1
Oblivionsmist
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Rep: Reputation: Disabled
Deleting log files


I need to write some code to clean all my log files/ sub directories that are less than 15MB and write a blank file to them instead of deleting them. How would i go about doing this?

This is my current code:

Code:
sudo find /var -name *.log -size -15M
i dont know what command to use after this.
 
Old 10-22-2017, 11:10 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Usually one would use the package "logrotate"

However, to answer the question as asked, you would append -delete to what you have above to delete them. Note that will zap all files less than 15M even the new ones. See

Code:
man find
man logrotate
Edit: If you want to append a blank line to each one, then append
Code:
-exec sh -c 'echo >> {}' \;
instead.

Last edited by Turbocapitalist; 10-22-2017 at 11:15 PM.
 
Old 10-22-2017, 11:17 PM   #3
Oblivionsmist
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks for the reply, so could i pipe it like this:

Code:
sudo find /var -name *.log -size -15M | -exec sh -c 'echo >> {}' \;
 
Old 10-22-2017, 11:20 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,306
Blog Entries: 3

Rep: Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720Reputation: 3720
Like this:

Code:
sudo find /var -name *.log -size -15M -exec sh -c 'echo >> {}' \;
It is an option for find. See "man find"

If you want to overwrite the file and leave it empty, then replace the append >>

Code:
sudo find /var -name *.log -size -15M -exec sh -c ':> {}' \;
Again since the search is not including a minimum age, you are also zapping new logs.
 
1 members found this post helpful.
Old 10-22-2017, 11:22 PM   #5
Oblivionsmist
LQ Newbie
 
Registered: Oct 2017
Posts: 6

Original Poster
Rep: Reputation: Disabled
cool thanks a bunch
 
Old 10-23-2017, 12:19 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
a request like this usually points to a different problem.

like said earlier, some sort of mechanism to rotate logs should already be in place on your system.
why are you doing it like this, why is it necessary?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving, creating and deleting log files. NotionCommotion Linux - Newbie 8 05-13-2017 06:03 PM
Deleting proxy log files? tezarin Linux - General 3 08-13-2014 11:27 AM
ZFS does not release space even after deleting apache log files in a non-global zone Nue-b Solaris / OpenSolaris 2 03-25-2014 02:06 PM
Disk is not release after deleting log files /home manishrmusale Linux - Newbie 10 05-06-2013 07:50 PM
Need terminal command for safely deleting log files and other useless garbage in / ahurd Linux - Newbie 8 02-24-2009 06:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration