Solved

Shell script to delete files

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

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Install Apache on a Mac with Homebrew | Apache Config Issues 1 183
Installing rpm without root access 1 52
monitor and log every file access on redhat/linux 4 123
awk file 6 52
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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