Solved

Shell script to delete files

Posted on 2002-07-26
3
341 Views
Last Modified: 2010-04-22
Sup everyone i need to write a script that will check how many files there are in a folder if there are more then 500 files then do a rm -rf
thanks in advance.
0
Comment
Question by:jpfrancai
  • 2
3 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 50 total points
ID: 7181208
find . -type f -maxdepth 1|awk '{i++}END{if(i>500){print "rm -rf your-pattern"}}'|sh
0
 
LVL 1

Author Comment

by:jpfrancai
ID: 7181241
Thanks Its great short and simple i had found another way to do it but im going to use yours, thanks again.
damn ahoffmann i've seen you around a lot, you must really know your(Edited by Computer101).  hope i can pick up some of those skills one day.



MAX_NUMBER=5

# DO NOT CHANGE THIS

NUMBER_OF_FILES=0

if [ $# -gt 0 -a -d "$1" ]; then #make sure given argument is a directory
        echo " There are $1 of files";
        cd "$1";
         for i in *; do
                   NUMBER_OF_FILES=$(($NUMBER_OF_FILES + 1)); #increment counter
                     #echo "$i" #print the name of the file
                      done
                       echo $NUMBER_OF_FILES;

                       if [ $NUMBER_OF_FILES -gt "$MAX_NUMBER" ]; then

  # Enter here what you want the script to do in case there are 5 or more files

                          echo "there are more then $MAX_NUMBER of files";

                          else

  # What to do if there are less then 5

                               echo "There are less then $MAX_NUMBER of files";
                       fi
               else #print some help
                        echo "Usage: countfiles [dir]"
                         echo "  Where dir is a directory whos files you wish to count"
                 fi
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7181753
KISS - keep it small and simple
;-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now