Solved

Shell script to delete files

Posted on 2002-07-26
3
342 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem passing file name with spaces to shell script 12 252
Chinese translation in RedHat 10 47
Cloning a part of HDD MIPS Linux 16 110
WiFi Router device supports GPON! 3 30
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

12 Experts available now in Live!

Get 1:1 Help Now