LinuxQuestions.org
Register a domain and help support LQ
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 02-20-2015, 09:31 AM   #1
abhi2412
LQ Newbie
 
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled
need a shell scrit urgent description in message


HI Everyone, need a shell script which checks size of a directory and if it gets greater than 1 GB then delete all the files in it.

ex: dir : /var/log/

/log contains numerous .log files, so script should delete .log files always when /log size is greater than 1 GB , it should not delete the directory
 
Old 02-20-2015, 09:43 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,339

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Quote:
Originally Posted by abhi2412 View Post
HI Everyone, need a shell script which checks size of a directory and if it gets greater than 1 GB then delete all the files in it.

ex: dir : /var/log/

/log contains numerous .log files, so script should delete .log files always when /log size is greater than 1 GB , it should not delete the directory
Great..so if you need a shell script, GO WRITE IT YOURSELF. If you have written it, and are having problems, then post what YOU have written/tried, and tell us where you're stuck.

Read the "Question Guidelines" link in my posting signature. We will be happy to HELP you with things, but asking for a handout and showing ZERO effort of your own, isn't the way to get help. There are MANY THOUSANDS of bash scripting guides you can easily find with a Google search...just like you found THIS site.
 
Old 02-20-2015, 11:20 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Quote:
Originally Posted by abhi2412 View Post
HI Everyone, need a shell script which checks size of a directory and if it gets greater than 1 GB then delete all the files in it.

ex: dir : /var/log/

/log contains numerous .log files, so script should delete .log files always when /log size is greater than 1 GB , it should not delete the directory
You don't want to delete "all" of them. Some will be open, and thus not released anyway.

You need them when problems happen so that you can find out what the problem is, and find out how to fix it.

If all you are concerned about is the old logs, you might look into using an existing script - "logrotate". It is also frequently installed as part of installation.

From the manpage:
Code:
LOGROTATE(8)             System Administrator's Manual            LOGROTATE(8)

NAME
       logrotate ‐ rotates, compresses, and mails system logs

SYNOPSIS
       logrotate [-dv] [-f|--force] [-s|--state file] config_file ..

DESCRIPTION
       logrotate  is  designed to ease administration of systems that generate
       large numbers of log files.  It allows automatic rotation, compression,
       removal, and mailing of log files.  Each log file may be handled daily,
       weekly, monthly, or when it grows too large.

       Normally, logrotate is run as a daily cron job.  It will not  modify  a
       log  multiple  times  in  one  day unless the criterion for that log is
       based on the log's size and logrotate is being run multiple times  each
       day, or unless the -f or --force option is used.
...
 
1 members found this post helpful.
Old 02-20-2015, 12:43 PM   #4
sudowtf
Member
 
Registered: Nov 2013
Posts: 187

Rep: Reputation: 45
In the interest of actually helping a newbie

you could use the find command to find files older than X days and delete them

something like
Code:
find /var/log/ -maxdepth 1 -type f -mtime +30 -name
or find log-files over X megabytes
Code:
find /var/log/ -maxdepth 1 -size 5M -name "*.log*"
or a combination of the two above (-mtime +X -size Y)

to delete, just add "- delete" to the end of the command.

and if you like it, add it to your root's cronjobs
Code:
EDITOR=nano sudo crontab -e
 
1 members found this post helpful.
Old 02-20-2015, 01:08 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,339

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Quote:
Originally Posted by sudowtf View Post
In the interest of actually helping a newbie
I appreciate your sentiment, and agree with it. However, this isn't asking for a handUP, it's asking for a handOUT. The OP showed NO EFFORT at all of their own, and only asked US to write a script FOR THEM.

I'm very hesitant to help ANYONE who doesn't show any effort of their own, especially with a topic like this, that has so much information, examples, and tips available with a very quick Google search.
 
Old 02-20-2015, 02:11 PM   #6
sudowtf
Member
 
Registered: Nov 2013
Posts: 187

Rep: Reputation: 45
i mostly concur... but sometimes people just don't even know how to start, not even what to google. i work at a university and you would think the employees are the brightest people around, but they are VERY FAR FROM IT (self inclusive).
 
Old 02-20-2015, 04:41 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,339

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Quote:
Originally Posted by sudowtf View Post
i mostly concur... but sometimes people just don't even know how to start, not even what to google.
If it's something very hard to explain, I'd agree. But just putting the question as the OP stated into Google pulls up lots of good starting points.
Quote:
i work at a university and you would think the employees are the brightest people around, but they are VERY FAR FROM IT (self inclusive).
I've worked with several, and lots of doctors and hospitals, and I know what you're talking about. But I take such things with a grain of salt...they may be a great neurosurgeon...but have no ideas about a computer, and I'd not expect them to.

In this case, it's a systems administrator who needs a script to perform maintenance...which would be like a neurosurgeon, asking how to perform surgery.
 
Old 02-20-2015, 04:49 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Quote:
Originally Posted by TB0ne View Post
If it's something very hard to explain, I'd agree. But just putting the question as the OP stated into Google pulls up lots of good starting points.

I've worked with several, and lots of doctors and hospitals, and I know what you're talking about. But I take such things with a grain of salt...they may be a great neurosurgeon...but have no ideas about a computer, and I'd not expect them to.

In this case, it's a systems administrator who needs a script to perform maintenance...which would be like a neurosurgeon, asking how to perform surgery.
Not the best comparison... If the neurosurgeon is in a different hospital then they just MIGHT have to ask about the procedure to get permission .... which is part of "how to perform surgery".
 
Old 02-20-2015, 05:06 PM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,339

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Quote:
Originally Posted by jpollard View Post
Not the best comparison... If the neurosurgeon is in a different hospital then they just MIGHT have to ask about the procedure to get permission .... which is part of "how to perform surgery".
The only part I agree about is "Not the best comparison"...that is much like saying them getting dressed in the morning is part of how to perform surgery. If you want to split hairs that finely, then it sure is. And I believe you know the difference.

Want a better one? How about "delivery driver not knowing how to start their car"?
 
Old 02-20-2015, 05:21 PM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
yeah... that one is better.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need advice/help in regards to encryption/description with PHP and SHELL indileosat Programming 5 11-22-2012 07:06 PM
how to set the Environment variable from shell Scrit Sundaram Linux - Software 2 10-14-2006 12:59 AM
Error message received from system Error while reading filter description for true Steel_J Linux - Software 2 03-04-2006 07:10 PM


All times are GMT -5. The time now is 10:20 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration