Solved

Shell script to delete files

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Optimize bash script 20 470
Debian 3.16.7 3 152
/var becomes read-only & can't create transaction lock on /var/lib/rpm 8 391
Linux : when to add the entries in /etc/hosts file 22 100
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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