Delete Files based on size


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete Files based on size
# 1  
Old 06-30-2011
Delete Files based on size

Hello Community!
Im newbie on shell programming and its my first post.

Im trying to make a bash shell script that it removes files of subdirectory.

it is called : rms -{g|l|b} size1 [size2] dir

-g means : remove file or files in dir that is above size1
-l means: remove file or files in dir that is under size1
-b means : remove file or files in dir that is between size1 and size2

i need to find files size with stat

this is my try :


Code:
#!/bin/bash

if [ $# -eq 0 ]; then
    
echo "Syntax is: rms -{g|l|b} size1 [size2] dir"
    
exit -1

elif [ $# -ne 3 ] && [ $# -ne 4 ]; then
    
echo "Syntax is: rms -{g|l|b} size1 [size2] dir"
    
exit -1

elif [ $# -eq 3 ]; then
    
   if [ $1 != "-g" ] && [ $1 != "-l" ] ; then 
    
   echo "Incorrect..[-g,-l]"
    
   exit -1
        
   fi
    
   if [[ $2 != +([0-9]) ]]; then

   echo " $2 must be numeric!"
    
   exit -1
    
   dir=.
    
   elif [ -d $3 ]; then
    
   dir=$3
    
   else
    
   echo "Directory \"$3\" does not exist!"
    
   exit -1
    
   fi

elif [ $# -eq 4 ]; then
    
   if [ $1 -ne -b]; then
    
   echo "Incorrect...[b]"
    
   exit -1
    
   elif [ $2 -ne [0-9] ]; 
   then
    echo "$2 must be numeric!"
    
   exit -1
    
   elif [ $3 -ne [0-9] ]; then
    
   echo "$3 must be numeric!"
    
   exit -1
    
   dir=.
        
   elif [ -d $4 ]; then 
       
   dir=$4
        
   else 
        
   echo "Directory \"$4\" does not exist!"
        
   exit -1
    
   fi

else 

  echo "Syntax is: rms -{g|l|b} size1 [size2] dir"
  
exit -1

fi

if [ $1 == "-g" ]; then
    
for f in $dir/*
        
do
    
filesize=$(stat -c %s $f)
    
if [ $filesize-gt$3 ]; then
    
rm $f
    
echo "Deleted!"
    
fi
        
done

fi


The problem is when i run the script it deletes all files in subdirectory

e.g when i use g in commadn i want to delete the files that they r above size1
it deletes all the files in subdirectory

Can somebody help with this ?

Thank you in advise

Last edited by radoulov; 06-30-2011 at 12:40 PM.. Reason: Code tags.
# 2  
Old 06-30-2011
Missing some spaces. this may help:

Code:
#if [ $filesize-gt$3 ]; then
 if [ $filesize -gt $3 ]; then

---------- Post updated at 09:40 AM ---------- Previous update was at 09:33 AM ----------

found another one:
Code:
#if [ $1 -ne -b]; then
 if [ $1 -ne -b ]; then

Also I think you could improve your program's organization by using a case statement, and shift. That many if-else's is difficult to look through since what's happening now directly depends on a decision made 3 pages ago, better to break into smaller blocks where you can. Remember that an if - then - exit - fi is as good as an if - else, since you can branch just by quitting.

Code:
if [ "$#" -lt 4 ]
then
        echo "Requires at least 4 parameters"
        exit 1
fi

case "$1" in
"-b")
        echo "handle -b stuff here"
        ;;
"-g")
        echo "handle -g stuff here"
        ;;
*)
        echo "First parameter must be -b or -g"
        exit 1
        ;;
esac

shift
size1="$1"
shift
size2="$1"
shift

while [ "$#" -gt 0 ]
do
        echo "Doing stuff with $1"
        shift
done

That way you don't need to keep remembering which number you need to use and can gracefully deal with more than one input directory.

And always make it 'echo rm' when you're just testing, you don't want to run rm when it's not working right!

And you should quote your variables in case the filename contains spaces. "$f" instead of just $f

Last edited by Corona688; 06-30-2011 at 12:45 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to sort the files by size and based subdirectory un UNIX?

I have the below input data in a file and need to get the output as mentioned below. Need to sort the data by size(Asc/des)/by subdirectory Below is the input which is there in a file: 120 /root/path2/part-00000-d3700305-428d-4b13-8161-42051f4ac5ed-c000.json 532 ... (3 Replies)
Discussion started by: ajarramuk
3 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

List duplicate files based on Name and size

Hello, I have a huge directory (with millions of files) and need to find out duplicates based on BOTH file name and File size. I know fdupes but it calculates MD5 which is very time-consuming and especially it takes forever as I have millions of files. Can anyone please suggest a script or... (7 Replies)
Discussion started by: prvnrk
7 Replies

4. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

5. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

6. Shell Programming and Scripting

Delete 2 months before old files when we get alarm directory size > 90%

Please Help me Question: - we manually moving/removing old files, when we are getting alarm as /dir1/dir2 size greater than 90%. we manually moving/removing old files and reduce the file size to less than 90% - I want to delete all 2months before old files once we get the thresh hold >= 90%... (3 Replies)
Discussion started by: piyus
3 Replies

7. Shell Programming and Scripting

Deleting files based on their size

I have several files in a folder and I would like to delete the ones that do not contain all the required information (size) let say 1kb. Any ideas? (4 Replies)
Discussion started by: Xterra
4 Replies

8. Shell Programming and Scripting

Delete Files Based on Datetime Stamp

I have a Unix directory, let's call it /home/id for example purposes. It contains the following files: oldfile.txt.20091101, oldfile.txt.20091102, oldfile.txt.20091103, etc. I am trying to create a Korn Shell script that will go to /home/id and delete any oldfile.txt that has a datetime stamp... (1 Reply)
Discussion started by: ijmoore
1 Replies

9. UNIX for Dummies Questions & Answers

split files based on size

I have a few txt files in some directory and I need to check their sizes one by one. If any of them are greater than 5mb then I need to split the file in two. Can someone help? Thanks. (6 Replies)
Discussion started by: khanvader
6 Replies

10. Shell Programming and Scripting

sort files by date, delete oldest, if total size bigger than

hello people i need your help please i want to achieve the following with the simplest, most efficient shell-tools: i have a directory with a lot of files from users. the script should check which partition the dir is on if the partition with the directory is more than 90% full ... (2 Replies)
Discussion started by: scarfake
2 Replies
Login or Register to Ask a Question