Solved

Shell script to delete files

Posted on 2002-07-26
3
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
OwnCloud Development and/or customization.. 19 1,219
Problem to command 8 103
understanding an rpm spec file 5 68
bash file 10 89
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

751 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