LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need a shell scrit urgent description in message (https://www.linuxquestions.org/questions/linux-newbie-8/need-a-shell-scrit-urgent-description-in-message-4175534611/)

abhi2412 02-20-2015 08:31 AM

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

TB0ne 02-20-2015 08:43 AM

Quote:

Originally Posted by abhi2412 (Post 5320561)
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.

jpollard 02-20-2015 10:20 AM

Quote:

Originally Posted by abhi2412 (Post 5320561)
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.
...


sudowtf 02-20-2015 11:43 AM

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

TB0ne 02-20-2015 12:08 PM

Quote:

Originally Posted by sudowtf (Post 5320658)
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.

sudowtf 02-20-2015 01:11 PM

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).

TB0ne 02-20-2015 03:41 PM

Quote:

Originally Posted by sudowtf (Post 5320708)
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.

jpollard 02-20-2015 03:49 PM

Quote:

Originally Posted by TB0ne (Post 5320766)
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".

TB0ne 02-20-2015 04:06 PM

Quote:

Originally Posted by jpollard (Post 5320770)
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"?

jpollard 02-20-2015 04:21 PM

yeah... that one is better.

:)


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